textbuilder

package
v3.14.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2017 License: Apache-2.0, Apache-2.0 Imports: 4 Imported by: 9

Documentation

Overview

This package provides lazy-loading and postfix binding for text building.

Abstract

When we are generating complex SQL statement, the final code of SQL is decided by query parameters:

SELECT *
FROM tab_car
WHERE car_name = ?

For 'car_name = ?', it is only shown if and only if user has input "viable" value on search field of UI.

Text by condition

You could use post-condition to decide whether or not a text should be shown:

t := StringGetter("Hello! If you are happy.").Post().Viable(false)

fmt.Sprintf("%s", t) // Nothing shown

With SQL syntax, you could decide whether or not to generate "WHERE" by variable:

where := Prefix(
	StringGetter("WHERE "),
	StrinGetter("car_name = ?").
		Post().Viable(carName != ""),
)

Breeds multiple text

You could use some built-in functions to generate multiple text by array/slice.

arrayData := []int { 1, 3, 5 }

// Generates "?, ?, ?"
conditions := RepeatAndJoinByLen("?", StringGetter(", "), arrayData)

Short term DSL

There are some pre-defined, short-typed DSLs to make code shorter:

where := Prefix(Dsl.S("WHERE "), Dsl.S("car_name = ?").
	Post().Viable(carName != ""))

Index

Examples

Constants

View Source
const EmptyGetter = StringGetter("")

This instance provides string of empty("")

Variables

View Source
var Dsl = &dsl{
	A:  ToTextGetter,
	AL: ToTextList,
	S: func(v string) TextGetter {
		return StringGetter(v)
	},
	SER: NewStringerGetter,
	PF:  TextGetterPrintf,
}

Short name of building blocks for text builder(DSL)

A - Any value to StringGetter
AL - Any list of objects to TextList
S - String value to StringGetter
SER - object of "fmt.Stringer" to StringerGetter
PF - "fmt.Sprintf" talk to TextGetter
View Source
var J = map[string]Distiller{
	",":  BuildJoin(Dsl.S(",")),
	", ": BuildJoin(Dsl.S(", ")),
}

Common characters for distiller

View Source
var QQ = map[string]Transformer{
	"()": BuildSurrounding(Dsl.S("("), Dsl.S(")")),
	"[]": BuildSurrounding(Dsl.S("["), Dsl.S("]")),
	"{}": BuildSurrounding(Dsl.S("{"), Dsl.S("}")),
	"<>": BuildSurrounding(Dsl.S("<"), Dsl.S(">")),
	"\"": BuildSameSurrounding(Dsl.S("\"")),
	"'":  BuildSameSurrounding(Dsl.S("'")),
}

Build-in double-quoted transformers

Functions

func IsViable

func IsViable(value interface{}) bool

Builds viable getter if the value is viable

The value would be evaluated eagerly.

For string - must be non-empty
For TextGetter - the result of content must be non empty
For array, slice, map, chan - the len(array) > 0

Otherwise - value.IsNil() should be false

Types

type Breeder

type Breeder func(TextGetter) TextList

Defines the function to generate TextList from a TextGetter

func BuildRepeat

func BuildRepeat(times int) Breeder

func BuildRepeatByLen

func BuildRepeatByLen(v interface{}) Breeder

type DefaultListPost

type DefaultListPost struct {
	// contains filtered or unexported fields
}

Implements default post prcessor for a list

func NewListPost

func NewListPost(list TextList) *DefaultListPost

Initialzie an instance of DefaultListPost

func (*DefaultListPost) Distill

func (l *DefaultListPost) Distill(d Distiller) TextGetter

func (*DefaultListPost) Join

func (l *DefaultListPost) Join(separator TextGetter) TextGetter

type DefaultPost

type DefaultPost struct {
	// contains filtered or unexported fields
}

Implements default post processor

func NewPost

func NewPost(content TextGetter) *DefaultPost

Initialize a new instance of post processor with default operations

func (*DefaultPost) Breed

func (p *DefaultPost) Breed(b Breeder) TextList

func (*DefaultPost) Post

func (p *DefaultPost) Post() PostProcessor

func (*DefaultPost) Prefix

func (p *DefaultPost) Prefix(prefix TextGetter) PostProcessor

func (*DefaultPost) Repeat

func (p *DefaultPost) Repeat(times int) TextList

func (*DefaultPost) RepeatByLen

func (p *DefaultPost) RepeatByLen(lenObject interface{}) TextList

func (*DefaultPost) String

func (p *DefaultPost) String() string

func (*DefaultPost) Suffix

func (p *DefaultPost) Suffix(suffix TextGetter) PostProcessor

func (*DefaultPost) Surrounding

func (p *DefaultPost) Surrounding(prefix TextGetter, suffix TextGetter) PostProcessor

func (*DefaultPost) Transform

func (p *DefaultPost) Transform(t Transformer) PostProcessor

func (*DefaultPost) Viable

func (p *DefaultPost) Viable(v bool) PostProcessor
Example

You could use "Viable" function to control the final output of a value

surrounding := Surrounding(
	StringGetter("["),
	StringGetter("Hello"),
	StringGetter("]"),
)

shown := surrounding.Post().Viable(true)
hidden := surrounding.Post().Viable(false)

fmt.Printf("1 - %s\n", shown)
fmt.Printf("2 - %s\n", hidden)
Output:

1 - [Hello]
2 -

type Distiller

type Distiller func(TextList) TextGetter

Defines the function to reduce a TextList to a TextGetter

func BuildJoin

func BuildJoin(separator TextGetter) Distiller

type ListPost

type ListPost interface {
	Post() ListPostProcessor
}

Gets posting processor of a TextList

type ListPostProcessor

type ListPostProcessor interface {
	Distill(Distiller) TextGetter
	Join(separator TextGetter) TextGetter
}

Defines operations for a TextList

type ObjectLen

type ObjectLen interface {
	Len() int
}

Used to get len of an object

This interface is usually used with RepeatByLen().

type Post

type Post interface {
	// Retrieves the PostProcessor, which defines interfaces for postfix on TextGetter
	Post() PostProcessor
}

Gets posting processor of a TextGetter

type PostProcessor

type PostProcessor interface {
	TextGetter
	Transform(t Transformer) PostProcessor
	Breed(b Breeder) TextList
	Prefix(prefix TextGetter) PostProcessor
	Suffix(suffix TextGetter) PostProcessor
	Surrounding(prefix TextGetter, suffix TextGetter) PostProcessor
	Repeat(times int) TextList
	RepeatByLen(lenObject interface{}) TextList
	Viable(v bool) PostProcessor
}

Defines the operations of post processor

type StringGetter

type StringGetter string

Implements the text getter with string value

Example
stringGetter := StringGetter("Your string")

fmt.Printf("%s", stringGetter.String())
Output:

Your string

func (StringGetter) Post

func (t StringGetter) Post() PostProcessor

func (StringGetter) String

func (t StringGetter) String() string

type StringerGetter

type StringerGetter struct {
	// contains filtered or unexported fields
}
Example
// Weight implments "fmt.Stringer" interface
var w1 = Weight(77)
var getter = NewStringerGetter(w1)

fmt.Printf("%s", getter.String())
Output:

Your weight is 77

func NewStringerGetter

func NewStringerGetter(v fmt.Stringer) *StringerGetter

Converts fmt.Stringer interface to TextGetter

func (*StringerGetter) Post

func (s *StringerGetter) Post() PostProcessor

func (*StringerGetter) String

func (s *StringerGetter) String() string

type TextGetter

type TextGetter interface {
	fmt.Stringer
	Post
}

Defines the common interface for a text object, which is evaluated lazily.

func Join

func Join(separator TextGetter, getters ...TextGetter) TextGetter

Joining the viable element of getters

func JoinTextList

func JoinTextList(separator TextGetter, textList TextList) TextGetter

Joining the viable element of TextList

func Prefix

func Prefix(prefix TextGetter, content TextGetter) TextGetter

Prefixing the content(if the content viable)

func RepeatAndJoin

func RepeatAndJoin(text TextGetter, separator TextGetter, times int) TextGetter

func RepeatAndJoinByLen

func RepeatAndJoinByLen(text TextGetter, separator TextGetter, lenObject interface{}) TextGetter

func Suffix

func Suffix(content TextGetter, suffix TextGetter) TextGetter

Suffixing the content(if the content is viable)

func Surrounding

func Surrounding(prefix TextGetter, content TextGetter, suffix TextGetter) TextGetter

Surrounding the content(if the content is viable)

func SurroundingSame

func SurroundingSame(s TextGetter, content TextGetter) TextGetter

Surrounding the content(if the content is viable)

func TextGetterPrintf

func TextGetterPrintf(format string, a ...interface{}) TextGetter

func ToTextGetter

func ToTextGetter(v interface{}) TextGetter

Converts any value to TextGetter

If the value is text getter, this function return it natively.

If the value is string, this function cast it to StringGetter.

Otherwise, use fmt.Sprintf("%v") to retrieve the string representation of input value.

type TextGetters

type TextGetters []TextGetter

func Getters

func Getters(getters ...TextGetter) TextGetters

func (TextGetters) Get

func (t TextGetters) Get(index int) TextGetter

func (TextGetters) Len

func (t TextGetters) Len() int

func (TextGetters) Post

func (t TextGetters) Post() ListPostProcessor

type TextList

type TextList interface {
	ListPost
	ObjectLen
	Get(int) TextGetter
}

func Repeat

func Repeat(text TextGetter, times int) TextList

Repeating the viable element of TextList

func RepeatByLen

func RepeatByLen(text TextGetter, lenObject interface{}) TextList

Repeats the len of object:

For object len: use Len() function For String: use utf8.RuneCountInString(<string>) function For Array, Chan, Map, or Slice: use reflect.Value.Len() function

func ToTextList

func ToTextList(anyObjects ...interface{}) TextList

Converts multiple values to TextList, for the conversion of element, see ToTextGetter

type Transformer

type Transformer func(TextGetter) TextGetter

Defines the transformation of a TextGetter to another

func BuildPrefix

func BuildPrefix(prefix TextGetter) Transformer

func BuildSameSurrounding

func BuildSameSurrounding(s TextGetter) Transformer

func BuildSuffix

func BuildSuffix(suffix TextGetter) Transformer

func BuildSurrounding

func BuildSurrounding(prefix TextGetter, suffix TextGetter) Transformer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL