Skip to content

Commit

Permalink
Merge pull request #17 from spekary/rewrite
Browse files Browse the repository at this point in the history
Fixing go error tag and root tag
  • Loading branch information
spekary authored Feb 2, 2022
2 parents 68142df + 8ae798b commit 5d5c567
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 7 deletions.
8 changes: 8 additions & 0 deletions internal/got/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func (a *astWalker) walk(item tokenItem) error {
case itemBytes:
return a.outputValue(item)

case itemGoErr:
return a.outputGoErr(item)

case itemIf:
return a.outputIf(item)

Expand Down Expand Up @@ -240,6 +243,11 @@ func (a *astWalker) outputValue(item tokenItem) (err error) {
return
}

func (a *astWalker) outputGoErr(item tokenItem) (err error) {
_,err = fmt.Fprintf(a.w, "\nif err = %s; err != nil {return}\n", item.val)
return
}

// Generally speaking, text is quoted with a backtick character. However, there is a special case. If the text actually
// contains a backtick character, we cannot use backticks to quote them, but rather double-quotes. This function prepares
// text, looking for these backticks, and then returns a golang quoted text that can be suitably used in all situations.
Expand Down
3 changes: 2 additions & 1 deletion internal/got/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func (p *parser) parseRun() (subItems []tokenItem, endItem tokenItem) {
case itemUInt: fallthrough
case itemFloat: fallthrough
case itemInterface: fallthrough
case itemBytes:
case itemBytes: fallthrough
case itemGoErr:
item2 := p.parseValue(item)
if item2.typ == itemError {
endItem = item2
Expand Down
8 changes: 8 additions & 0 deletions internal/got/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func Test_parse(t *testing.T) {
assert.Equal(t, 1, len(item.childItems))
assert.Equal(t, itemRun, item.childItems[0].typ)
})

t.Run("go with error", func(t *testing.T) {
item := parseContent("{{e abc }}")
assert.Equal(t, itemGoErr, item.childItems[0].typ)
assert.Equal(t, "abc", item.childItems[0].val)
assert.Equal(t, true, item.childItems[0].withError)
})

t.Run("text run", func(t *testing.T) {
item := parseContent("{{ abc }}")
assert.Equal(t, itemText, item.childItems[0].typ)
Expand Down
18 changes: 16 additions & 2 deletions internal/got/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,29 @@ func Run(outDir string,

// Default named block values
file,_ = filepath.Abs(file)
root := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
for {
ext := filepath.Ext(root)
if ext == "" {break}
root = strings.TrimSuffix(root, ext)
}

namedBlocks["templatePath"] = namedBlockEntry{file, 0, locationRef{}}
namedBlocks["templateName"] = namedBlockEntry{filepath.Base(file), 0, locationRef{}}
namedBlocks["templateRoot"] = namedBlockEntry{strings.TrimSuffix(filepath.Base(file), filepath.Ext(file)), 0, locationRef{}}
namedBlocks["templateRoot"] = namedBlockEntry{root, 0, locationRef{}}
namedBlocks["templateParent"] = namedBlockEntry{filepath.Base(filepath.Dir(file)), 0, locationRef{}}

newPath,_ = filepath.Abs(newPath)
root = strings.TrimSuffix(filepath.Base(newPath), filepath.Ext(newPath))
for {
ext := filepath.Ext(root)
if ext == "" {break}
root = strings.TrimSuffix(root, ext)
}

namedBlocks["outPath"] = namedBlockEntry{newPath, 0, locationRef{}}
namedBlocks["outName"] = namedBlockEntry{filepath.Base(newPath), 0, locationRef{}}
namedBlocks["outRoot"] = namedBlockEntry{strings.TrimSuffix(filepath.Base(newPath), filepath.Ext(newPath)), 0, locationRef{}}
namedBlocks["outRoot"] = namedBlockEntry{root, 0, locationRef{}}
namedBlocks["outParent"] = namedBlockEntry{filepath.Base(filepath.Dir(newPath)), 0, locationRef{}}

var a astType
Expand Down
5 changes: 3 additions & 2 deletions internal/got/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (

itemEnd
itemGo
itemGoErr
itemText

itemRun // A run of text
Expand Down Expand Up @@ -137,8 +138,8 @@ func init() {
// go code straight pass through
tokens["{{g"] = tokenItem{typ: itemGo}
tokens["{{go"] = tokenItem{typ: itemGo}
tokens["{{e"] = tokenItem{typ: itemGo, withError: true}
tokens["{{err"] = tokenItem{typ: itemGo, withError: true}
tokens["{{e"] = tokenItem{typ: itemGoErr, withError: true}
tokens["{{err"] = tokenItem{typ: itemGoErr, withError: true}

tokens["{{begin"] = tokenItem{typ: itemStrictBlock}
tokens["{{define"] = tokenItem{typ: itemNamedBlock} // must follow with a name and a close tag
Expand Down
4 changes: 2 additions & 2 deletions test/expected/TestPredefines.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
testPredefines.tpl.got
testPredefines.tpl
testPredefines
src
testPredefines.tpl.go
testPredefines.tpl
testPredefines
template
4 changes: 4 additions & 0 deletions test/expected/TestVarsWithErr.out
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ Evaluates to a string
Here is the number: -5
And another: Me with no escaping
And a float: 6.400000095367432
Bytes: some bytes
Boolean: true

Just error: 2
20 changes: 20 additions & 0 deletions test/src/testVarsWithErr.tpl.got
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
Here is the number: {{ie givesIntWithErr() }}
And another: {{se givesStringWithErr() }}
And a float: {{fe givesFloatWithErr() }}
Bytes: {{we givesBytesWithErr() }}
Boolean: {{be givesBoolWithErr() }}

{{g a := 1}}
Just error: {{e justGivesErr(&a) }} {{i a}}
}}

{{# Here is a comment }}
Expand All @@ -29,5 +34,20 @@ func givesFloatWithErr() (float32, error) {
return 6.4, nil
}

func givesBytesWithErr() ([]byte, error) {
return []byte("some bytes"), nil
}

func givesBoolWithErr() (bool, error) {
return true, nil
}

func justGivesErr(a *int) (err error) {
*a = 2
return nil
}





0 comments on commit 5d5c567

Please sign in to comment.