Skip to content

Commit c584cee

Browse files
committed
cleaning up
1 parent 82169e2 commit c584cee

File tree

8 files changed

+77
-173
lines changed

8 files changed

+77
-173
lines changed

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ val := jsonast.Parse(`{
3232
}
3333
}`)
3434

35-
walked := val.
36-
Walk().
35+
walked := jsonast.NewWalker(val).
3736
Field("street_num").
38-
.Field("street_name")
39-
.Field("city").
40-
.Field("state").Validate(func(v Value) bool {
37+
Field("street_name").
38+
Field("city").
39+
Field("state").Validate(func(v Value) bool {
4140
str, ok := v.(jsonast.String)
4241
if !ok {
4342
return false

array.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,18 @@ type arrayImpl struct {
3232
arr []Value
3333
}
3434

35-
func isArray(i interface{}) bool {
36-
_, ok := i.([]interface{})
37-
return ok
38-
}
39-
40-
func newArray(v *value) (Array, error) {
41-
if !isArray(v.cur) {
42-
return nil, errNotAnArray
43-
}
44-
ifaceArr := v.cur.([]interface{})
45-
convertedArr := make([]Value, len(ifaceArr))
46-
for i, iface := range ifaceArr {
47-
val, err := valueFromIface(iface)
48-
if err != nil {
49-
return nil, err
50-
}
51-
convertedArr[i] = val
35+
func newArray(elts []Value) Array {
36+
return arrayImpl{
37+
Value: valueImpl{isArray: true},
38+
arr: elts,
5239
}
53-
return &arrayImpl{Value: v, arr: convertedArr}, nil
5440
}
5541

56-
func (v *arrayImpl) Elts() []Value {
42+
func (v arrayImpl) Elts() []Value {
5743
return v.arr
5844
}
5945

60-
func (v *arrayImpl) Foreach(fn func(Value) error) error {
46+
func (v arrayImpl) Foreach(fn func(Value) error) error {
6147
for _, val := range v.arr {
6248
if err := fn(val); err != nil {
6349
return err
@@ -66,7 +52,7 @@ func (v *arrayImpl) Foreach(fn func(Value) error) error {
6652
return nil
6753
}
6854

69-
func (v *arrayImpl) At(i int) (Value, error) {
55+
func (v arrayImpl) At(i int) (Value, error) {
7056
if i >= len(v.arr) {
7157
return nil, errIndexOutOfBounds{i: i}
7258
}

bool.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,11 @@ type boolImpl struct {
1717
val bool
1818
}
1919

20-
func isBool(i interface{}) bool {
21-
_, ok := i.(bool)
22-
return ok
23-
}
24-
25-
func newBool(v *value) (Bool, error) {
26-
if !isBool(v.cur) {
27-
return nil, errNotABool
28-
}
20+
func newBool(b bool) Bool {
2921
return boolImpl{
30-
Value: v,
31-
val: v.cur.(bool),
32-
}, nil
22+
Value: valueImpl{isBool: true},
23+
val: b,
24+
}
3325
}
3426

3527
func (b boolImpl) True() bool {

number.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ type number struct {
1717
i float64
1818
}
1919

20+
func newNumber(f float64) Number {
21+
return &number{
22+
Value: valueImpl{isNumber: true},
23+
i: f,
24+
}
25+
}
26+
2027
func (n *number) Float64() float64 {
2128
return n.i
2229
}
2330

31+
// TODO: remove?
2432
func convertNumber(i interface{}) (float64, error) {
2533
switch t := i.(type) {
2634
case int:
@@ -50,16 +58,3 @@ func convertNumber(i interface{}) (float64, error) {
5058

5159
}
5260
}
53-
54-
func isNumber(i interface{}) bool {
55-
_, err := convertNumber(i)
56-
return err == nil
57-
}
58-
59-
func newNumber(v *value) (Number, error) {
60-
flt, err := convertNumber(v.cur)
61-
if err != nil {
62-
return nil, err
63-
}
64-
return &number{Value: v, i: flt}, nil
65-
}

object.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jsonast
22

33
import (
44
"errors"
5+
"fmt"
56
)
67

78
var (
@@ -12,21 +13,36 @@ var (
1213
type Object interface {
1314
Value
1415
Fields() []ObjectPair
15-
ValueAt(name string) (Value, error)
16+
ValueAt(key string) (Value, error)
1617
}
1718

1819
// ObjectPair represents a single key-value pair in an object
1920
type ObjectPair struct {
20-
Name string
21+
Key string
2122
Value Value
2223
}
2324

24-
type object struct {
25+
type objectImpl struct {
2526
Value
26-
obj []ObjectPair
27+
pairs []ObjectPair
2728
}
2829

29-
func isObject(i interface{}) bool {
30-
_, ok := i.(map[interface{}]interface{})
31-
return ok
30+
func newObject(pairs []ObjectPair) Object {
31+
return objectImpl{
32+
Value: valueImpl{isObject: true},
33+
pairs: pairs,
34+
}
35+
}
36+
37+
func (o objectImpl) Fields() []ObjectPair {
38+
return o.pairs
39+
}
40+
41+
func (o objectImpl) ValueAt(key string) (Value, error) {
42+
for _, pair := range o.pairs {
43+
if pair.Key == key {
44+
return pair.Value, nil
45+
}
46+
}
47+
return nil, fmt.Errorf("no such key %s", key)
3248
}

string.go

+5-15
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ import (
88
var errNotAString = errors.New("not a string")
99

1010
// String is a string JSON value
11-
type String struct {
11+
type String interface {
1212
fmt.Stringer
1313
Value
14-
Str string
15-
}
16-
17-
func NewString(s string) String {
18-
String{Str: s}
1914
}
2015

2116
type stringImpl struct {
@@ -27,14 +22,9 @@ func (s stringImpl) String() string {
2722
return s.str
2823
}
2924

30-
func isString(i interface{}) bool {
31-
_, ok := i.(string)
32-
return ok
33-
}
34-
35-
func newString(v *value) (String, error) {
36-
if !isString(v.cur) {
37-
return nil, errNotAString
25+
func newString(str string) String {
26+
return stringImpl{
27+
Value: valueImpl{isString: true},
28+
str: str,
3829
}
39-
return &stringImpl{Value: v, str: v.cur.(string)}, nil
4030
}

value.go

+13-93
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package jsonast
22

3-
import (
4-
"encoding/json"
5-
)
6-
73
// Value is a generic node in a JSON AST. This implements fmt.Stringer
84
type Value interface {
9-
// Walk creates a walker, which is capable of walking the JSON
10-
// tree with a few function calls
11-
Walk() Walker
125
// IsString returns true if the data at this node is a string
136
IsString() bool
147
// IsNumber returns true if the data at this node is numeric
@@ -23,91 +16,18 @@ type Value interface {
2316
IsNull() bool
2417
}
2518

26-
type value struct {
27-
b []byte
28-
cur interface{}
29-
}
30-
31-
// ValueFromBytes decodes b into a Value, or returns an error if b is invalid
32-
// json
33-
func ValueFromBytes(b []byte) (Value, error) {
34-
var iface interface{}
35-
if err := json.Unmarshal(b, &iface); err != nil {
36-
return nil, err
37-
}
38-
return &value{cur: iface, b: b}, nil
39-
}
40-
41-
func valueFromIface(i interface{}) (Value, error) {
42-
b, err := json.Marshal(i)
43-
if err != nil {
44-
return nil, err
45-
}
46-
return &value{b: b, cur: i}, nil
47-
}
48-
49-
func (v *value) Bytes() []byte {
50-
return v.b
51-
}
52-
53-
// func (v *value) String() string {
54-
// return string(v.b)
55-
// }
56-
57-
func (v *value) IsString() bool {
58-
return isString(v.cur)
59-
}
60-
61-
// func (v *value) StringVal() (String, error) {
62-
// return newString(v)
63-
// }
64-
65-
func (v *value) IsNumber() bool {
66-
return isNumber(v.cur)
67-
}
68-
69-
// func (v *value) NumberVal() (Number, error) {
70-
// return newNumber(v)
71-
// }
72-
73-
func (v *value) IsBool() bool {
74-
return isBool(v)
75-
}
76-
77-
// func (v *value) BoolVal() (Bool, error) {
78-
// if !v.IsBool() {
79-
// return nil, errNotABool
80-
// }
81-
// return newBool(v), nil
82-
// }
83-
84-
func (v *value) IsObject() bool {
85-
_, ok := v.cur.(map[interface{}]interface{})
86-
return ok
87-
}
88-
89-
// func (v *value) ObjectVal() (Object, error) {
90-
// if !v.IsObject() {
91-
// return nil, errNotAnObject
92-
// }
93-
// return newObject(v)
94-
// }
95-
96-
func (v *value) IsArray() bool {
97-
return isArray(v.cur)
98-
}
99-
100-
// func (v *value) ArrayVal() (Array, error) {
101-
// if !v.IsArray() {
102-
// return nil, errNotAnArray
103-
// }
104-
// return newArray(v)
105-
// }
106-
107-
func (v *value) IsNull() bool {
108-
return v.cur == nil
19+
type valueImpl struct {
20+
isString bool
21+
isNumber bool
22+
isBool bool
23+
isObject bool
24+
isArray bool
25+
isNull bool
10926
}
11027

111-
// func (v *value) NullVal() (Null, error) {
112-
// return newNull(v)
113-
// }
28+
func (v valueImpl) IsString() bool { return v.isString }
29+
func (v valueImpl) IsNumber() bool { return v.isNumber }
30+
func (v valueImpl) IsBool() bool { return v.isBool }
31+
func (v valueImpl) IsObject() bool { return v.isObject }
32+
func (v valueImpl) IsArray() bool { return v.isArray }
33+
func (v valueImpl) IsNull() bool { return v.isNull }

walker.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import (
77
// Walker is a utility for walking the JSON tree with a few function calls,
88
// and transforming the JSON that was walked into a type
99
type Walker struct {
10-
latestValue Value
11-
Nulls []Null
12-
Strings []String
13-
Numbers []Number
14-
Objects []Object
15-
Arrays []Array
16-
err error
10+
initialValue Value
11+
latestValue Value
12+
Nulls []Null
13+
Strings []String
14+
Numbers []Number
15+
Objects []Object
16+
Arrays []Array
17+
err error
18+
}
19+
20+
// NewWalker creates a new Walker from the given initVal
21+
func NewWalker(initVal Value) Walker {
22+
return Walker{initialValue: initVal}
1723
}
1824

1925
// Elt picks out the ith element of the current array

0 commit comments

Comments
 (0)