Skip to content

Commit

Permalink
export type Selection
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Oct 18, 2021
1 parent 1802534 commit 515659f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ doc := `
`

result, _ := jsonmask.Mask([]byte(doc), "kind,items(title,characteristics/length)")
// OR:
// selection, err := jsonmask.Compile("kind,items(title,characteristics/length)")
// result, err := selection.Mask([]byte(doc))
fmt.Println(string(result))
// Output:
// {"items":[{"characteristics":{"length":"short"},"title":"First title"},{"characteristics":{"length":"long"},"title":"Second title"}],"kind":"demo"}
Expand Down
3 changes: 2 additions & 1 deletion debug/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func main() {
}
`

result, err := jsonmask.Mask([]byte(doc), "kind,items(title,characteristics/length)")
sl, err := jsonmask.Compile("kind,items(title,characteristics/length)")
result, err := sl.Mask([]byte(doc))

fmt.Println("json output: ", err, string(result))
// json output: {"a":"aaa","c":{"c1":12,"c2":33}}
Expand Down
9 changes: 7 additions & 2 deletions jsonmask.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ func Mask(doc []byte, fields string) ([]byte, error) {
if !json.Valid(doc) {
return nil, fmt.Errorf("invalid json string")
}
sl, err := compile(fields)
sl, err := Compile(fields)
if err != nil {
return nil, err
}

return sl.Mask(doc)
}

func (sl Selection) Mask(doc []byte) ([]byte, error) {
if len(doc) == 0 || len(sl) == 0 {
return doc, nil
}
Expand Down Expand Up @@ -198,7 +203,7 @@ func checkWhich(buf []byte) int {
return eOther
}

func copyLazyNode(dst, src *lazyNode, sl selection) error {
func copyLazyNode(dst, src *lazyNode, sl Selection) error {
err := src.unmarshal()
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"unicode/utf8"
)

type selection map[string]selection
type Selection map[string]Selection

// used for testing purposes
func (s selection) equal(other selection) bool {
func (s Selection) equal(other Selection) bool {
if other == nil {
return false
}
Expand All @@ -33,7 +33,7 @@ func (s selection) equal(other selection) bool {
// a/*/c the star * wildcard will select all items in a field
// a,b/c(d,e(f,g/h)),i
//
func compile(str string) (selection, error) {
func Compile(str string) (Selection, error) {
if !utf8.ValidString(str) {
return nil, fmt.Errorf("invalid fields")
}
Expand Down Expand Up @@ -101,17 +101,17 @@ func compile(str string) (selection, error) {
return nil, fmt.Errorf("invalid end")
}

node := make(selection)
node := make(Selection)
err := buildSelection(tokens, node)
return node, err
}

func buildSelection(tokens []string, root selection) error {
func buildSelection(tokens []string, root Selection) error {
if len(tokens) == 0 {
return nil
}

var child selection
var child Selection
node := root
for i := 0; i < len(tokens); i++ {
switch tokens[i] {
Expand All @@ -131,7 +131,7 @@ func buildSelection(tokens []string, root selection) error {
case ")":
return fmt.Errorf("invalid field char: ')'")
default:
child = make(selection)
child = make(Selection)
node[tokens[i]] = child
}
}
Expand Down
40 changes: 20 additions & 20 deletions selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "testing"
type selectionCase struct {
fields string
shouldErr bool
res selection
res Selection
}

// a select a field 'a'
Expand Down Expand Up @@ -82,62 +82,62 @@ var selectionCases = []selectionCase{
{
fields: "a",
shouldErr: false,
res: selection{"a": selection{}},
res: Selection{"a": Selection{}},
},
{
fields: "*",
shouldErr: false,
res: selection{"*": selection{}},
res: Selection{"*": Selection{}},
},
{
fields: "a,b,c",
shouldErr: false,
res: selection{
"a": selection{},
"b": selection{},
"c": selection{},
res: Selection{
"a": Selection{},
"b": Selection{},
"c": Selection{},
},
},
{
fields: "a/b/c",
shouldErr: false,
res: selection{"a": selection{"b": selection{"c": selection{}}}},
res: Selection{"a": Selection{"b": Selection{"c": Selection{}}}},
},
{
fields: "a(b,c)",
shouldErr: false,
res: selection{"a": selection{"b": selection{}, "c": selection{}}},
res: Selection{"a": Selection{"b": Selection{}, "c": Selection{}}},
},
{
fields: "a(b(c))",
shouldErr: false,
res: selection{"a": selection{"b": selection{"c": selection{}}}},
res: Selection{"a": Selection{"b": Selection{"c": Selection{}}}},
},
{
fields: "a/*/c",
shouldErr: false,
res: selection{"a": selection{"*": selection{"c": selection{}}}},
res: Selection{"a": Selection{"*": Selection{"c": Selection{}}}},
},
{
fields: "a,b/c(d,e(f,g/h)),i",
shouldErr: false,
res: selection{
"a": selection{},
"b": selection{"c": selection{
"d": selection{},
"e": selection{
"f": selection{},
"g": selection{"h": selection{}},
res: Selection{
"a": Selection{},
"b": Selection{"c": Selection{
"d": Selection{},
"e": Selection{
"f": Selection{},
"g": Selection{"h": Selection{}},
},
}},
"i": selection{},
"i": Selection{},
},
},
}

func TestCompile(t *testing.T) {
for _, c := range selectionCases {
res, err := compile(c.fields)
res, err := Compile(c.fields)
if c.shouldErr {
if err == nil {
t.Errorf("Testing case[%s] failed: should error but got: %#v", c.fields, res)
Expand Down

0 comments on commit 515659f

Please sign in to comment.