-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): add list skip / CEL filters, test db (#60)
To provide an illustrative example of CEL's usage as a filter language, implement it, along with filtering and sending data along to a functional database (in this case, sqlite). A small example cel2sql parser was written to illustrate the ability to use CEL as a query language. However, this will be replaced by some cel -> sql parser, hopefully contributed as a more general library and dependency. Example: ``` curl $'http://localhost:8081/publishers?filter=description.startsWith(\'tomorrow\')' {"results":[{"description":"tomorrow they will write again","path":"publishers/foo"}],"nextPageToken":""} ``` Fixes #59
- Loading branch information
1 parent
a521231
commit 6fa792d
Showing
19 changed files
with
1,455 additions
and
375 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,29 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/aep-dev/aepc/pkg/cel2ansisql" | ||
"github.com/google/cel-go/cel" | ||
) | ||
|
||
func convertCELToSQL(expr string) (string, error) { | ||
if expr == "" { | ||
return "", nil | ||
} | ||
env, err := cel.NewEnv( | ||
cel.Variable("path", cel.StringType), | ||
cel.Variable("description", cel.StringType), | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
ast, iss := env.Compile(expr) | ||
if iss.Err() != nil { | ||
return "", iss.Err() | ||
} | ||
|
||
sql, err := cel2ansisql.ConvertToSQL(ast) | ||
if err != nil { | ||
return "", err | ||
} | ||
return sql, nil | ||
} |
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,36 @@ | ||
package service | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestCELToSQL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "simple expression", | ||
input: "description.startsWith('tomorrow')", | ||
expected: "description LIKE CONCAT('tomorrow', '%')", | ||
}, | ||
{ | ||
name: "empty", | ||
input: "", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := convertCELToSQL(tt.input) | ||
if err != nil { | ||
t.Errorf("convertCELToSQL() = %v, want %v", err, tt.expected) | ||
} | ||
if got != tt.expected { | ||
t.Errorf("convertCELToSQL() = %v, want %v", got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.