Skip to content

Commit

Permalink
add trim and contains?
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Nov 23, 2023
1 parent 54dcbaa commit 34f6e49
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
16 changes: 9 additions & 7 deletions pkg/eval/builtin/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ var Functions = types.Functions{
"trim-suffix": fromStringFunc(trimSuffixFunction, 2),
"to-lower": fromStringFunc(toLowerFunction, 1),
"to-upper": fromStringFunc(toUpperFunction, 1),
"trim": fromStringFunc(trimFunction, 1),

// lists
"len": lenFunction,
"append": appendFunction,
"prepend": prependFunction,
"reverse": reverseFunction,
"range": rangeFunction,
"map": mapFunction,
"filter": filterFunction,
"len": lenFunction,
"append": appendFunction,
"prepend": prependFunction,
"reverse": reverseFunction,
"range": rangeFunction,
"map": mapFunction,
"filter": filterFunction,
"contains?": containsFunction,

// logic
"and": andFunction,
Expand Down
49 changes: 49 additions & 0 deletions pkg/eval/builtin/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package builtin
import (
"errors"
"fmt"
"strings"

"go.xrstf.de/rudi/pkg/equality"
"go.xrstf.de/rudi/pkg/eval"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/lang/ast"
Expand Down Expand Up @@ -635,3 +637,50 @@ func evalNamingVector(ctx types.Context, expr ast.Expression) (indexName string,

return indexName, valueName, nil
}

func containsFunction(ctx types.Context, args []ast.Expression) (any, error) {
if size := len(args); size < 2 {
return nil, fmt.Errorf("expected 2+ arguments, got %d", size)
}

arguments, err := evalArgs(ctx, args, 0)
if err != nil {
return nil, err
}

haystack, ok := arguments[0].(ast.Literal)
if !ok {
return nil, fmt.Errorf("argument #0 is not a literal, but %T", arguments[0])
}

needle, ok := arguments[1].(ast.Literal)
if !ok {
return nil, fmt.Errorf("argument #1 is not a literal, but %T", arguments[1])
}

if str, ok := haystack.(ast.String); ok {
if strNeedle, ok := needle.(ast.String); ok {
contains := strings.Contains(string(str), string(strNeedle))

return ast.Bool(contains), nil
}

return nil, fmt.Errorf("argument #1: expected string, got %T", needle)
}

if vec, ok := haystack.(ast.Vector); ok {
for _, val := range vec.Data {
valLiteral, err := types.WrapNative(val)
if err != nil {
return nil, fmt.Errorf("cannot compare with %T: %w", val, err)
}

equal, err := equality.StrictEqual(valLiteral, needle)
if err == nil && equal {
return ast.Bool(true), nil
}
}
}

return ast.Bool(false), nil
}
7 changes: 7 additions & 0 deletions pkg/eval/builtin/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,10 @@ func toUpperFunction(ctx types.Context, args []string) (any, error) {

return ast.String(result), nil
}

// (trim SOURCE:String)
func trimFunction(ctx types.Context, args []string) (any, error) {
result := strings.TrimSpace(args[0])

return ast.String(result), nil
}

0 comments on commit 34f6e49

Please sign in to comment.