Compare commits
5 Commits
c480e81c0a
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 549578cb48 | |||
| ab722abe2d | |||
| b1fdc7d511 | |||
| 857155274e | |||
| f0e600dd8b |
+36
-23
@@ -44,30 +44,43 @@ func NewDictSetContextV(flags DictSetFlag, varStores []map[string]string) *DictS
|
|||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DictSetContext) GetVar(name string) (value string, exists bool) {
|
func (ctx *DictSetContext) GetVar(name string) (value string, exists bool) {
|
||||||
if value, exists = self.localStore[name]; !exists {
|
if value, exists = ctx.localStore[name]; !exists {
|
||||||
value, exists = findInStores(name, self.varStores)
|
value, exists = ctx.findInStores(name)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DictSetContext) SetVar(name string, value string) (repeatedValue string) {
|
func (ctx *DictSetContext) SetVar(name string, value string) (repeatedValue string) {
|
||||||
self.localStore[name] = value
|
ctx.localStore[name] = value
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
func findInStores(key string, stores []map[string]string) (value string, found bool) {
|
func (ctx *DictSetContext) findInStores(key string) (value string, found bool) {
|
||||||
for _, vars := range stores {
|
if value, found = ctx.localStore[key]; !found {
|
||||||
if vars != nil {
|
for _, vars := range ctx.varStores {
|
||||||
if value, found = vars[key]; found {
|
if vars != nil {
|
||||||
break
|
if value, found = vars[key]; found {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value string, err error) {
|
// func findInStores(key string, stores []map[string]string) (value string, found bool) {
|
||||||
|
// for _, vars := range stores {
|
||||||
|
// if vars != nil {
|
||||||
|
// if value, found = vars[key]; found {
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (ctx *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value string, err error) {
|
||||||
var found bool
|
var found bool
|
||||||
var rawValue string
|
var rawValue string
|
||||||
|
|
||||||
@@ -81,30 +94,30 @@ func (self *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value st
|
|||||||
rawValue = name
|
rawValue = name
|
||||||
found = true
|
found = true
|
||||||
} else {
|
} else {
|
||||||
rawValue, found = findInStores(name, self.varStores)
|
rawValue, found = ctx.findInStores(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.SetVar(pipeNameKey, name) // NOTE Maybe this line can be deleted
|
ctx.SetVar(pipeNameKey, name) // NOTE Maybe this line can be deleted
|
||||||
|
|
||||||
origVarFound := found
|
origVarFound := found
|
||||||
for _, part := range parts {
|
for _, part := range parts {
|
||||||
var expandedPart string
|
var expandedPart string
|
||||||
self.varStores[0][pipeValueKey] = rawValue
|
// ctx.varStores[0][pipeValueKey] = rawValue
|
||||||
self.SetVar(pipeValueKey, rawValue)
|
ctx.SetVar(pipeValueKey, rawValue)
|
||||||
if expandedPart, err = Scan(self, part); err != nil {
|
if expandedPart, err = Scan(ctx, part); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
self.SetVar(pipeNameKey, name)
|
ctx.SetVar(pipeNameKey, name)
|
||||||
if rawValue, err = self.evalContent(origVarFound, rawValue, expandedPart); err != nil {
|
if rawValue, err = ctx.evalContent(origVarFound, rawValue, expandedPart); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
origVarFound = true
|
origVarFound = true
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(rawValue) == 0 && (self.flags&KeepVar) != 0 {
|
if len(rawValue) == 0 && (ctx.flags&KeepVar) != 0 {
|
||||||
value = Var(name)
|
value = Var(name)
|
||||||
} else {
|
} else {
|
||||||
value, err = Scan(self, rawValue)
|
value, err = Scan(ctx, rawValue)
|
||||||
//value = rawValue
|
//value = rawValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,7 +129,7 @@ func (self *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value st
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DictSetContext) evalContent(varFound bool, rawValue string, alternateValue string) (value string, err error) {
|
func (ctx *DictSetContext) evalContent(varFound bool, rawValue string, alternateValue string) (value string, err error) {
|
||||||
var args []string
|
var args []string
|
||||||
if args, err = sq.Split(alternateValue); err != nil {
|
if args, err = sq.Split(alternateValue); err != nil {
|
||||||
return
|
return
|
||||||
@@ -127,8 +140,8 @@ func (self *DictSetContext) evalContent(varFound bool, rawValue string, alternat
|
|||||||
op := args[0]
|
op := args[0]
|
||||||
if strings.HasPrefix(op, "__") {
|
if strings.HasPrefix(op, "__") {
|
||||||
var f ExpanderFunc
|
var f ExpanderFunc
|
||||||
if f, err = self.GetFunc(op); err == nil {
|
if f, err = ctx.GetFunc(op); err == nil {
|
||||||
value, err = f(self, rawValue, args[1:])
|
value, err = f(ctx, rawValue, args[1:])
|
||||||
}
|
}
|
||||||
} else if varFound {
|
} else if varFound {
|
||||||
value = rawValue
|
value = rawValue
|
||||||
|
|||||||
@@ -33,13 +33,17 @@ func TestNewDictSetContext(t *testing.T) {
|
|||||||
err = fmt.Errorf("unsupported '%%%c' time specifier at %d", ch, i)
|
err = fmt.Errorf("unsupported '%%%c' time specifier at %d", ch, i)
|
||||||
*/
|
*/
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
yday := now.Add(-24*time.Hour)
|
yday := now.Add(-24 * time.Hour)
|
||||||
inputs := []inputType{
|
inputs := []inputType{
|
||||||
|
{ctx1, "${|__default ' abc cba '|__trim}", "abc cba", nil},
|
||||||
|
{ctx1, "${|__default two|__map ':' 'one:1' 'two:2' 'six:6'}", "2", nil},
|
||||||
|
{ctx1, "${|__default two|__map ':' 'one:1' 'TWO:2' 'six:6'}", "", nil},
|
||||||
|
{ctx1, "${|__default two|__map-nc ':' 'one:1' 'TWO:2' 'six:6'}", "2", nil},
|
||||||
{ctx2, `form: ${|__time-fmt now ""}`, fmt.Sprintf("form: %d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()), nil},
|
{ctx2, `form: ${|__time-fmt now ""}`, fmt.Sprintf("form: %d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()), nil},
|
||||||
{ctx2, `form: ${|__time-fmt now "%-D"}`, fmt.Sprintf("form: %d-%d-%d", now.Year(), now.Month(), now.Day()), nil},
|
{ctx2, `form: ${|__time-fmt now "%-D"}`, fmt.Sprintf("form: %d-%d-%d", now.Year(), now.Month(), now.Day()), nil},
|
||||||
{ctx2, `form: ${|__time-fmt now "%:T"}`, fmt.Sprintf("form: %d:%d:%d", now.Hour(), now.Minute(), now.Second()), nil},
|
{ctx2, `form: ${|__time-fmt now "%:T"}`, fmt.Sprintf("form: %d:%d:%d", now.Hour(), now.Minute(), now.Second()), nil},
|
||||||
{ctx2, `form: ${|__time-fmt now "%02:T"}`,"", errors.New("invalid time specifier format at 3")},
|
{ctx2, `form: ${|__time-fmt now "%02:T"}`, "", errors.New("invalid time specifier format at 3")},
|
||||||
{ctx2, `form: ${|__time-fmt ""}`,"", errors.New("time value is empty")},
|
{ctx2, `form: ${|__time-fmt ""}`, "", errors.New("time value is empty")},
|
||||||
{ctx2, `form: ${|__time-fmt yesterday "%-D"}`, fmt.Sprintf("form: %d-%d-%d", yday.Year(), yday.Month(), yday.Day()), nil},
|
{ctx2, `form: ${|__time-fmt yesterday "%-D"}`, fmt.Sprintf("form: %d-%d-%d", yday.Year(), yday.Month(), yday.Day()), nil},
|
||||||
{ctx1, "${one|pippo}", "1", nil},
|
{ctx1, "${one|pippo}", "1", nil},
|
||||||
{ctx1, "${one|__default pluto}", "1", nil},
|
{ctx1, "${one|__default pluto}", "1", nil},
|
||||||
@@ -86,7 +90,6 @@ func TestNewDictSetContext(t *testing.T) {
|
|||||||
if (gotErr != nil && input.wantErr == nil) || (gotErr == nil && input.wantErr != nil) || (gotErr.Error() != input.wantErr.Error()) {
|
if (gotErr != nil && input.wantErr == nil) || (gotErr == nil && input.wantErr != nil) || (gotErr.Error() != input.wantErr.Error()) {
|
||||||
t.Errorf("%d: %s(%q)/err = <%v>, want <%v>", i, funcName, input.source, gotErr, input.wantErr)
|
t.Errorf("%d: %s(%q)/err = <%v>, want <%v>", i, funcName, input.source, gotErr, input.wantErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,6 +84,40 @@ func ExFuncTrimSuffix(ctx ExpanderContext, varValue string, args []string) (resu
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExFuncTrim(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
|
||||||
|
result = strings.TrimSpace(varValue)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExFuncMap(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
|
||||||
|
sep := args[0]
|
||||||
|
for _, pairSpec := range args[1:] {
|
||||||
|
pair := strings.SplitN(pairSpec, sep, 2)
|
||||||
|
if pair[0] == varValue {
|
||||||
|
if len(pair) == 2 {
|
||||||
|
result = pair[1]
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExFuncMapNC(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
|
||||||
|
sep := args[0]
|
||||||
|
varValue = strings.ToLower(varValue)
|
||||||
|
for _, pairSpec := range args[1:] {
|
||||||
|
pair := strings.SplitN(pairSpec, sep, 2)
|
||||||
|
if strings.ToLower(pair[0]) == varValue {
|
||||||
|
if len(pair) == 2 {
|
||||||
|
result = pair[1]
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
|
func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
|
||||||
ctx.AddFunc("__default", ExFuncDefault, 0, -1, "Returns the flow value if not empty, else all args joined; first arg is used as separator.")
|
ctx.AddFunc("__default", ExFuncDefault, 0, -1, "Returns the flow value if not empty, else all args joined; first arg is used as separator.")
|
||||||
ctx.AddFunc("__get", ExFuncGet, 0, 2, "Returns the value of the specified variable (arg1). With no arguments it returns the value of the variable having the flow value as name (same as __get ${.}). Arg2 is the default value. ")
|
ctx.AddFunc("__get", ExFuncGet, 0, 2, "Returns the value of the specified variable (arg1). With no arguments it returns the value of the variable having the flow value as name (same as __get ${.}). Arg2 is the default value. ")
|
||||||
@@ -91,6 +125,9 @@ func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
|
|||||||
ctx.AddFunc("__join", ExFuncJoin, 1, -1, "Joins its args starting from the second. The first one (arg1) is the separator")
|
ctx.AddFunc("__join", ExFuncJoin, 1, -1, "Joins its args starting from the second. The first one (arg1) is the separator")
|
||||||
ctx.AddFunc("__lower", ExFuncLower, 0, 0, "Changes the flow value to lower case.")
|
ctx.AddFunc("__lower", ExFuncLower, 0, 0, "Changes the flow value to lower case.")
|
||||||
ctx.AddFunc("__upper", ExFuncUpper, 0, 0, "Changes the flow value to upper case.")
|
ctx.AddFunc("__upper", ExFuncUpper, 0, 0, "Changes the flow value to upper case.")
|
||||||
|
ctx.AddFunc("__trim", ExFuncTrim, 0, 0, "Removes blanks on the left and on the right of the flow.")
|
||||||
ctx.AddFunc("__trim-suffix", ExFuncTrimSuffix, 1, 1, "Removes the specified suffix (arg1) from the flow.")
|
ctx.AddFunc("__trim-suffix", ExFuncTrimSuffix, 1, 1, "Removes the specified suffix (arg1) from the flow.")
|
||||||
|
ctx.AddFunc("__map", ExFuncMap, 1, 1, "Returns the value mapped by the flow value as a key; e.g. two __map : 'one:1' 'two:2' 'six:6' returns 2")
|
||||||
|
ctx.AddFunc("__map-nc", ExFuncMapNC, 1, 1, "Same as __map but this comapares keys ignoring case differencies; e.g. two __map-nc : 'ONE:1' 'Two:2' 'sIX:6' returns 2")
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -128,7 +128,7 @@ func (self *BaseExpander) AddFunc(name string, f ExpanderFunc, minArgs, maxArgs
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *BaseExpander) Handle(varSpec string, flags ScannerFlag) (value string, err error) {
|
func (self *BaseExpander) Handle(varSpec string, flags ScannerFlag) (value string, err error) {
|
||||||
err = errors.New("maybe you did somwthing wrong")
|
err = errors.New("maybe you did something wrong")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,4 +140,3 @@ func (self *BaseExpander) IterateFunc(op func(funcInfo *ExpanderFuncBlock) (err
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -17,12 +17,11 @@ const (
|
|||||||
EncryptValue
|
EncryptValue
|
||||||
)
|
)
|
||||||
|
|
||||||
// Flags symbols in the abova iota order
|
// Flags symbols in the above iota order
|
||||||
const flagSymbols = "!=*"
|
const flagSymbols = "!=*"
|
||||||
|
|
||||||
const defaultVarIntro = '$'
|
const defaultVarIntro = '$'
|
||||||
|
|
||||||
|
|
||||||
type VariableHandler interface {
|
type VariableHandler interface {
|
||||||
Handle(varSpec string, flags ScannerFlag) (value string, err error)
|
Handle(varSpec string, flags ScannerFlag) (value string, err error)
|
||||||
}
|
}
|
||||||
@@ -31,7 +30,7 @@ func Scan(handler VariableHandler, source string) (result string, err error) {
|
|||||||
return ScanExt(defaultVarIntro, handler, source)
|
return ScanExt(defaultVarIntro, handler, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScanExt(varIntro byte, handler VariableHandler, source string) (result string, err error) {
|
func ScanExt(varIntro byte, handler VariableHandler, source string) (result string, err error) {
|
||||||
var spec, value string
|
var spec, value string
|
||||||
var flags ScannerFlag
|
var flags ScannerFlag
|
||||||
var backSlash bool
|
var backSlash bool
|
||||||
@@ -123,4 +122,3 @@ func getVarSpec(source string, startOffset int) (spec string, flags ScannerFlag,
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user