@@ -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 "<"/"<="/">"/">="
153153const (
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" ))
0 commit comments