-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from vektah/feat-path
introduce ast.Path type
- Loading branch information
Showing
6 changed files
with
178 additions
and
27 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
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