Skip to content

Commit 05b10a6

Browse files
authored
Merge pull request #1647 from felixfontein/json-serialization
Correctly handle comments during JSON serialization
2 parents f76921b + 2463a6f commit 05b10a6

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

stores/json/store.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,35 @@ func (store Store) encodeValue(v interface{}) ([]byte, error) {
186186

187187
func (store Store) encodeArray(array []interface{}) ([]byte, error) {
188188
out := "["
189-
for i, item := range array {
189+
empty := true
190+
for _, item := range array {
190191
if _, ok := item.(sops.Comment); ok {
191192
continue
192193
}
194+
if !empty {
195+
out += ","
196+
}
193197
v, err := store.encodeValue(item)
194198
if err != nil {
195199
return nil, err
196200
}
197201
out += string(v)
198-
if i != len(array)-1 {
199-
out += ","
200-
}
202+
empty = false
201203
}
202204
out += "]"
203205
return []byte(out), nil
204206
}
205207

206208
func (store Store) encodeTree(tree sops.TreeBranch) ([]byte, error) {
207209
out := "{"
208-
for i, item := range tree {
210+
empty := true
211+
for _, item := range tree {
209212
if _, ok := item.Key.(sops.Comment); ok {
210213
continue
211214
}
215+
if !empty {
216+
out += ","
217+
}
212218
v, err := store.encodeValue(item.Value)
213219
if err != nil {
214220
return nil, fmt.Errorf("Error encoding value %s: %s", v, err)
@@ -218,9 +224,7 @@ func (store Store) encodeTree(tree sops.TreeBranch) ([]byte, error) {
218224
return nil, fmt.Errorf("Error encoding key %s: %s", k, err)
219225
}
220226
out += string(k) + `: ` + string(v)
221-
if i != len(tree)-1 {
222-
out += ","
223-
}
227+
empty = false
224228
}
225229
return []byte(out + "}"), nil
226230
}

stores/json/store_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,70 @@ func TestNoIndent(t *testing.T) {
540540
assert.Nil(t, err)
541541
assert.Equal(t, expected, string(out))
542542
}
543+
544+
func TestComments(t *testing.T) {
545+
tree := sops.Tree{
546+
Branches: sops.TreeBranches{
547+
sops.TreeBranch{
548+
sops.TreeItem{
549+
Key: "foo",
550+
Value: []interface{}{
551+
sops.Comment{Value: " comment 0"},
552+
sops.TreeBranch{
553+
sops.TreeItem{
554+
Key: sops.Comment{Value: " comment 1"},
555+
Value: nil,
556+
},
557+
sops.TreeItem{
558+
Key: "foo",
559+
Value: 3,
560+
},
561+
sops.TreeItem{
562+
Key: sops.Comment{Value: " comment 2"},
563+
Value: nil,
564+
},
565+
sops.TreeItem{
566+
Key: sops.Comment{Value: " comment 3"},
567+
Value: nil,
568+
},
569+
sops.TreeItem{
570+
Key: "bar",
571+
Value: false,
572+
},
573+
sops.TreeItem{
574+
Key: sops.Comment{Value: " comment 4"},
575+
Value: nil,
576+
},
577+
sops.TreeItem{
578+
Key: sops.Comment{Value: " comment 5"},
579+
Value: nil,
580+
},
581+
},
582+
sops.Comment{Value: " comment 6"},
583+
sops.Comment{Value: " comment 7"},
584+
2,
585+
sops.Comment{Value: " comment 8"},
586+
sops.Comment{Value: " comment 9"},
587+
},
588+
},
589+
},
590+
},
591+
}
592+
expected := `{
593+
"foo": [
594+
{
595+
"foo": 3,
596+
"bar": false
597+
},
598+
2
599+
]
600+
}`
601+
store := Store{
602+
config: config.JSONStoreConfig{
603+
Indent: 2,
604+
},
605+
}
606+
out, err := store.EmitPlainFile(tree.Branches)
607+
assert.Nil(t, err)
608+
assert.Equal(t, expected, string(out))
609+
}

0 commit comments

Comments
 (0)