Skip to content

Commit

Permalink
Format and some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Oct 13, 2019
1 parent d77c5fa commit 0740a8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,15 @@ Uses truthy logic and applies `and` or `or` to the values.

and: All arguments need to be truthy
or: At least one argument needs to be truthy

## Like, Prefix, Suffix

Syntax: `{like val contains}`, `{prefix val startsWith}`, `{suffix val endsWith}`

Truthy check if a value contains a sub-value, starts with, or ends with

## Format

Syntax: `{format "%s" ...}`

Formats a string based on `fmt.Sprintf`
19 changes: 19 additions & 0 deletions pkg/expressions/keyFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ func kfSuffix(args []KeyBuilderStage) KeyBuilderStage {
})
}

// {format str args...}
// just like fmt.Sprintf
func kfFormat(args []KeyBuilderStage) KeyBuilderStage {
return KeyBuilderStage(func(context KeyBuilderContext) string {
if len(args) < 1 {
return ErrorArgCount
}
format := args[0](context)

printArgs := make([]interface{}, len(args)-1)
for idx, stage := range args[1:] {
printArgs[idx] = stage(context)
}

return fmt.Sprintf(format, printArgs...)
})
}

var defaultFunctions = map[string]KeyBuilderFunction{
"coalesce": KeyBuilderFunction(kfCoalesce),
"bucket": KeyBuilderFunction(kfBucket),
Expand Down Expand Up @@ -272,4 +290,5 @@ var defaultFunctions = map[string]KeyBuilderFunction{
"like": KeyBuilderFunction(kfLike),
"prefix": KeyBuilderFunction(kfPrefix),
"suffix": KeyBuilderFunction(kfSuffix),
"format": KeyBuilderFunction(kfFormat),
}

0 comments on commit 0740a8b

Please sign in to comment.