Skip to content

Commit 6aa55c9

Browse files
committed
refactor: define interfaces for JSON types
1 parent 4443069 commit 6aa55c9

File tree

3 files changed

+64
-64
lines changed

3 files changed

+64
-64
lines changed

dsl/deparse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Deparse(expr types.Expr) (interface{}, error) {
1919
}
2020

2121
func deparse(value types.Value) (interface{}, error) {
22-
if _, ok := value.Type().(*types.NullType); ok {
22+
if value.Type() == types.NullType {
2323
return nil, nil
2424
}
2525
switch val := value.(type) {

dsl/types/json.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (v *nullT) Eval(context.Context) (val Value, rollback func(), err error) {
1818
}
1919

2020
func (*nullT) Type() Type {
21-
return &NullType{}
21+
return NullType
2222
}
2323

2424
// ================ Bool ================
@@ -62,7 +62,7 @@ func (v *boolT) Eval(context.Context) (val Value, rollback func(), err error) {
6262
}
6363

6464
func (*boolT) Type() Type {
65-
return &BoolType{}
65+
return BoolType
6666
}
6767

6868
// ================ Number ================
@@ -97,7 +97,7 @@ func (v *numberT) Eval(context.Context) (val Value, rollback func(), err error)
9797
}
9898

9999
func (*numberT) Type() Type {
100-
return &NumberType{}
100+
return NumberType
101101
}
102102

103103
// ================ String ================
@@ -132,7 +132,7 @@ func (v *stringT) Eval(context.Context) (val Value, rollback func(), err error)
132132
}
133133

134134
func (*stringT) Type() Type {
135-
return &StringType{}
135+
return StringType
136136
}
137137

138138
// ================ Array ================

dsl/types/types.go

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,140 +9,141 @@ type Type interface {
99
InstanceOf(Type) bool
1010
}
1111

12-
// ===================== VoidType ===================== //
12+
// ===================== Void type ===================== //
1313

14-
// VoidType is void type
15-
type VoidType struct{}
14+
// VoidType is a void type
15+
var VoidType = &voidType{}
1616

17-
func (*VoidType) String() string {
17+
type voidType struct{}
18+
19+
func (*voidType) String() string {
1820
return "Void"
1921
}
2022

21-
// InstanceOf returns true if t is VoidType
22-
func (*VoidType) InstanceOf(t Type) bool {
23-
if _, ok := t.(*VoidType); ok {
23+
func (*voidType) InstanceOf(t Type) bool {
24+
if _, ok := t.(*voidType); ok {
2425
return true
2526
}
2627
return false
2728
}
2829

29-
// ===================== NullType ===================== //
30+
// ===================== Null type ===================== //
31+
32+
// NullType is a null type
33+
var NullType = &nullType{}
3034

31-
// NullType is JSON null type
32-
type NullType struct{}
35+
type nullType struct{}
3336

34-
func (*NullType) String() string {
37+
func (*nullType) String() string {
3538
return "Null"
3639
}
3740

38-
// InstanceOf returns true if t is NullType
39-
func (*NullType) InstanceOf(t Type) bool {
40-
if _, ok := t.(*NullType); ok {
41+
func (*nullType) InstanceOf(t Type) bool {
42+
if _, ok := t.(*nullType); ok {
4143
return true
4244
}
4345
return false
4446
}
4547

46-
// ===================== BoolType ===================== //
48+
// ===================== Bool type ===================== //
49+
50+
// BoolType is a null type
51+
var BoolType = &boolType{}
4752

48-
// BoolType is JSON boolean type
49-
type BoolType struct{}
53+
type boolType struct{}
5054

51-
func (*BoolType) String() string {
55+
func (*boolType) String() string {
5256
return "Bool"
5357
}
5458

55-
// InstanceOf returns true if t is BoolType
56-
func (*BoolType) InstanceOf(t Type) bool {
57-
if _, ok := t.(*BoolType); ok {
59+
func (*boolType) InstanceOf(t Type) bool {
60+
if _, ok := t.(*boolType); ok {
5861
return true
5962
}
6063
return false
6164
}
6265

63-
// ===================== NumberType ===================== //
66+
// ===================== Number type ===================== //
6467

65-
// NumberType is JSON number type
66-
type NumberType struct{}
68+
// NumberType is a null type
69+
var NumberType = &numberType{}
6770

68-
func (*NumberType) String() string {
71+
type numberType struct{}
72+
73+
func (*numberType) String() string {
6974
return "Number"
7075
}
7176

72-
// InstanceOf returns true if t is NumberType
73-
func (*NumberType) InstanceOf(t Type) bool {
74-
if _, ok := t.(*NumberType); ok {
77+
func (*numberType) InstanceOf(t Type) bool {
78+
if _, ok := t.(*numberType); ok {
7579
return true
7680
}
7781
return false
7882
}
7983

80-
// ===================== StringType ===================== //
84+
// ===================== String type ===================== //
85+
86+
// StringType is a null type
87+
var StringType = &stringType{}
8188

82-
// StringType is JSON string type
83-
type StringType struct{}
89+
type stringType struct{}
8490

85-
func (*StringType) String() string {
91+
func (*stringType) String() string {
8692
return "String"
8793
}
8894

89-
// InstanceOf returns true if t is StringType
90-
func (*StringType) InstanceOf(t Type) bool {
91-
if _, ok := t.(*StringType); ok {
95+
func (*stringType) InstanceOf(t Type) bool {
96+
if _, ok := t.(*stringType); ok {
9297
return true
9398
}
9499
return false
95100
}
96101

97-
// ===================== ArrayType ===================== //
102+
// ===================== Array type ===================== //
98103

99-
// ArrayType is JSON array type
100-
type ArrayType struct {
101-
arg Type
104+
// NewArrayType creates array type instance
105+
func NewArrayType(arg Type) Type {
106+
return &arrayType{arg: arg}
102107
}
103108

104-
// NewArrayType creates ArrayType instance
105-
func NewArrayType(arg Type) *ArrayType {
106-
return &ArrayType{arg: arg}
109+
type arrayType struct {
110+
arg Type
107111
}
108112

109-
func (t *ArrayType) String() string {
113+
func (t *arrayType) String() string {
110114
return "Array[" + t.arg.String() + "]"
111115
}
112116

113-
// InstanceOf returns true if t is instance of t2
114-
func (t *ArrayType) InstanceOf(t2 Type) bool {
115-
if array, ok := t2.(*ArrayType); ok {
117+
func (t *arrayType) InstanceOf(t2 Type) bool {
118+
if array, ok := t2.(*arrayType); ok {
116119
return t.arg.InstanceOf(array.arg)
117120
}
118121
return false
119122
}
120123

121-
// ===================== ObjectType ===================== //
124+
// ===================== Object type ===================== //
122125

123-
// ObjectType is JSON object type
124-
type ObjectType struct {
125-
arg Type
126+
// NewObjectType creates object type instance
127+
func NewObjectType(arg Type) Type {
128+
return &objectType{arg: arg}
126129
}
127130

128-
// NewObjectType creates ObjectType instance
129-
func NewObjectType(arg Type) *ObjectType {
130-
return &ObjectType{arg: arg}
131+
type objectType struct {
132+
arg Type
131133
}
132134

133-
func (t *ObjectType) String() string {
135+
func (t *objectType) String() string {
134136
return "Object[" + t.arg.String() + "]"
135137
}
136138

137-
// InstanceOf returns true if t is instance of t2
138-
func (t *ObjectType) InstanceOf(t2 Type) bool {
139-
if array, ok := t2.(*ObjectType); ok {
139+
func (t *objectType) InstanceOf(t2 Type) bool {
140+
if array, ok := t2.(*objectType); ok {
140141
return t.arg.InstanceOf(array.arg)
141142
}
142143
return false
143144
}
144145

145-
// ===================== AnyType ===================== //
146+
// ===================== Any type ===================== //
146147

147148
// AnyValue allows any type
148149
var AnyValue = &anyType{}
@@ -153,7 +154,6 @@ func (*anyType) String() string {
153154
return "Any"
154155
}
155156

156-
// InstanceOf always returns true
157157
func (*anyType) InstanceOf(_ Type) bool {
158158
return true
159159
}

0 commit comments

Comments
 (0)