Skip to content

Commit 03e732d

Browse files
committed
refactor: define interfaces for JSON types
1 parent 4443069 commit 03e732d

File tree

3 files changed

+61
-64
lines changed

3 files changed

+61
-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: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,140 +9,138 @@ 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+
type voidType struct{}
1615

17-
func (*VoidType) String() string {
16+
func (*voidType) String() string {
1817
return "Void"
1918
}
2019

21-
// InstanceOf returns true if t is VoidType
22-
func (*VoidType) InstanceOf(t Type) bool {
23-
if _, ok := t.(*VoidType); ok {
20+
func (*voidType) InstanceOf(t Type) bool {
21+
if _, ok := t.(*voidType); ok {
2422
return true
2523
}
2624
return false
2725
}
2826

29-
// ===================== NullType ===================== //
27+
// ===================== Null type ===================== //
3028

31-
// NullType is JSON null type
32-
type NullType struct{}
29+
// NullType is a null type
30+
var NullType = &nullType{}
3331

34-
func (*NullType) String() string {
32+
type nullType struct{}
33+
34+
func (*nullType) String() string {
3535
return "Null"
3636
}
3737

38-
// InstanceOf returns true if t is NullType
39-
func (*NullType) InstanceOf(t Type) bool {
40-
if _, ok := t.(*NullType); ok {
38+
func (*nullType) InstanceOf(t Type) bool {
39+
if _, ok := t.(*nullType); ok {
4140
return true
4241
}
4342
return false
4443
}
4544

46-
// ===================== BoolType ===================== //
45+
// ===================== Bool type ===================== //
46+
47+
// BoolType is a null type
48+
var BoolType = &boolType{}
4749

48-
// BoolType is JSON boolean type
49-
type BoolType struct{}
50+
type boolType struct{}
5051

51-
func (*BoolType) String() string {
52+
func (*boolType) String() string {
5253
return "Bool"
5354
}
5455

55-
// InstanceOf returns true if t is BoolType
56-
func (*BoolType) InstanceOf(t Type) bool {
57-
if _, ok := t.(*BoolType); ok {
56+
func (*boolType) InstanceOf(t Type) bool {
57+
if _, ok := t.(*boolType); ok {
5858
return true
5959
}
6060
return false
6161
}
6262

63-
// ===================== NumberType ===================== //
63+
// ===================== Number type ===================== //
6464

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

68-
func (*NumberType) String() string {
68+
type numberType struct{}
69+
70+
func (*numberType) String() string {
6971
return "Number"
7072
}
7173

72-
// InstanceOf returns true if t is NumberType
73-
func (*NumberType) InstanceOf(t Type) bool {
74-
if _, ok := t.(*NumberType); ok {
74+
func (*numberType) InstanceOf(t Type) bool {
75+
if _, ok := t.(*numberType); ok {
7576
return true
7677
}
7778
return false
7879
}
7980

80-
// ===================== StringType ===================== //
81+
// ===================== String type ===================== //
82+
83+
// StringType is a null type
84+
var StringType = &stringType{}
8185

82-
// StringType is JSON string type
83-
type StringType struct{}
86+
type stringType struct{}
8487

85-
func (*StringType) String() string {
88+
func (*stringType) String() string {
8689
return "String"
8790
}
8891

89-
// InstanceOf returns true if t is StringType
90-
func (*StringType) InstanceOf(t Type) bool {
91-
if _, ok := t.(*StringType); ok {
92+
func (*stringType) InstanceOf(t Type) bool {
93+
if _, ok := t.(*stringType); ok {
9294
return true
9395
}
9496
return false
9597
}
9698

97-
// ===================== ArrayType ===================== //
99+
// ===================== Array type ===================== //
98100

99-
// ArrayType is JSON array type
100-
type ArrayType struct {
101-
arg Type
101+
// NewArrayType creates array type instance
102+
func NewArrayType(arg Type) Type {
103+
return &arrayType{arg: arg}
102104
}
103105

104-
// NewArrayType creates ArrayType instance
105-
func NewArrayType(arg Type) *ArrayType {
106-
return &ArrayType{arg: arg}
106+
type arrayType struct {
107+
arg Type
107108
}
108109

109-
func (t *ArrayType) String() string {
110+
func (t *arrayType) String() string {
110111
return "Array[" + t.arg.String() + "]"
111112
}
112113

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 {
114+
func (t *arrayType) InstanceOf(t2 Type) bool {
115+
if array, ok := t2.(*arrayType); ok {
116116
return t.arg.InstanceOf(array.arg)
117117
}
118118
return false
119119
}
120120

121-
// ===================== ObjectType ===================== //
121+
// ===================== Object type ===================== //
122122

123-
// ObjectType is JSON object type
124-
type ObjectType struct {
125-
arg Type
123+
// NewObjectType creates object type instance
124+
func NewObjectType(arg Type) Type {
125+
return &objectType{arg: arg}
126126
}
127127

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

133-
func (t *ObjectType) String() string {
132+
func (t *objectType) String() string {
134133
return "Object[" + t.arg.String() + "]"
135134
}
136135

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 {
136+
func (t *objectType) InstanceOf(t2 Type) bool {
137+
if array, ok := t2.(*objectType); ok {
140138
return t.arg.InstanceOf(array.arg)
141139
}
142140
return false
143141
}
144142

145-
// ===================== AnyType ===================== //
143+
// ===================== Any type ===================== //
146144

147145
// AnyValue allows any type
148146
var AnyValue = &anyType{}
@@ -153,7 +151,6 @@ func (*anyType) String() string {
153151
return "Any"
154152
}
155153

156-
// InstanceOf always returns true
157154
func (*anyType) InstanceOf(_ Type) bool {
158155
return true
159156
}

0 commit comments

Comments
 (0)