-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Join function combines two resource names, using [resourcename.Join] from aip-go. [resourcename.Join]: https://pkg.go.dev/go.einride.tech/aip/resourcename#Join
- Loading branch information
Showing
9 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package iamcel | ||
|
||
import ( | ||
"github.com/google/cel-go/checker/decls" | ||
"github.com/google/cel-go/common/types" | ||
"github.com/google/cel-go/common/types/ref" | ||
"github.com/google/cel-go/interpreter/functions" | ||
"go.einride.tech/aip/resourcename" | ||
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1" | ||
) | ||
|
||
// JoinFunction is the name of the CEL descendant function. | ||
const JoinFunction = "join" | ||
|
||
const joinFunctionOverload = "join_string_string" | ||
|
||
// NewJoinFunctionDeclaration creates a new declaration for the descendant function. | ||
func NewJoinFunctionDeclaration() *expr.Decl { | ||
return decls.NewFunction( | ||
JoinFunction, | ||
// TODO: if ever possible in CEL-go, declare this as a variadic function. | ||
decls.NewOverload( | ||
joinFunctionOverload, | ||
[]*expr.Type{decls.String, decls.String}, | ||
decls.String, | ||
), | ||
) | ||
} | ||
|
||
// NewJoinFunctionImplementation creates a new implementation for the descendant function. | ||
func NewJoinFunctionImplementation() *functions.Overload { | ||
return &functions.Overload{ | ||
Operator: joinFunctionOverload, | ||
Binary: func(parentVal, childVal ref.Val) ref.Val { | ||
parent, ok := parentVal.Value().(string) | ||
if !ok { | ||
return types.NewErr("parent: unexpected type of arg 1, expected string but got %T", parentVal.Value()) | ||
} | ||
child, ok := childVal.Value().(string) | ||
if !ok { | ||
return types.NewErr("child: unexpected type of arg 2, expected string but got %T", childVal.Value()) | ||
} | ||
return types.String(resourcename.Join(parent, child)) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package iamcel | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/cel-go/cel" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestJoinFunction(t *testing.T) { | ||
env, err := cel.NewEnv(cel.Declarations(NewJoinFunctionDeclaration())) | ||
assert.NilError(t, err) | ||
t.Run("ok", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('parent/1', 'child/2')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1/child/2", result.Value().(string)) | ||
}) | ||
t.Run("root parent", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('/', 'child/2')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "child/2", result.Value().(string)) | ||
}) | ||
t.Run("root child", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('parent/1', '/')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1", result.Value().(string)) | ||
}) | ||
t.Run("root parent and child", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('/', '/')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "/", result.Value().(string)) | ||
}) | ||
t.Run("empty parent", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('', 'child/2')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "child/2", result.Value().(string)) | ||
}) | ||
t.Run("empty child", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('parent/1', '')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1", result.Value().(string)) | ||
}) | ||
t.Run("parent slash suffix", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('parent/1/', 'child/2')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1/child/2", result.Value().(string)) | ||
}) | ||
t.Run("child slash suffix", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('parent/1', 'child/2/')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1/child/2", result.Value().(string)) | ||
}) | ||
t.Run("parent slash prefix", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('/parent/1', 'child/2')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1/child/2", result.Value().(string)) | ||
}) | ||
t.Run("child slash prefix", func(t *testing.T) { | ||
ast, issues := env.Compile(`join('parent/1', '/child/2')`) | ||
assert.NilError(t, issues.Err()) | ||
//nolint: staticcheck // TODO: migrate to new top-level API | ||
program, err := env.Program(ast, cel.Functions(NewJoinFunctionImplementation())) | ||
assert.NilError(t, err) | ||
result, _, err := program.Eval(map[string]interface{}(nil)) | ||
assert.NilError(t, err) | ||
assert.Equal(t, "parent/1/child/2", result.Value().(string)) | ||
}) | ||
} |