mirror of
https://github.com/getsops/sops.git
synced 2026-02-05 12:45:21 +01:00
Handle escaping in original json (#357)
* Handle escaping in original json * Replace conditional magic with proper json encoding call for key * swap TestDecodeJSONWithEscaping with new TestEncodeJSONWithEscaping * fix copy/paste typo
This commit is contained in:
@@ -168,7 +168,11 @@ func (store Store) encodeTree(tree sops.TreeBranch) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error encoding value %s: %s", v, err)
|
||||
}
|
||||
out += `"` + item.Key.(string) + `": ` + string(v)
|
||||
k, err := json.Marshal(item.Key.(string))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error encoding key %s: %s", k, err)
|
||||
}
|
||||
out += string(k) + `: ` + string(v)
|
||||
if i != len(tree)-1 {
|
||||
out += ","
|
||||
}
|
||||
|
||||
@@ -219,6 +219,27 @@ func TestEncodeSimpleJSON(t *testing.T) {
|
||||
assert.Equal(t, expected, branch)
|
||||
}
|
||||
|
||||
func TestEncodeJSONWithEscaping(t *testing.T) {
|
||||
branch := sops.TreeBranch{
|
||||
sops.TreeItem{
|
||||
Key: "foo\\bar",
|
||||
Value: "value",
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: "a_key_with\"quotes\"",
|
||||
Value: 4.0,
|
||||
},
|
||||
sops.TreeItem{
|
||||
Key: "baz\\\\foo",
|
||||
Value: 2.0,
|
||||
},
|
||||
}
|
||||
out, err := Store{}.jsonFromTreeBranch(branch)
|
||||
assert.Nil(t, err)
|
||||
expected, _ := Store{}.treeBranchFromJSON(out)
|
||||
assert.Equal(t, expected, branch)
|
||||
}
|
||||
|
||||
func TestEncodeJSONArrayOfObjects(t *testing.T) {
|
||||
branch := sops.TreeBranch{
|
||||
sops.TreeItem{
|
||||
|
||||
Reference in New Issue
Block a user