Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flatten builtin func #680

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ var Builtins = []*Function{
Predicate: true,
Types: types(new(func([]any, func(any, any) any, any) any)),
},
{
Name: "flatten",
Func: func(args ...any) (any, error) {
if len(args) != 1 {
return nil, fmt.Errorf("invalid number of arguments for flatten (expected 1, got %d)", len(args))
}
res, err := flatten(args[0])

return any(res), err
},
Validate: func(args []reflect.Type) (reflect.Type, error) {
return validateAggregateFunc("flatten", args)
},
},
{
Name: "len",
Fast: Len,
Expand Down
6 changes: 6 additions & 0 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func TestBuiltin(t *testing.T) {
{`reduce([], 5, 0)`, 0},
{`concat(ArrayOfString, ArrayOfInt)`, []any{"foo", "bar", "baz", 1, 2, 3}},
{`concat(PtrArrayWithNil, [nil])`, []any{42, nil}},
{`flatten([[1, 2], [3, 4]])`, []any{1, 2, 3, 4}},
{`flatten([[1, 2], [3, 4], "5"])`, []any{1, 2, 3, 4, "5"}},
{`flatten([1, 2, 3, 4])`, []any{1, 2, 3, 4}},
{`flatten([1,[2,[3,4]]])`, []any{1, 2, 3, 4}},
}

for _, test := range tests {
Expand Down Expand Up @@ -236,6 +240,8 @@ func TestBuiltin_errors(t *testing.T) {
{`now(nil)`, "invalid number of arguments (expected 0, got 1)"},
{`date(nil)`, "interface {} is nil, not string (1:1)"},
{`timezone(nil)`, "cannot use nil as argument (type string) to call timezone (1:10)"},
{`flatten(nil)`, "invalid argument for flatten (type %!s(<nil>)) (1:1)"},
{`flatten([1, 2], [3, 4])`, "invalid number of arguments for flatten (expected 1, got 2) (1:1)"},
}
for _, test := range errorTests {
t.Run(test.input, func(t *testing.T) {
Expand Down
22 changes: 22 additions & 0 deletions builtin/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,25 @@ func median(args ...any) ([]float64, error) {
}
return values, nil
}

func flatten(args ...any) ([]any, error) {
var values []any

for _, arg := range args {
rv := reflect.ValueOf(deref.Deref(arg))
switch rv.Kind() {
case reflect.Array, reflect.Slice:
size := rv.Len()
for i := 0; i < size; i++ {
elems, err := flatten(rv.Index(i).Interface())
if err != nil {
return nil, err
}
values = append(values, elems...)
}
default:
values = append(values, arg)
}
}
return values, nil
}
1 change: 1 addition & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestCheck(t *testing.T) {
{"count(1..30, {# % 3 == 0}) > 0"},
{"map(1..3, {#}) == [1,2,3]"},
{"map(1..3, #index) == [0,1,2]"},
{"map(1..3, [#index]) == [[0],[1],[2]]"},
{"map(filter(ArrayOfFoo, {.Bar.Baz != ''}), {.Bar}) == []"},
{"filter(Any, {.AnyMethod()})[0] == ''"},
{"Time == Time"},
Expand Down
21 changes: 11 additions & 10 deletions docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ type Type struct {
var (
Operators = []string{"matches", "contains", "startsWith", "endsWith"}
Builtins = map[Identifier]*Type{
"true": {Kind: "bool"},
"false": {Kind: "bool"},
"len": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "int"}},
"all": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"none": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"any": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"one": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"filter": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},
"map": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},
"count": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "int"}},
"true": {Kind: "bool"},
"false": {Kind: "bool"},
"len": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "int"}},
"all": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"none": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"any": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"one": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}},
"filter": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},
"map": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},
"count": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "int"}},
"flatten": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}},

"trim": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "cutstr", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
"trimPrefix": {Kind: "func", Arguments: []*Type{{Name: "string", Kind: "string"}, {Name: "cutstr", Kind: "string"}}, Return: &Type{Name: "string", Kind: "string"}},
Expand Down
Loading