Repository: vanus-labs/vanus
Pull request #644 (2023-11-05 05:37:22)

fix: json template render bad empty array and empty object
### What problem does this PR solve?

JSON template renders bad empty array and empty object

### Problem Summary

- missing '[' when renders empty array
- missing '{' when renders empty object

### What has changed and how does it work?

### Check List

<!-- At least one of them must be included. -->

#### Tests

- [x] Unit test
- [ ] Integration test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No code


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) Report
> Merging [#644](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) (b8dfc8d) into [main](https://app.codecov.io/gh/vanus-labs/vanus/commit/ee76217182ba60efbcbb62fd374b8e75127d5133?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) (ee76217) will **decrease** coverage by `5.74%`.
> Report is 106 commits behind head on main.
> The diff coverage is `3.63%`.

[![Impacted file tree graph](https://app.codecov.io/gh/vanus-labs/vanus/pull/644/graphs/tree.svg?width=650&height=150&src=pr&token=RSXSIMEY4V&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)

```diff
@@            Coverage Diff             @@
##             main     #644      +/-   ##
==========================================
- Coverage   57.07%   51.33%   -5.74%     
==========================================
  Files         190      260      +70     
  Lines       15191    19566    +4375     
==========================================
+ Hits         8670    10045    +1375     
- Misses       5884     8703    +2819     
- Partials      637      818     +181     
```

| [Flag](https://app.codecov.io/gh/vanus-labs/vanus/pull/644/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/vanus-labs/vanus/pull/644/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | `51.33% <3.63%> (?)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | Coverage Δ | |
|---|---|---|
| [api/errors/app.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-YXBpL2Vycm9ycy9hcHAuZ28=) | `30.90% <ø> (ø)` | |
| [lib/bytes/clone\_go119.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL2Nsb25lX2dvMTE5Lmdv) | `0.00% <ø> (ø)` | |
| [lib/bytes/escape.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL2VzY2FwZS5nbw==) | `54.46% <ø> (ø)` | |
| [lib/bytes/io.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL2lvLmdv) | `0.00% <ø> (ø)` | |
| [lib/bytes/parse.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL3BhcnNlLmdv) | `7.14% <ø> (ø)` | |
| [lib/bytes/unsafe.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL3Vuc2FmZS5nbw==) | `100.00% <ø> (ø)` | |
| [lib/bytes/utf8.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2J5dGVzL3V0ZjguZ28=) | `11.11% <ø> (ø)` | |
| [lib/json/path/parse.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9wYXJzZS5nbw==) | `49.41% <ø> (ø)` | |
| [lib/json/path/path.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9wYXRoLmdv) | `0.00% <ø> (ø)` | |
| [lib/json/path/segment.go](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs#diff-bGliL2pzb24vcGF0aC9zZWdtZW50Lmdv) | `0.00% <ø> (ø)` | |
| ... and [31 more](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs) | |

... and [393 files with indirect coverage changes](https://app.codecov.io/gh/vanus-labs/vanus/pull/644/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)

------

[Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs). Last update [3267c49...b8dfc8d](https://app.codecov.io/gh/vanus-labs/vanus/pull/644?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=vanus-labs).


Patch:
diff --git a/pkg/template/json/generator.go b/pkg/template/json/generator.go
index ee5428e..3cc7f49 100644
--- a/pkg/template/json/generator.go
+++ b/pkg/template/json/generator.go
@@ -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:
@@ -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:


Test patch:
diff --git a/pkg/template/json/template_test.go b/pkg/template/json/template_test.go
index 5c14c68..8cfc302 100644
--- a/pkg/template/json/template_test.go
+++ b/pkg/template/json/template_test.go
@@ -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{}{}