Compare commits

..

No commits in common. "857155274e260692095d646eaa7c80d873873db4" and "c480e81c0a63a494abbd4d13a6f5f8b8e800d817" have entirely different histories.

4 changed files with 33 additions and 42 deletions

View File

@ -44,43 +44,30 @@ func NewDictSetContextV(flags DictSetFlag, varStores []map[string]string) *DictS
return ctx
}
func (ctx *DictSetContext) GetVar(name string) (value string, exists bool) {
if value, exists = ctx.localStore[name]; !exists {
value, exists = ctx.findInStores(name)
func (self *DictSetContext) GetVar(name string) (value string, exists bool) {
if value, exists = self.localStore[name]; !exists {
value, exists = findInStores(name, self.varStores)
}
return
}
func (ctx *DictSetContext) SetVar(name string, value string) (repeatedValue string) {
ctx.localStore[name] = value
func (self *DictSetContext) SetVar(name string, value string) (repeatedValue string) {
self.localStore[name] = value
return value
}
func (ctx *DictSetContext) findInStores(key string) (value string, found bool) {
if value, found = ctx.localStore[key]; !found {
for _, vars := range ctx.varStores {
if vars != nil {
if value, found = vars[key]; found {
break
}
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 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) {
func (self *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value string, err error) {
var found bool
var rawValue string
@ -94,30 +81,30 @@ func (ctx *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value str
rawValue = name
found = true
} else {
rawValue, found = ctx.findInStores(name)
rawValue, found = findInStores(name, self.varStores)
}
}
ctx.SetVar(pipeNameKey, name) // NOTE Maybe this line can be deleted
self.SetVar(pipeNameKey, name) // NOTE Maybe this line can be deleted
origVarFound := found
for _, part := range parts {
var expandedPart string
ctx.varStores[0][pipeValueKey] = rawValue
ctx.SetVar(pipeValueKey, rawValue)
if expandedPart, err = Scan(ctx, part); err != nil {
self.varStores[0][pipeValueKey] = rawValue
self.SetVar(pipeValueKey, rawValue)
if expandedPart, err = Scan(self, part); err != nil {
break
}
ctx.SetVar(pipeNameKey, name)
if rawValue, err = ctx.evalContent(origVarFound, rawValue, expandedPart); err != nil {
self.SetVar(pipeNameKey, name)
if rawValue, err = self.evalContent(origVarFound, rawValue, expandedPart); err != nil {
break
}
origVarFound = true
}
if err == nil {
if len(rawValue) == 0 && (ctx.flags&KeepVar) != 0 {
if len(rawValue) == 0 && (self.flags&KeepVar) != 0 {
value = Var(name)
} else {
value, err = Scan(ctx, rawValue)
value, err = Scan(self, rawValue)
//value = rawValue
}
}
@ -129,7 +116,7 @@ func (ctx *DictSetContext) Handle(spec string, scanFlags ScannerFlag) (value str
return
}
func (ctx *DictSetContext) evalContent(varFound bool, rawValue string, alternateValue string) (value string, err error) {
func (self *DictSetContext) evalContent(varFound bool, rawValue string, alternateValue string) (value string, err error) {
var args []string
if args, err = sq.Split(alternateValue); err != nil {
return
@ -140,8 +127,8 @@ func (ctx *DictSetContext) evalContent(varFound bool, rawValue string, alternate
op := args[0]
if strings.HasPrefix(op, "__") {
var f ExpanderFunc
if f, err = ctx.GetFunc(op); err == nil {
value, err = f(ctx, rawValue, args[1:])
if f, err = self.GetFunc(op); err == nil {
value, err = f(self, rawValue, args[1:])
}
} else if varFound {
value = rawValue

View File

@ -33,13 +33,13 @@ func TestNewDictSetContext(t *testing.T) {
err = fmt.Errorf("unsupported '%%%c' time specifier at %d", ch, i)
*/
now := time.Now()
yday := now.Add(-24 * time.Hour)
yday := now.Add(-24*time.Hour)
inputs := []inputType{
{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 "%: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 ""}`, "", errors.New("time value is empty")},
{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 yesterday "%-D"}`, fmt.Sprintf("form: %d-%d-%d", yday.Year(), yday.Month(), yday.Day()), nil},
{ctx1, "${one|pippo}", "1", nil},
{ctx1, "${one|__default pluto}", "1", nil},
@ -86,6 +86,7 @@ func TestNewDictSetContext(t *testing.T) {
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)
}
}
}

View File

@ -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) {
err = errors.New("maybe you did something wrong")
err = errors.New("maybe you did somwthing wrong")
return
}
@ -140,3 +140,4 @@ func (self *BaseExpander) IterateFunc(op func(funcInfo *ExpanderFuncBlock) (err
}
return
}

View File

@ -17,11 +17,12 @@ const (
EncryptValue
)
// Flags symbols in the above iota order
// Flags symbols in the abova iota order
const flagSymbols = "!=*"
const defaultVarIntro = '$'
type VariableHandler interface {
Handle(varSpec string, flags ScannerFlag) (value string, err error)
}
@ -30,7 +31,7 @@ func Scan(handler VariableHandler, source string) (result string, err error) {
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 flags ScannerFlag
var backSlash bool
@ -122,3 +123,4 @@ func getVarSpec(source string, startOffset int) (spec string, flags ScannerFlag,
}
return
}