Skip to content

Commit

Permalink
Go 1.18 as minimum requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoholiveira committed Jan 5, 2025
1 parent ba20aad commit 1246eb3
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.16', '1.18', '1.19', '1.20', '1.21']
go: ['1.18', '1.19', '1.20', '1.21', '1.22', '1.23']
name: Running with Go ${{ matrix.go }}
steps:
- name: Install Go
Expand Down
32 changes: 16 additions & 16 deletions arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func (e ErrReduceDataType) Error() string {
return fmt.Sprintf("The type \"%s\" is not supported", e.dataType)
}

func filter(values, data interface{}) interface{} {
parsed := values.([]interface{})
func filter(values, data any) any {
parsed := values.([]any)

var subject interface{}
var subject any

if isSlice(parsed[0]) {
subject = parsed[0]
Expand All @@ -25,15 +25,15 @@ func filter(values, data interface{}) interface{} {
subject = apply(parsed[0], data)
}

result := make([]interface{}, 0)
result := make([]any, 0)

if subject == nil {
return result
}

logic := solveVars(parsed[1], data)

for _, value := range subject.([]interface{}) {
for _, value := range subject.([]any) {
v := parseValues(logic, value)

if isTrue(v) {
Expand All @@ -44,10 +44,10 @@ func filter(values, data interface{}) interface{} {
return result
}

func _map(values, data interface{}) interface{} {
parsed := values.([]interface{})
func _map(values, data any) any {
parsed := values.([]any)

var subject interface{}
var subject any

if isSlice(parsed[0]) {
subject = parsed[0]
Expand All @@ -57,15 +57,15 @@ func _map(values, data interface{}) interface{} {
subject = apply(parsed[0], data)
}

result := make([]interface{}, 0)
result := make([]any, 0)

if subject == nil {
return result
}

logic := solveVars(parsed[1], data)

for _, value := range subject.([]interface{}) {
for _, value := range subject.([]any) {
v := parseValues(logic, value)

if isTrue(v) || isNumber(v) || isBool(v) {
Expand All @@ -76,10 +76,10 @@ func _map(values, data interface{}) interface{} {
return result
}

func reduce(values, data interface{}) interface{} {
parsed := values.([]interface{})
func reduce(values, data any) any {
parsed := values.([]any)

var subject interface{}
var subject any

if isSlice(parsed[0]) {
subject = parsed[0]
Expand All @@ -94,7 +94,7 @@ func reduce(values, data interface{}) interface{} {
}

var (
accumulator interface{}
accumulator any
valueType string
)

Expand All @@ -120,13 +120,13 @@ func reduce(values, data interface{}) interface{} {
}
}

context := map[string]interface{}{
context := map[string]any{
"current": float64(0),
"accumulator": accumulator,
"valueType": valueType,
}

for _, value := range subject.([]interface{}) {
for _, value := range subject.([]any) {
if value == nil {
continue
}
Expand Down
10 changes: 5 additions & 5 deletions comp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// at simulate undefined in javascript
func at(values []interface{}, index int) interface{} {
func at(values []any, index int) any {
if index >= 0 && index < len(values) {
return values[index]
}
Expand All @@ -16,7 +16,7 @@ func at(values []interface{}, index int) interface{} {

type undefinedType struct{}

func toNumberForLess(v interface{}) float64 {
func toNumberForLess(v any) float64 {
switch value := v.(type) {
case nil:
return 0
Expand Down Expand Up @@ -45,7 +45,7 @@ func toNumberForLess(v interface{}) float64 {

// less reference javascript implementation
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than#description
func less(a, b interface{}) bool {
func less(a, b any) bool {
// If both values are strings, they are compared as strings,
// based on the values of the Unicode code points they contain.
if isString(a) && isString(b) {
Expand All @@ -56,7 +56,7 @@ func less(a, b interface{}) bool {
return toNumberForLess(b) > toNumberForLess(a)
}

func hardEquals(a, b interface{}) bool {
func hardEquals(a, b any) bool {
ra := reflect.ValueOf(a).Kind()
rb := reflect.ValueOf(b).Kind()

Expand All @@ -67,7 +67,7 @@ func hardEquals(a, b interface{}) bool {
return equals(a, b)
}

func equals(a, b interface{}) bool {
func equals(a, b any) bool {
// comparison to a nil value is falsy
if a == nil || b == nil {
// if a and b is nil, return true, else return falsy
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module github.com/diegoholiveira/jsonlogic/v3

go 1.14
go 1.18

require (
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 6 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df h1:GSoSVRLoBaFpOOds6QyY1L8AX7uoY+Ln3BHc22W40X0=
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df/go.mod h1:hiVxq5OP2bUGBRNS3Z/bt/reCLFNbdcST6gISi1fiOM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 16 additions & 16 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"strconv"
)

func is(obj interface{}, kind reflect.Kind) bool {
func is(obj any, kind reflect.Kind) bool {
return obj != nil && reflect.TypeOf(obj).Kind() == kind
}

func isBool(obj interface{}) bool {
func isBool(obj any) bool {
return is(obj, reflect.Bool)
}

func isString(obj interface{}) bool {
func isString(obj any) bool {
return is(obj, reflect.String)
}

func isNumber(obj interface{}) bool {
func isNumber(obj any) bool {
switch obj.(type) {
case int, float64:
return true
Expand All @@ -26,19 +26,19 @@ func isNumber(obj interface{}) bool {
}
}

func isPrimitive(obj interface{}) bool {
func isPrimitive(obj any) bool {
return isBool(obj) || isString(obj) || isNumber(obj)
}

func isMap(obj interface{}) bool {
func isMap(obj any) bool {
return is(obj, reflect.Map)
}

func isSlice(obj interface{}) bool {
func isSlice(obj any) bool {
return is(obj, reflect.Slice)
}

func isTrue(obj interface{}) bool {
func isTrue(obj any) bool {
if isBool(obj) {
return obj.(bool)
}
Expand All @@ -56,8 +56,8 @@ func isTrue(obj interface{}) bool {
return false
}

func toSliceOfNumbers(values interface{}) []float64 {
_values := values.([]interface{})
func toSliceOfNumbers(values any) []float64 {
_values := values.([]any)

numbers := make([]float64, len(_values))
for i, n := range _values {
Expand All @@ -66,26 +66,26 @@ func toSliceOfNumbers(values interface{}) []float64 {
return numbers
}

func toNumber(value interface{}) float64 {
func toNumber(value any) float64 {
if isString(value) {
w, _ := strconv.ParseFloat(value.(string), 64)

return w
}

switch value.(type) {
switch value := value.(type) {
case int:
return float64(value.(int))
return float64(value)
default:
return value.(float64)
}
}

func toString(value interface{}) string {
func toString(value any) string {
if isNumber(value) {
switch value.(type) {
switch value := value.(type) {
case int:
return strconv.FormatInt(int64(value.(int)), 10)
return strconv.FormatInt(int64(value), 10)
default:
return strconv.FormatFloat(value.(float64), 'f', -1, 64)
}
Expand Down
18 changes: 9 additions & 9 deletions internal/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (
Tests []Test
)

func convertInterfaceToReader(i interface{}) io.Reader {
func convertInterfaceToReader(i any) io.Reader {
var result bytes.Buffer

encoder := json.NewEncoder(&result)
Expand All @@ -45,7 +45,7 @@ func GetScenariosFromOfficialTestSuite() Tests {

response.Body.Close()

var scenarios []interface{}
var scenarios []any

err = json.Unmarshal(buffer, &scenarios)
if err != nil {
Expand All @@ -55,23 +55,23 @@ func GetScenariosFromOfficialTestSuite() Tests {
}

// add missing but relevant scenarios
var rule []interface{}
var rule []any

scenarios = append(scenarios,
append(rule,
make(map[string]interface{}),
make(map[string]interface{}),
make(map[string]interface{})))
make(map[string]any),
make(map[string]any),
make(map[string]any)))

for _, scenario := range scenarios {
if reflect.ValueOf(scenario).Kind() == reflect.String {
continue
}

tests = append(tests, Test{
Rule: convertInterfaceToReader(scenario.([]interface{})[0]),
Data: convertInterfaceToReader(scenario.([]interface{})[1]),
Expected: convertInterfaceToReader(scenario.([]interface{})[2]),
Rule: convertInterfaceToReader(scenario.([]any)[0]),
Data: convertInterfaceToReader(scenario.([]any)[1]),
Expected: convertInterfaceToReader(scenario.([]any)[2]),
})
}

Expand Down
Loading

0 comments on commit 1246eb3

Please sign in to comment.