-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/approvals/go-approval-tests | ||
|
||
go 1.12 | ||
|
||
require github.com/bitly/go-simplejson v0.5.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y= | ||
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package approvals | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/bitly/go-simplejson" | ||
) | ||
|
||
// converts received JSON string into a simpleJSON struct, scrubs the path with passed "scrubVal" using a separate util function, turns it into a string again | ||
// (pretty naive implementation) | ||
func scrubJSONPath(jsonStr string, path string, scrubVal interface{}) string { | ||
js, err := simplejson.NewJson([]byte(jsonStr)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
scrubJSONPathSimpleJSON(js, path, scrubVal) | ||
|
||
resS, err := js.EncodePretty() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(resS) | ||
} | ||
|
||
// limited implementation of JSONPath for scrubbing values in simplejson structs: | ||
// | ||
// ScrubJSONPath(data, "foo.bar", "<bar_scrubbed>") | ||
// { foo: { bar: "123" } } -> { foo: { bar: "<bar_scrubbed>" } | ||
// | ||
// - ScrubJSONPath(data, "foo[*].bar", "<bar_arr_scrubbed>") | ||
// { foo: [{ bar: "123" }, { bar: "234" }] } -> { foo: [{ bar: "<bar_arr_scrubbed>" }, { bar: "<bar_arr_scrubbed>" }]) } | ||
func scrubJSONPathSimpleJSON(data *simplejson.Json, path string, scrubVal interface{}) { | ||
// "foo[*].bar" -> ["foo", "[*]", "bar"] | ||
var elems []string | ||
for _, elDot := range strings.Split(path, ".") { | ||
if elDot == "[*]" { | ||
elems = append(elems, "[*]") | ||
} else if strings.HasSuffix(elDot, "[*]") { | ||
elems = append(elems, strings.TrimSuffix(elDot, "[*]")) | ||
elems = append(elems, "[*]") | ||
} else { | ||
elems = append(elems, elDot) | ||
} | ||
} | ||
|
||
// end of recursive iteration, set the value (if key is present) and return: | ||
if len(elems) == 1 { | ||
if _, found := data.CheckGet(elems[0]); found { | ||
data.Set(elems[0], scrubVal) | ||
} | ||
return | ||
} | ||
|
||
leftPath := path | ||
for _, el := range elems { | ||
leftPath = strings.TrimPrefix(leftPath, el) | ||
leftPath = strings.TrimPrefix(leftPath, ".") | ||
|
||
kjkj // special key for array object keys: | ||
if el == "[*]" { | ||
for i := 0; i < len(data.MustArray()); i++ { | ||
scrubJSONPathSimpleJSON(data.GetIndex(i), leftPath, scrubVal) | ||
} | ||
} else { | ||
if _, found := data.CheckGet(el); found { | ||
scrubJSONPathSimpleJSON(data.Get(el), leftPath, scrubVal) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
testdata/scrubber_test.TestVerifyJSONBytesWithJSONPathScrubber.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"greeting": "Hi!!!!", | ||
"list": [ | ||
{ | ||
"greeting": "Why hello!!!" | ||
}, | ||
{ | ||
"greeting": "Why hello!!!" | ||
} | ||
] | ||
} |