From 83b5bb128f7b2020246625923287d369868a9b4d Mon Sep 17 00:00:00 2001 From: fefit Date: Tue, 21 Mar 2023 14:08:31 +0800 Subject: [PATCH] fix: fix no INJECT_INDEX after the empty,isset methods --- fet.go | 2 +- fet_test.go | 6 ++++++ go.mod | 2 ++ go.sum | 34 ++++++++++++++++++++++++++++++ lib/generator/generator.go | 15 ++++++++----- tests/smarty/templates/compare.tpl | 1 + utils/utils.go | 8 +++++++ 7 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 tests/smarty/templates/compare.tpl diff --git a/fet.go b/fet.go index af6c391..24dfcad 100644 --- a/fet.go +++ b/fet.go @@ -671,7 +671,7 @@ func (node *Node) Compile(options *CompileOptions) (result string, err error) { result = "{{" + capVar + " := (INJECT_CAPTURE_SCOPE . " for _, varName := range currentScopes { varName = strings.TrimPrefix(varName, "$") - result += "\"" + strings.Title(varName) + "\" $" + varName + result += "\"" + utils.Ucase(varName) + "\" $" + varName } result += ")}}" (*captures)[keyName] = capVar diff --git a/fet_test.go b/fet_test.go index 73ffaea..7ee5b8f 100644 --- a/fet_test.go +++ b/fet_test.go @@ -75,6 +75,12 @@ func TestCompile(t *testing.T) { assertOutputToBe(t, "condition.tpl", map[string]int{ "Number": -1, }, "smaller") + // compare + assertOutputToBe(t, "compare.tpl", map[string]interface{}{ + "Result": map[string]int{ + "Errno": 100, + }, + }, helloFet) }) } diff --git a/go.mod b/go.mod index b345b5b..65c0e2d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,8 @@ module github.com/fefit/fet go 1.14 require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fefit/dateutil v0.0.3 github.com/stretchr/testify v1.7.0 + golang.org/x/text v0.8.0 // indirect ) diff --git a/go.sum b/go.sum index 56d5262..b5fa0f1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fefit/dateutil v0.0.3 h1:ew0U2YeJIUJIpD0uHVDdbkEdC47pmKtyKjKhpR3FUk4= github.com/fefit/dateutil v0.0.3/go.mod h1:wIh6p89zkfpj4nvyyPx7U6AwQuRHJAAFAjgZ4aC5R3E= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -7,6 +9,38 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/lib/generator/generator.go b/lib/generator/generator.go index 9010615..79c37f9 100644 --- a/lib/generator/generator.go +++ b/lib/generator/generator.go @@ -156,7 +156,7 @@ func (gen *Generator) parseIdentifier(options *GenOptions, parseOptions *ParseOp if isInCapture { str.WriteString("$.Variables.") varName := strings.TrimPrefix(name, "$") - str.WriteString(strings.Title(varName)) + str.WriteString(utils.Ucase(varName)) } else { str.WriteString(name) } @@ -183,7 +183,7 @@ func (gen *Generator) parseIdentifier(options *GenOptions, parseOptions *ParseOp str.WriteString("$") } if conf.Ucfirst { - name = strings.Title(name) + name = utils.Ucase(name) } } str.WriteString(name) @@ -351,7 +351,7 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio if t, ok := token.(*e.StringToken); ok { prop := string(t.Stat.Values) if conf.Ucfirst { - str.WriteString(strings.Title(prop)) + str.WriteString(utils.Ucase(prop)) } else { str.WriteString(prop) } @@ -362,7 +362,7 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio ident := string(t.Stat.Values) if cur.Operator == "." { if conf.Ucfirst { - ident = strings.Title(ident) + ident = utils.Ucase(ident) } str.WriteString("\"") str.WriteString(ident) @@ -386,7 +386,8 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio } if !noObjectIndex && !isStatic { str.WriteString(")") - } else { + } + if !noObjectIndex { parseOptions.NoObjectIndex = false } } else if curType == "function" { @@ -421,6 +422,10 @@ func (gen *Generator) parseRecursive(node *Node, options *GenOptions, parseOptio } } str.WriteString(")") + // reset no object index + if parseOptions.NoObjectIndex { + parseOptions.NoObjectIndex = false + } } else { op := node.Operator if name, ok := operatorFnNames[op]; ok { diff --git a/tests/smarty/templates/compare.tpl b/tests/smarty/templates/compare.tpl new file mode 100644 index 0000000..0bc1e44 --- /dev/null +++ b/tests/smarty/templates/compare.tpl @@ -0,0 +1 @@ +{%if !empty($result.errno) && $result.errno == 100%}hello fet!{%/if%} \ No newline at end of file diff --git a/utils/utils.go b/utils/utils.go index 6eb4362..6446000 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,6 +2,8 @@ package utils import ( "github.com/fefit/fet/types" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) // IsIdentifier judge @@ -40,3 +42,9 @@ func IsEnLetter(s rune) bool { func IsArabicNumber(s rune) bool { return s >= 48 && s <= 57 } + +// Ucase +func Ucase(str string) string { + caser := cases.Title(language.English) + return caser.String(str) +}