Skip to content

Commit 55b7eb4

Browse files
committed
(chore)Export more helper functions.
1 parent 6759dc9 commit 55b7eb4

File tree

5 files changed

+102
-89
lines changed

5 files changed

+102
-89
lines changed

array_str.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func opFilter(apply Applier, params []interface{}, data interface{}) (res interf
6565
if err != nil {
6666
return nil, err
6767
}
68-
if toBool(r) {
68+
if ToBool(r) {
6969
filteredArr = append(filteredArr, item)
7070
}
7171
}
@@ -138,7 +138,7 @@ func opAll(apply Applier, params []interface{}, data interface{}) (res interface
138138
if err != nil {
139139
return nil, err
140140
}
141-
if !toBool(r) {
141+
if !ToBool(r) {
142142
return false, nil
143143
}
144144
}
@@ -175,7 +175,7 @@ func opNone(apply Applier, params []interface{}, data interface{}) (res interfac
175175
if err != nil {
176176
return nil, err
177177
}
178-
if toBool(r) {
178+
if ToBool(r) {
179179
return false, nil
180180
}
181181
}
@@ -212,7 +212,7 @@ func opSome(apply Applier, params []interface{}, data interface{}) (res interfac
212212
if err != nil {
213213
return nil, err
214214
}
215-
if toBool(r) {
215+
if ToBool(r) {
216216
return true, nil
217217
}
218218
}
@@ -258,14 +258,14 @@ func opIn(apply Applier, params []interface{}, data interface{}) (res interface{
258258
}
259259

260260
param0 := params[0]
261-
if !isPrimitive(param0) {
261+
if !IsPrimitive(param0) {
262262
return nil, fmt.Errorf("in: expect json primitive as first param but got %T", param0)
263263
}
264264

265265
switch param1 := params[1].(type) {
266266
case []interface{}:
267267
for _, item := range param1 {
268-
if !isPrimitive(item) {
268+
if !IsPrimitive(item) {
269269
return nil, fmt.Errorf("in: expect json primitives in array but got %T", item)
270270
}
271271
if param0 == item {
@@ -274,7 +274,7 @@ func opIn(apply Applier, params []interface{}, data interface{}) (res interface{
274274
}
275275
return false, nil
276276
case string:
277-
s, err := toString(param0)
277+
s, err := ToString(param0)
278278
if err != nil {
279279
return nil, err
280280
}
@@ -298,7 +298,7 @@ func opCat(apply Applier, params []interface{}, data interface{}) (res interface
298298
}
299299
parts := []string{}
300300
for _, param := range params {
301-
s, err := toString(param)
301+
s, err := ToString(param)
302302
if err != nil {
303303
return nil, err
304304
}
@@ -322,15 +322,15 @@ func opSubstr(apply Applier, params []interface{}, data interface{}) (res interf
322322
return
323323
}
324324

325-
s, err := toString(params[0])
325+
s, err := ToString(params[0])
326326
if err != nil {
327327
return nil, err
328328
}
329329
r := []rune(s)
330330

331331
var start int
332332
{
333-
param1, err := toNumeric(params[1])
333+
param1, err := ToNumeric(params[1])
334334
if err != nil {
335335
return nil, err
336336
}
@@ -348,7 +348,7 @@ func opSubstr(apply Applier, params []interface{}, data interface{}) (res interf
348348
hasEnd bool
349349
)
350350
if len(params) > 2 {
351-
param2, err := toNumeric(params[2])
351+
param2, err := ToNumeric(params[2])
352352
if err != nil {
353353
return nil, err
354354
}

helpers.go

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func getLogic(obj interface{}) (op string, params []interface{}) {
3838
panic(fmt.Errorf("no operator in logic"))
3939
}
4040

41-
// isPrimitive returns true if obj is json primitive.
42-
func isPrimitive(obj interface{}) bool {
41+
// IsPrimitive returns true if obj is json primitive (null/bool/float64/string).
42+
func IsPrimitive(obj interface{}) bool {
4343
switch obj.(type) {
4444
case nil:
4545
return true
@@ -54,15 +54,15 @@ func isPrimitive(obj interface{}) bool {
5454
case map[string]interface{}:
5555
return false
5656
default:
57-
panic(fmt.Errorf("isPrimitive not support type %T", obj))
57+
panic(fmt.Errorf("IsPrimitive not support type %T", obj))
5858
}
5959
}
6060

61-
// toBool returns the truthy of a json object.
61+
// ToBool returns the truthy of a json object.
6262
// ref:
6363
// - http://jsonlogic.com/truthy.html
6464
// - json-logic-js/logic.js::truthy
65-
func toBool(obj interface{}) bool {
65+
func ToBool(obj interface{}) bool {
6666
switch o := obj.(type) {
6767
case nil:
6868
return false
@@ -78,23 +78,23 @@ func toBool(obj interface{}) bool {
7878
// Always true
7979
return true
8080
default:
81-
panic(fmt.Errorf("toBool got non-json type %T", obj))
81+
panic(fmt.Errorf("ToBool got non-json type %T", obj))
8282
}
8383
}
8484

85-
// toNumeric converts json primitive to numeric. It should be the same as JavaScript's Number(), except:
85+
// ToNumeric converts json primitive to numeric. It should be the same as JavaScript's Number(), except:
8686
// - an error is returned if obj is not a json primitive.
8787
// - an error is returned if obj is string but not well-formed.
8888
// - the number is NaN or +Inf/-Inf.
89-
func toNumeric(obj interface{}) (f float64, err error) {
89+
func ToNumeric(obj interface{}) (f float64, err error) {
9090
defer func() {
9191
if err == nil {
9292
if math.IsNaN(f) {
9393
f = 0
94-
err = fmt.Errorf("toNumeric got NaN")
94+
err = fmt.Errorf("ToNumeric got NaN")
9595
} else if math.IsInf(f, 0) {
9696
f = 0
97-
err = fmt.Errorf("toNumeric got +Inf/-Inf")
97+
err = fmt.Errorf("ToNumeric got +Inf/-Inf")
9898
}
9999
}
100100
}()
@@ -112,16 +112,16 @@ func toNumeric(obj interface{}) (f float64, err error) {
112112
case string:
113113
return strconv.ParseFloat(o, 64)
114114
case []interface{}, map[string]interface{}:
115-
return 0, fmt.Errorf("toNumeric not support type %T", obj)
115+
return 0, fmt.Errorf("ToNumeric not support type %T", obj)
116116
default:
117-
panic(fmt.Errorf("toNumeric got non-json type %T", obj))
117+
panic(fmt.Errorf("ToNumeric got non-json type %T", obj))
118118
}
119119
}
120120

121-
// toString converts json primitive to string. It should be the same as JavaScript's String(), except:
121+
// ToString converts json primitive to string. It should be the same as JavaScript's String(), except:
122122
// - an error is returned if obj is not a json primitive.
123123
// - obj is number NaN or +Inf/-Inf.
124-
func toString(obj interface{}) (string, error) {
124+
func ToString(obj interface{}) (string, error) {
125125
switch o := obj.(type) {
126126
case nil:
127127
return "null", nil
@@ -132,34 +132,36 @@ func toString(obj interface{}) (string, error) {
132132
return "false", nil
133133
case float64:
134134
if math.IsNaN(o) {
135-
return "", fmt.Errorf("toString got NaN")
135+
return "", fmt.Errorf("ToString got NaN")
136136
}
137137
if math.IsInf(o, 0) {
138-
return "", fmt.Errorf("toString got +Inf/-Inf")
138+
return "", fmt.Errorf("ToString got +Inf/-Inf")
139139
}
140140
return strconv.FormatFloat(o, 'f', -1, 64), nil
141141
case string:
142142
return o, nil
143143
case []interface{}, map[string]interface{}:
144-
return "", fmt.Errorf("toString not support type %T", obj)
144+
return "", fmt.Errorf("ToString not support type %T", obj)
145145
default:
146-
panic(fmt.Errorf("toString got non-json type %T", obj))
146+
panic(fmt.Errorf("ToString got non-json type %T", obj))
147147
}
148148
}
149149

150-
type compSymbol string
150+
// CompSymbol represents compare operator.
151+
type CompSymbol string
151152

152-
// compare symbol can be "<"/"<="/">"/">="
153153
const (
154-
lt compSymbol = "<"
155-
le compSymbol = "<="
156-
gt compSymbol = ">"
157-
ge compSymbol = ">="
154+
LT CompSymbol = "<"
155+
LE CompSymbol = "<="
156+
GT CompSymbol = ">"
157+
GE CompSymbol = ">="
158+
EQ CompSymbol = "==="
159+
NE CompSymbol = "!=="
158160
)
159161

160-
// compareValues compares json primitives. It should be the same as JavaScript's "<"/"<="/">"/">=", except:
162+
// CompareValues compares json primitives. It should be the same as JavaScript's "<"/"<="/">"/">="/"==="/"!==", except:
161163
// - an error is returned if any value is not a json primitive.
162-
// - any error retuend by toNumeric.
164+
// - any error retuend by ToNumeric.
163165
//
164166
// ref:
165167
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than
@@ -172,44 +174,51 @@ const (
172174
// > Strings are converted based on the values they contain, and are converted as NaN if they do not contain numeric values.
173175
// > If either value is NaN, the operator returns false.
174176
// > Otherwise the values are compared as numeric values.
175-
func compareValues(symbol compSymbol, left, right interface{}) (bool, error) {
176-
if !isPrimitive(left) || !isPrimitive(right) {
177+
func CompareValues(symbol CompSymbol, left, right interface{}) (bool, error) {
178+
if !IsPrimitive(left) || !IsPrimitive(right) {
177179
return false, fmt.Errorf("only primitive values can be compared")
178180
}
181+
switch symbol {
182+
case EQ:
183+
return left == right, nil
184+
case NE:
185+
return left != right, nil
186+
}
187+
179188
leftStr, leftIsStr := left.(string)
180189
rightStr, rightIsStr := right.(string)
181190
if leftIsStr && rightIsStr {
182191
switch symbol {
183-
case lt:
192+
case LT:
184193
return leftStr < rightStr, nil
185-
case le:
194+
case LE:
186195
return leftStr <= rightStr, nil
187-
case gt:
196+
case GT:
188197
return leftStr > rightStr, nil
189-
case ge:
198+
case GE:
190199
return leftStr >= rightStr, nil
191200
default:
192201
panic(fmt.Errorf("Impossible branch"))
193202
}
194203
}
195204

196-
leftNum, err := toNumeric(left)
205+
leftNum, err := ToNumeric(left)
197206
if err != nil {
198207
return false, err
199208
}
200209

201-
rightNum, err := toNumeric(right)
210+
rightNum, err := ToNumeric(right)
202211
if err != nil {
203212
return false, err
204213
}
205214
switch symbol {
206-
case lt:
215+
case LT:
207216
return leftNum < rightNum, nil
208-
case le:
217+
case LE:
209218
return leftNum <= rightNum, nil
210-
case gt:
219+
case GT:
211220
return leftNum > rightNum, nil
212-
case ge:
221+
case GE:
213222
return leftNum >= rightNum, nil
214223
default:
215224
panic(fmt.Errorf("Impossible branch"))

helpers_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestToBool(t *testing.T) {
6565
{Obj: map[string]interface{}{}, IsTrue: true},
6666
{Obj: map[string]interface{}{"var": "x"}, IsTrue: true},
6767
} {
68-
assert.Equal(testCase.IsTrue, toBool(testCase.Obj), "test case %d", i)
68+
assert.Equal(testCase.IsTrue, ToBool(testCase.Obj), "test case %d", i)
6969
}
7070
}
7171

@@ -92,7 +92,7 @@ func TestToNumeric(t *testing.T) {
9292
{Obj: "-inf", Err: true},
9393
{Obj: "+inf", Err: true},
9494
} {
95-
n, err := toNumeric(testCase.Obj)
95+
n, err := ToNumeric(testCase.Obj)
9696
if testCase.Err {
9797
assert.Error(err, "test case %d", i)
9898
} else {
@@ -101,6 +101,7 @@ func TestToNumeric(t *testing.T) {
101101
}
102102
}
103103
}
104+
104105
func TestToString(t *testing.T) {
105106
assert := assert.New(t)
106107

@@ -120,7 +121,7 @@ func TestToString(t *testing.T) {
120121
{Obj: []interface{}{1}, Err: true},
121122
{Obj: map[string]interface{}{}, Err: true},
122123
} {
123-
s, err := toString(testCase.Obj)
124+
s, err := ToString(testCase.Obj)
124125
if testCase.Err {
125126
assert.Error(err, "test case %d", i)
126127
} else {
@@ -134,19 +135,21 @@ func TestCompareValues(t *testing.T) {
134135
assert := assert.New(t)
135136

136137
for i, testCase := range []struct {
137-
Symbol compSymbol
138+
Symbol CompSymbol
138139
Left interface{}
139140
Right interface{}
140141
Result bool
141142
Err bool
142143
}{
143-
{Symbol: lt, Left: "1", Right: "a", Result: true}, // This is compared as strings.
144-
{Symbol: lt, Left: "1", Right: float64(1.1), Result: true}, // This is compared as numerics.
145-
{Symbol: lt, Left: float64(1), Right: "a", Err: true},
146-
{Symbol: lt, Left: "a", Right: float64(1), Err: true},
147-
{Symbol: lt, Left: []interface{}{}, Right: float64(1), Err: true},
144+
{Symbol: LT, Left: "1", Right: "a", Result: true}, // This is compared as strings.
145+
{Symbol: LT, Left: "1", Right: float64(1.1), Result: true}, // This is compared as numerics.
146+
{Symbol: LT, Left: float64(1), Right: "a", Err: true},
147+
{Symbol: LT, Left: "a", Right: float64(1), Err: true},
148+
{Symbol: LT, Left: []interface{}{}, Right: float64(1), Err: true},
149+
{Symbol: EQ, Left: float64(3), Right: float64(3), Result: true},
150+
{Symbol: EQ, Left: float64(3), Right: "3", Result: false},
148151
} {
149-
result, err := compareValues(testCase.Symbol, testCase.Left, testCase.Right)
152+
result, err := CompareValues(testCase.Symbol, testCase.Left, testCase.Right)
150153
if testCase.Err {
151154
assert.Error(err, "test case %d", i)
152155
} else {

0 commit comments

Comments
 (0)