-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce ast.Path type #117
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ast | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
var _ json.Unmarshaler = (*Path)(nil) | ||
|
||
type Path []PathElement | ||
|
||
type PathElement interface { | ||
isPathElement() | ||
} | ||
|
||
var _ PathElement = PathIndex(0) | ||
var _ PathElement = PathName("") | ||
|
||
func (path Path) String() string { | ||
var str bytes.Buffer | ||
for i, v := range path { | ||
switch v := v.(type) { | ||
case PathIndex: | ||
str.WriteString(fmt.Sprintf("[%d]", v)) | ||
case PathName: | ||
if i != 0 { | ||
str.WriteByte('.') | ||
} | ||
str.WriteString(string(v)) | ||
default: | ||
panic(fmt.Sprintf("unknown type: %T", v)) | ||
} | ||
} | ||
return str.String() | ||
} | ||
|
||
func (path *Path) UnmarshalJSON(b []byte) error { | ||
var vs []interface{} | ||
err := json.Unmarshal(b, &vs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*path = make([]PathElement, 0, len(vs)) | ||
for _, v := range vs { | ||
switch v := v.(type) { | ||
case string: | ||
*path = append(*path, PathName(v)) | ||
case int: | ||
*path = append(*path, PathIndex(v)) | ||
case float64: | ||
*path = append(*path, PathIndex(int(v))) | ||
default: | ||
return fmt.Errorf("unknown path element type: %T", v) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type PathIndex int | ||
|
||
func (_ PathIndex) isPathElement() {} | ||
|
||
type PathName string | ||
|
||
func (_ PathName) isPathElement() {} |
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,96 @@ | ||
package ast | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPath_String(t *testing.T) { | ||
type Spec struct { | ||
Value Path | ||
Expected string | ||
} | ||
specs := []*Spec{ | ||
{ | ||
Value: Path{PathName("a"), PathIndex(2), PathName("c")}, | ||
Expected: "a[2].c", | ||
}, | ||
{ | ||
Value: Path{}, | ||
Expected: ``, | ||
}, | ||
{ | ||
Value: Path{PathIndex(1), PathName("b")}, | ||
Expected: `[1].b`, | ||
}, | ||
} | ||
|
||
for _, spec := range specs { | ||
t.Run(spec.Value.String(), func(t *testing.T) { | ||
require.Equal(t, spec.Expected, spec.Value.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestPath_MarshalJSON(t *testing.T) { | ||
type Spec struct { | ||
Value Path | ||
Expected string | ||
} | ||
specs := []*Spec{ | ||
{ | ||
Value: Path{PathName("a"), PathIndex(2), PathName("c")}, | ||
Expected: `["a",2,"c"]`, | ||
}, | ||
{ | ||
Value: Path{}, | ||
Expected: `[]`, | ||
}, | ||
{ | ||
Value: Path{PathIndex(1), PathName("b")}, | ||
Expected: `[1,"b"]`, | ||
}, | ||
} | ||
|
||
for _, spec := range specs { | ||
t.Run(spec.Value.String(), func(t *testing.T) { | ||
b, err := json.Marshal(spec.Value) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, spec.Expected, string(b)) | ||
}) | ||
} | ||
} | ||
|
||
func TestPath_UnmarshalJSON(t *testing.T) { | ||
type Spec struct { | ||
Value string | ||
Expected Path | ||
} | ||
specs := []*Spec{ | ||
{ | ||
Value: `["a",2,"c"]`, | ||
Expected: Path{PathName("a"), PathIndex(2), PathName("c")}, | ||
}, | ||
{ | ||
Value: `[]`, | ||
Expected: Path{}, | ||
}, | ||
{ | ||
Value: `[1,"b"]`, | ||
Expected: Path{PathIndex(1), PathName("b")}, | ||
}, | ||
} | ||
|
||
for _, spec := range specs { | ||
t.Run(spec.Value, func(t *testing.T) { | ||
var path Path | ||
err := json.Unmarshal([]byte(spec.Value), &path) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, spec.Expected, path) | ||
}) | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug if path starts with number?
eg
.[0].foo.bar
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected is
[0].foo.bar
? 🤔I think number is not coming at head of path.
so, this PR can't validate element value.
nil
value