Skip to content

Commit

Permalink
embrace native types: evaluate objects/vectors with native child valu…
Browse files Browse the repository at this point in the history
…es instead of wrapped ones
  • Loading branch information
xrstf committed Nov 26, 2023
1 parent 4e586cd commit a6646f1
Show file tree
Hide file tree
Showing 50 changed files with 614 additions and 726 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ from GitHub.
Examples:

* `rudi '.foo' myfile.json`
* `rudi '(set .foo "bar") (set .users 42) .' myfile.json`
* `rudi '(set! .foo "bar") (set! .users 42) .' myfile.json`
* `rudi --script convert.rudi myfile.json`

`rudi` has extensive help built right into it, try running `rudi help` to get started.
Expand Down Expand Up @@ -118,7 +118,7 @@ func main() {
// functions like "if" and "and", so running with an empty function set
// is generally not advisable).
rudi.NewBuiltInFunctions(),
// decide what kind of type strictness you would like; pedantic, strict
// Decide what kind of type strictness you would like; pedantic, strict
// or humane; choose your own adventure (strict is default if you use nil
// here; humane allows conversions like 1 == "1").
coalescing.NewStrict(),
Expand Down
8 changes: 4 additions & 4 deletions pkg/deepcopy/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func clone(val any) (any, error) {
case string:
return asserted, nil
case map[string]any:
return cloneObject(asserted)
return cloneMap(asserted)
case []any:
return cloneVector(asserted)
return cloneSlice(asserted)

// pointer to Go types

Expand Down Expand Up @@ -129,7 +129,7 @@ func clone(val any) (any, error) {
}
}

func cloneVector(obj []any) ([]any, error) {
func cloneSlice(obj []any) ([]any, error) {
result := make([]any, len(obj))
for i, item := range obj {
cloned, err := clone(item)
Expand All @@ -143,7 +143,7 @@ func cloneVector(obj []any) ([]any, error) {
return result, nil
}

func cloneObject(obj map[string]any) (map[string]any, error) {
func cloneMap(obj map[string]any) (map[string]any, error) {
result := map[string]any{}
for key, value := range obj {
cloned, err := clone(value)
Expand Down
28 changes: 24 additions & 4 deletions pkg/eval/builtin/comparisons.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
return nil, fmt.Errorf("argument #0: %w", err)
}

leftData, err = types.WrapNative(leftData)
if err != nil {
return nil, fmt.Errorf("argument #0: %w", err)
}

leftValue, ok := leftData.(ast.Literal)
if !ok {
return nil, fmt.Errorf("argument #0 is not a literal, but %T", leftData)
Expand All @@ -33,6 +38,11 @@ func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
return nil, fmt.Errorf("argument #1: %w", err)
}

rightData, err = types.WrapNative(rightData)
if err != nil {
return nil, fmt.Errorf("argument #1: %w", err)
}

rightValue, ok := rightData.(ast.Literal)
if !ok {
return nil, fmt.Errorf("argument #1 is not a literal, but %T", rightData)
Expand All @@ -43,7 +53,7 @@ func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
return nil, err
}

return ast.Bool(equal), nil
return equal, nil
}

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

leftData, err = types.WrapNative(leftData)
if err != nil {
return nil, fmt.Errorf("argument #0: %w", err)
}

leftValue, ok := leftData.(ast.Literal)
if !ok {
return nil, fmt.Errorf("argument #0 is not a literal, but %T", leftData)
Expand All @@ -66,6 +81,11 @@ func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
return nil, fmt.Errorf("argument #1: %w", err)
}

rightData, err = types.WrapNative(rightData)
if err != nil {
return nil, fmt.Errorf("argument #1: %w", err)
}

rightValue, ok := rightData.(ast.Literal)
if !ok {
return nil, fmt.Errorf("argument #1 is not a literal, but %T", rightData)
Expand All @@ -76,11 +96,11 @@ func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
return nil, err
}

return ast.Bool(equal), nil
return equal, nil
}

type intProcessor func(left, right int64) (ast.Bool, error)
type floatProcessor func(left, right float64) (ast.Bool, error)
type intProcessor func(left, right int64) (bool, error)
type floatProcessor func(left, right float64) (bool, error)

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

0 comments on commit a6646f1

Please sign in to comment.