expr.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package filter
  2. import (
  3. "errors"
  4. exprv1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
  5. )
  6. // GetConstValue returns the constant value of the expression.
  7. func GetConstValue(expr *exprv1.Expr) (any, error) {
  8. v, ok := expr.ExprKind.(*exprv1.Expr_ConstExpr)
  9. if !ok {
  10. return nil, errors.New("invalid constant expression")
  11. }
  12. switch v.ConstExpr.ConstantKind.(type) {
  13. case *exprv1.Constant_StringValue:
  14. return v.ConstExpr.GetStringValue(), nil
  15. case *exprv1.Constant_Int64Value:
  16. return v.ConstExpr.GetInt64Value(), nil
  17. case *exprv1.Constant_Uint64Value:
  18. return v.ConstExpr.GetUint64Value(), nil
  19. case *exprv1.Constant_DoubleValue:
  20. return v.ConstExpr.GetDoubleValue(), nil
  21. case *exprv1.Constant_BoolValue:
  22. return v.ConstExpr.GetBoolValue(), nil
  23. default:
  24. return nil, errors.New("unexpected constant type")
  25. }
  26. }
  27. // GetIdentExprName returns the name of the identifier expression.
  28. func GetIdentExprName(expr *exprv1.Expr) (string, error) {
  29. _, ok := expr.ExprKind.(*exprv1.Expr_IdentExpr)
  30. if !ok {
  31. return "", errors.New("invalid identifier expression")
  32. }
  33. return expr.GetIdentExpr().GetName(), nil
  34. }