Skip to content

Commit 4e47385

Browse files
Merge pull request #10 from danielgtaylor/no-deps
feat: remove unnecessary dependencies
2 parents e8452fc + 76cafd0 commit 4e47385

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

go.mod

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
module github.com/danielgtaylor/mexpr
22

33
go 1.18
4-
5-
require (
6-
github.com/stretchr/testify v1.7.0
7-
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a
8-
)
9-
10-
require (
11-
github.com/davecgh/go-spew v1.1.0 // indirect
12-
github.com/pmezard/go-difflib v1.0.0 // indirect
13-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
14-
)

go.sum

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8-
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a h1:tlXy25amD5A7gOfbXdqCGN5k8ESEed/Ee1E5RcrYnqU=
9-
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
10-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
11-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
13-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

interpreter.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package mexpr
33
import (
44
"math"
55
"strings"
6-
7-
"golang.org/x/exp/maps"
86
)
97

108
// InterpreterOption passes configuration settings when creating a new
@@ -21,6 +19,16 @@ const (
2119
UnquotedStrings
2220
)
2321

22+
// mapValues returns the values of the map m.
23+
// The values will be in an indeterminate order.
24+
func mapValues[M ~map[K]V, K comparable, V any](m M) []V {
25+
r := make([]V, 0, len(m))
26+
for _, v := range m {
27+
r = append(r, v)
28+
}
29+
return r
30+
}
31+
2432
// checkBounds returns an error if the index is out of bounds.
2533
func checkBounds(ast *Node, input any, idx int) Error {
2634
if v, ok := input.([]any); ok {
@@ -437,7 +445,7 @@ func (i *interpreter) run(ast *Node, value any) (any, Error) {
437445
return nil, nil
438446
}
439447
if m, ok := resultLeft.(map[string]any); ok {
440-
resultLeft = maps.Values(m)
448+
resultLeft = mapValues(m)
441449
}
442450
if m, ok := resultLeft.(map[any]any); ok {
443451
values := make([]any, 0, len(m))

interpreter_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package mexpr
22

33
import (
44
"encoding/json"
5+
"reflect"
56
"strings"
67
"testing"
7-
8-
"github.com/stretchr/testify/assert"
98
)
109

1110
func TestInterpreter(t *testing.T) {
@@ -224,7 +223,9 @@ func TestInterpreter(t *testing.T) {
224223
if err != nil {
225224
t.Fatal(err.Pretty(tc.expr))
226225
}
227-
assert.Equal(t, tc.output, result)
226+
if !reflect.DeepEqual(tc.output, result) {
227+
t.Fatalf("expected %v but found %v", tc.output, result)
228+
}
228229
}
229230
})
230231
}
@@ -283,7 +284,9 @@ func Benchmark(b *testing.B) {
283284
ast, _ := Parse(bm.mexpr, input)
284285
r, _ = Run(ast, input, StrictMode)
285286
}
286-
assert.Equal(b, bm.result, r)
287+
if !reflect.DeepEqual(bm.result, r) {
288+
b.Fatalf("expected %v but found %v", bm.result, r)
289+
}
287290
})
288291

289292
// b.Run(" expr-"+bm.name+"-slow", func(b *testing.B) {
@@ -299,13 +302,17 @@ func Benchmark(b *testing.B) {
299302
b.Run("mexpr-"+bm.name+"-cached", func(b *testing.B) {
300303
b.ReportAllocs()
301304
ast, err := Parse(bm.mexpr, input)
302-
assert.NoError(b, err)
305+
if err != nil {
306+
b.Fatal(err)
307+
}
303308
i := NewInterpreter(ast)
304309
b.ResetTimer()
305310
for n := 0; n < b.N; n++ {
306311
r, _ = i.Run(input)
307312
}
308-
assert.Equal(b, bm.result, r)
313+
if !reflect.DeepEqual(bm.result, r) {
314+
b.Fatalf("expected %v but found %v", bm.result, r)
315+
}
309316
})
310317

311318
// b.Run(" expr-"+bm.name+"-cached", func(b *testing.B) {

typecheck.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"sort"
66
"strings"
7-
8-
"golang.org/x/exp/maps"
97
)
108

119
type valueType string
@@ -19,6 +17,16 @@ const (
1917
typeObject valueType = "object"
2018
)
2119

20+
// mapKeys returns the keys of the map m.
21+
// The keys will be in an indeterminate order.
22+
func mapKeys[M ~map[K]V, K comparable, V any](m M) []K {
23+
r := make([]K, 0, len(m))
24+
for k := range m {
25+
r = append(r, k)
26+
}
27+
return r
28+
}
29+
2230
type schema struct {
2331
typeName valueType
2432
items *schema
@@ -30,7 +38,7 @@ func (s *schema) String() string {
3038
return fmt.Sprintf("%s[%s]", s.typeName, s.items)
3139
}
3240
if s.isObject() {
33-
return fmt.Sprintf("%s{%v}", s.typeName, maps.Keys(s.properties))
41+
return fmt.Sprintf("%s{%v}", s.typeName, mapKeys(s.properties))
3442
}
3543
return string(s.typeName)
3644
}
@@ -285,7 +293,7 @@ func (i *typeChecker) run(ast *Node, value any) (*schema, Error) {
285293
return nil, err
286294
}
287295
if leftType.isObject() {
288-
keys := maps.Keys(leftType.properties)
296+
keys := mapKeys(leftType.properties)
289297
sort.Strings(keys)
290298
if len(keys) > 0 {
291299
// Pick the first prop as the representative item type.

0 commit comments

Comments
 (0)