filter.go 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. package filter
  2. import (
  3. "github.com/google/cel-go/cel"
  4. "github.com/pkg/errors"
  5. exprv1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
  6. )
  7. // MemoFilterCELAttributes are the CEL attributes for memo.
  8. var MemoFilterCELAttributes = []cel.EnvOption{
  9. cel.Variable("content", cel.StringType),
  10. // As the built-in timestamp type is deprecated, we use string type for now.
  11. // e.g., "2021-01-01T00:00:00Z"
  12. cel.Variable("create_time", cel.StringType),
  13. cel.Variable("tag", cel.StringType),
  14. cel.Variable("update_time", cel.StringType),
  15. cel.Variable("visibility", cel.StringType),
  16. }
  17. // Parse parses the filter string and returns the parsed expression.
  18. // The filter string should be a CEL expression.
  19. func Parse(filter string, opts ...cel.EnvOption) (expr *exprv1.ParsedExpr, err error) {
  20. e, err := cel.NewEnv(opts...)
  21. if err != nil {
  22. return nil, errors.Wrap(err, "failed to create CEL environment")
  23. }
  24. ast, issues := e.Compile(filter)
  25. if issues != nil {
  26. return nil, errors.Errorf("failed to compile filter: %v", issues)
  27. }
  28. return cel.AstToParsedExpr(ast)
  29. }