Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit a6646f1

Browse files
committed
embrace native types: evaluate objects/vectors with native child values instead of wrapped ones
1 parent 4e586cd commit a6646f1

50 files changed

Lines changed: 614 additions & 726 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ from GitHub.
6666
Examples:
6767

6868
* `rudi '.foo' myfile.json`
69-
* `rudi '(set .foo "bar") (set .users 42) .' myfile.json`
69+
* `rudi '(set! .foo "bar") (set! .users 42) .' myfile.json`
7070
* `rudi --script convert.rudi myfile.json`
7171

7272
`rudi` has extensive help built right into it, try running `rudi help` to get started.
@@ -118,7 +118,7 @@ func main() {
118118
// functions like "if" and "and", so running with an empty function set
119119
// is generally not advisable).
120120
rudi.NewBuiltInFunctions(),
121-
// decide what kind of type strictness you would like; pedantic, strict
121+
// Decide what kind of type strictness you would like; pedantic, strict
122122
// or humane; choose your own adventure (strict is default if you use nil
123123
// here; humane allows conversions like 1 == "1").
124124
coalescing.NewStrict(),

pkg/deepcopy/deepcopy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ func clone(val any) (any, error) {
6161
case string:
6262
return asserted, nil
6363
case map[string]any:
64-
return cloneObject(asserted)
64+
return cloneMap(asserted)
6565
case []any:
66-
return cloneVector(asserted)
66+
return cloneSlice(asserted)
6767

6868
// pointer to Go types
6969

@@ -129,7 +129,7 @@ func clone(val any) (any, error) {
129129
}
130130
}
131131

132-
func cloneVector(obj []any) ([]any, error) {
132+
func cloneSlice(obj []any) ([]any, error) {
133133
result := make([]any, len(obj))
134134
for i, item := range obj {
135135
cloned, err := clone(item)
@@ -143,7 +143,7 @@ func cloneVector(obj []any) ([]any, error) {
143143
return result, nil
144144
}
145145

146-
func cloneObject(obj map[string]any) (map[string]any, error) {
146+
func cloneMap(obj map[string]any) (map[string]any, error) {
147147
result := map[string]any{}
148148
for key, value := range obj {
149149
cloned, err := clone(value)

pkg/eval/builtin/comparisons.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
2323
return nil, fmt.Errorf("argument #0: %w", err)
2424
}
2525

26+
leftData, err = types.WrapNative(leftData)
27+
if err != nil {
28+
return nil, fmt.Errorf("argument #0: %w", err)
29+
}
30+
2631
leftValue, ok := leftData.(ast.Literal)
2732
if !ok {
2833
return nil, fmt.Errorf("argument #0 is not a literal, but %T", leftData)
@@ -33,6 +38,11 @@ func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
3338
return nil, fmt.Errorf("argument #1: %w", err)
3439
}
3540

41+
rightData, err = types.WrapNative(rightData)
42+
if err != nil {
43+
return nil, fmt.Errorf("argument #1: %w", err)
44+
}
45+
3646
rightValue, ok := rightData.(ast.Literal)
3747
if !ok {
3848
return nil, fmt.Errorf("argument #1 is not a literal, but %T", rightData)
@@ -43,7 +53,7 @@ func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
4353
return nil, err
4454
}
4555

46-
return ast.Bool(equal), nil
56+
return equal, nil
4757
}
4858

4959
func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
@@ -56,6 +66,11 @@ func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
5666
return nil, fmt.Errorf("argument #0: %w", err)
5767
}
5868

69+
leftData, err = types.WrapNative(leftData)
70+
if err != nil {
71+
return nil, fmt.Errorf("argument #0: %w", err)
72+
}
73+
5974
leftValue, ok := leftData.(ast.Literal)
6075
if !ok {
6176
return nil, fmt.Errorf("argument #0 is not a literal, but %T", leftData)
@@ -66,6 +81,11 @@ func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
6681
return nil, fmt.Errorf("argument #1: %w", err)
6782
}
6883

84+
rightData, err = types.WrapNative(rightData)
85+
if err != nil {
86+
return nil, fmt.Errorf("argument #1: %w", err)
87+
}
88+
6989
rightValue, ok := rightData.(ast.Literal)
7090
if !ok {
7191
return nil, fmt.Errorf("argument #1 is not a literal, but %T", rightData)
@@ -76,11 +96,11 @@ func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
7696
return nil, err
7797
}
7898

79-
return ast.Bool(equal), nil
99+
return equal, nil
80100
}
81101

82-
type intProcessor func(left, right int64) (ast.Bool, error)
83-
type floatProcessor func(left, right float64) (ast.Bool, error)
102+
type intProcessor func(left, right int64) (bool, error)
103+
type floatProcessor func(left, right float64) (bool, error)
84104

85105
func makeNumberComparatorFunc(inter intProcessor, floater floatProcessor, desc string) types.Function {
86106
return types.BasicFunction(func(ctx types.Context, args []ast.Expression) (any, error) {

0 commit comments

Comments
 (0)