Skip to content

Commit 4443069

Browse files
committed
refactor: define interfaces for JSON values
1 parent fc726fd commit 4443069

File tree

2 files changed

+106
-94
lines changed

2 files changed

+106
-94
lines changed

dsl/deparse.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ func Deparse(expr types.Expr) (interface{}, error) {
1919
}
2020

2121
func deparse(value types.Value) (interface{}, error) {
22-
switch val := value.(type) {
23-
case *types.Null:
22+
if _, ok := value.Type().(*types.NullType); ok {
2423
return nil, nil
25-
case *types.Bool:
24+
}
25+
switch val := value.(type) {
26+
case types.Bool:
2627
return val.Value(), nil
27-
case *types.String:
28+
case types.String:
2829
return val.Value(), nil
29-
case *types.Number:
30+
case types.Number:
3031
return val.Value(), nil
31-
case *types.Object:
32+
case types.Object:
3233
m := make(map[string]interface{}, len(val.Value()))
3334
for k, o := range val.Value() {
3435
v, err := deparse(o)

dsl/types/json.go

Lines changed: 99 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,198 +4,209 @@ import "context"
44

55
// ================ Null ================
66

7-
// Null is JSON null struct
8-
type Null struct{}
9-
107
// NullValue is the JSON null value
11-
var NullValue = &Null{}
8+
var NullValue = &nullT{}
129

13-
// Invert returns itself as-is.
14-
func (v *Null) Invert() (Value, error) {
15-
return v, nil
10+
type nullT struct{}
11+
12+
func (*nullT) Invert() (Value, error) {
13+
return NullValue, nil
1614
}
1715

18-
// Eval returns itself as-is.
19-
func (v *Null) Eval(context.Context) (val Value, rollback func(), err error) {
16+
func (v *nullT) Eval(context.Context) (val Value, rollback func(), err error) {
2017
return v, func() {}, nil
2118
}
2219

23-
// Type returns the type.
24-
func (v *Null) Type() Type {
20+
func (*nullT) Type() Type {
2521
return &NullType{}
2622
}
2723

2824
// ================ Bool ================
2925

26+
// TrueValue is the JSON true value
27+
var TrueValue = &boolT{true}
28+
29+
// FalseValue is the JSON false value
30+
var FalseValue = &boolT{false}
31+
3032
// Bool is JSON boolean struct
31-
type Bool struct {
32-
value bool
33+
type Bool interface {
34+
Value
35+
36+
// Value returns the holding internal value
37+
Value() bool
3338
}
3439

3540
// NewBool creates Bool instance
36-
func NewBool(value bool) *Bool {
41+
func NewBool(value bool) Bool {
3742
if value {
3843
return TrueValue
3944
}
4045
return FalseValue
4146
}
4247

43-
// Value returns the holding internal value
44-
func (v *Bool) Value() bool {
48+
type boolT struct {
49+
value bool
50+
}
51+
52+
func (v *boolT) Value() bool {
4553
return v.value
4654
}
4755

48-
// Invert returns itself as-is. All literal types of JSON values are the same.
49-
func (v *Bool) Invert() (Value, error) {
56+
func (v *boolT) Invert() (Value, error) {
5057
return v, nil
5158
}
5259

53-
// Eval returns itself as-is.
54-
func (v *Bool) Eval(context.Context) (val Value, rollback func(), err error) {
60+
func (v *boolT) Eval(context.Context) (val Value, rollback func(), err error) {
5561
return v, func() {}, nil
5662
}
5763

58-
// Type returns the type.
59-
func (v *Bool) Type() Type {
64+
func (*boolT) Type() Type {
6065
return &BoolType{}
6166
}
6267

63-
// TrueValue is the JSON true value
64-
var TrueValue = &Bool{true}
65-
66-
// FalseValue is the JSON false value
67-
var FalseValue = &Bool{false}
68-
6968
// ================ Number ================
7069

7170
// Number is JSON number struct
72-
type Number struct {
73-
value float64
74-
}
71+
type Number interface {
72+
Value
7573

76-
// Value returns the holding internal value
77-
func (v *Number) Value() float64 {
78-
return v.value
74+
// Value returns the holding internal value
75+
Value() float64
7976
}
8077

8178
// NewNumber creates Number instance
82-
func NewNumber(value float64) *Number {
83-
return &Number{value: value}
79+
func NewNumber(value float64) Number {
80+
return &numberT{value: value}
8481
}
8582

86-
// Invert returns itself as-is. All literal types of JSON values are the same.
87-
func (v *Number) Invert() (Value, error) {
83+
type numberT struct {
84+
value float64
85+
}
86+
87+
func (v *numberT) Value() float64 {
88+
return v.value
89+
}
90+
91+
func (v *numberT) Invert() (Value, error) {
8892
return v, nil
8993
}
9094

91-
// Eval returns itself as-is.
92-
func (v *Number) Eval(context.Context) (val Value, rollback func(), err error) {
95+
func (v *numberT) Eval(context.Context) (val Value, rollback func(), err error) {
9396
return v, func() {}, nil
9497
}
9598

96-
// Type returns the type.
97-
func (v *Number) Type() Type {
99+
func (*numberT) Type() Type {
98100
return &NumberType{}
99101
}
100102

101103
// ================ String ================
102104

103105
// String is JSON string struct
104-
type String struct {
105-
value string
106-
}
106+
type String interface {
107+
Value
107108

108-
// Value returns the holding internal value
109-
func (v *String) Value() string {
110-
return v.value
109+
// Value returns the holding internal value
110+
Value() string
111111
}
112112

113113
// NewString creates String instance
114-
func NewString(value string) *String {
115-
return &String{value: value}
114+
func NewString(value string) String {
115+
return &stringT{value: value}
116116
}
117117

118-
// Invert returns itself as-is. All literal types of JSON values are the same.
119-
func (v *String) Invert() (Value, error) {
118+
type stringT struct {
119+
value string
120+
}
121+
122+
func (v *stringT) Value() string {
123+
return v.value
124+
}
125+
126+
func (v *stringT) Invert() (Value, error) {
120127
return v, nil
121128
}
122129

123-
// Eval returns itself as-is.
124-
func (v *String) Eval(context.Context) (val Value, rollback func(), err error) {
130+
func (v *stringT) Eval(context.Context) (val Value, rollback func(), err error) {
125131
return v, func() {}, nil
126132
}
127133

128-
// Type returns the type.
129-
func (v *String) Type() Type {
134+
func (*stringT) Type() Type {
130135
return &StringType{}
131136
}
132137

133138
// ================ Array ================
134139

135140
// Array is JSON array struct
136-
type Array struct {
141+
type Array interface {
142+
Value
143+
144+
// Value returns the holding internal value.
145+
// DO NOT CHANGE THE RETURN VALUE DIRECTLY!
146+
// Copy the slice before changing the value.
147+
Value() []Value
148+
}
149+
150+
// NewArray creates Array instance
151+
func NewArray(value []Value, argType Type) Array {
152+
return &arrayT{value: value, argType: argType}
153+
}
154+
155+
type arrayT struct {
137156
value []Value
138157
argType Type
139158
}
140159

141-
// Value returns the holding internal value.
142-
// DO NOT CHANGE THE RETURN VALUE DIRECTLY!
143-
// Copy the slice before changing the value.
144-
func (v *Array) Value() []Value {
160+
func (v *arrayT) Value() []Value {
145161
return v.value
146162
}
147163

148-
// NewArray creates Array instance
149-
func NewArray(value []Value, argType Type) *Array {
150-
return &Array{value: value, argType: argType}
151-
}
152-
153-
// Invert returns itself as-is. All literal types of JSON values are the same.
154-
func (v *Array) Invert() (Value, error) {
164+
func (v *arrayT) Invert() (Value, error) {
155165
return v, nil
156166
}
157167

158-
// Eval returns itself as-is.
159-
func (v *Array) Eval(context.Context) (val Value, rollback func(), err error) {
168+
func (v *arrayT) Eval(context.Context) (val Value, rollback func(), err error) {
160169
return v, func() {}, nil
161170
}
162171

163-
// Type returns the type.
164-
func (v *Array) Type() Type {
172+
func (v *arrayT) Type() Type {
165173
return NewArrayType(v.argType)
166174
}
167175

168176
// ================ Object ================
169177

170178
// Object is JSON object struct
171-
type Object struct {
179+
type Object interface {
180+
Value
181+
182+
// Value returns the holding internal value.
183+
// DO NOT CHANGE THE RETURN VALUE DIRECTLY!
184+
// Copy the map instance before changing the value.
185+
Value() map[string]Value
186+
}
187+
188+
// NewObject creates Object instance
189+
func NewObject(value map[string]Value, argType Type) Object {
190+
return &objectT{value: value, argType: argType}
191+
}
192+
193+
type objectT struct {
172194
value map[string]Value
173195
argType Type
174196
}
175197

176-
// Value returns the holding internal value.
177-
// DO NOT CHANGE THE RETURN VALUE DIRECTLY!
178-
// Copy the map instance before changing the value.
179-
func (v *Object) Value() map[string]Value {
198+
func (v *objectT) Value() map[string]Value {
180199
return v.value
181200
}
182201

183-
// NewObject creates Object instance
184-
func NewObject(value map[string]Value, argType Type) *Object {
185-
return &Object{value: value, argType: argType}
186-
}
187-
188-
// Invert returns itself as-is. All literal types of JSON values are the same.
189-
func (v *Object) Invert() (Value, error) {
202+
func (v *objectT) Invert() (Value, error) {
190203
return v, nil
191204
}
192205

193-
// Eval returns itself as-is.
194-
func (v *Object) Eval(context.Context) (val Value, rollback func(), err error) {
206+
func (v *objectT) Eval(context.Context) (val Value, rollback func(), err error) {
195207
return v, func() {}, nil
196208
}
197209

198-
// Type returns the type.
199-
func (v *Object) Type() Type {
210+
func (v *objectT) Type() Type {
200211
return NewObjectType(v.argType)
201212
}

0 commit comments

Comments
 (0)