Skip to content

Commit

Permalink
fix: json template render bad empty array and empty object (#644)
Browse files Browse the repository at this point in the history
Signed-off-by: James Yean <[email protected]>
  • Loading branch information
ifplusor authored Nov 5, 2023
1 parent 3267c49 commit 00d9a94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pkg/template/json/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ func (g *templateGenerator) generate(root templateNode) []templateSegment {
}

func (g *templateGenerator) generateObjectNode(node *objectNode, i int) bool {
if i == 0 {
g.buf.WriteByte('{')
}

if i >= len(node.members) {
g.buf.WriteByte('}')
return false
}

if i == 0 {
g.buf.WriteByte('{')
}

mn := node.members[i]
switch n := mn.value.(type) {
case *jsonPathNode:
Expand All @@ -114,15 +114,15 @@ func (g *templateGenerator) generateObjectNode(node *objectNode, i int) bool {
}

func (g *templateGenerator) generateArrayNode(node *arrayNode, i int) bool {
if i == 0 {
g.buf.WriteByte('[')
}

if i >= len(node.elements) {
g.buf.WriteByte(']')
return false
}

if i == 0 {
g.buf.WriteByte('[')
}

en := node.elements[i]
switch n := en.(type) {
case *jsonPathNode:
Expand Down
12 changes: 12 additions & 0 deletions pkg/template/json/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ import (
)

func TestTemplate_Execute(t *testing.T) {
Convey("empty array and empty object", t, func() {
model := map[string]interface{}{}
variables := map[string]interface{}{}

tp, err := Compile(`[{"empty array":[],"empty object":{}}]`)
So(err, ShouldBeNil)

v, err := tp.Execute(model, variables)
So(err, ShouldBeNil)
So(string(v), ShouldEqual, `[{"empty array":[],"empty object":{}}]`)
})

Convey("refer variables", t, func() {
model := map[string]interface{}{}
variables := map[string]interface{}{}
Expand Down

0 comments on commit 00d9a94

Please sign in to comment.