| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package ext |
| 16 |
|
| 17 |
import ( |
| 18 |
"math" |
| 19 |
|
| 20 |
"github.com/google/cel-go/cel" |
| 21 |
"github.com/google/cel-go/common/ast" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func Protos(options ...ProtosOption) cel.EnvOption { |
| 55 |
l := &protoLib{version: math.MaxUint32} |
| 56 |
for _, o := range options { |
| 57 |
l = o(l) |
| 58 |
} |
| 59 |
return cel.Lib(l) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
type ProtosOption func(*protoLib) *protoLib |
| 64 |
|
| 65 |
|
| 66 |
func ProtosVersion(version uint32) ProtosOption { |
| 67 |
return func(lib *protoLib) *protoLib { |
| 68 |
lib.version = version |
| 69 |
return lib |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
var ( |
| 74 |
protoNamespace = "proto" |
| 75 |
hasExtension = "hasExt" |
| 76 |
getExtension = "getExt" |
| 77 |
) |
| 78 |
|
| 79 |
type protoLib struct { |
| 80 |
version uint32 |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (protoLib) LibraryName() string { |
| 85 |
return "cel.lib.ext.protos" |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (protoLib) CompileOptions() []cel.EnvOption { |
| 90 |
return []cel.EnvOption{ |
| 91 |
cel.Macros( |
| 92 |
|
| 93 |
cel.ReceiverMacro(getExtension, 2, getProtoExt), |
| 94 |
|
| 95 |
cel.ReceiverMacro(hasExtension, 2, hasProtoExt), |
| 96 |
), |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func (protoLib) ProgramOptions() []cel.ProgramOption { |
| 102 |
return []cel.ProgramOption{} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func hasProtoExt(mef cel.MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *cel.Error) { |
| 107 |
if !macroTargetMatchesNamespace(protoNamespace, target) { |
| 108 |
return nil, nil |
| 109 |
} |
| 110 |
extensionField, err := getExtFieldName(mef, args[1]) |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
return mef.NewPresenceTest(args[0], extensionField), nil |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func getProtoExt(mef cel.MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *cel.Error) { |
| 119 |
if !macroTargetMatchesNamespace(protoNamespace, target) { |
| 120 |
return nil, nil |
| 121 |
} |
| 122 |
extFieldName, err := getExtFieldName(mef, args[1]) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
return mef.NewSelect(args[0], extFieldName), nil |
| 127 |
} |
| 128 |
|
| 129 |
func getExtFieldName(mef cel.MacroExprFactory, expr ast.Expr) (string, *cel.Error) { |
| 130 |
isValid := false |
| 131 |
extensionField := "" |
| 132 |
switch expr.Kind() { |
| 133 |
case ast.SelectKind: |
| 134 |
extensionField, isValid = validateIdentifier(expr) |
| 135 |
} |
| 136 |
if !isValid { |
| 137 |
return "", mef.NewError(expr.ID(), "invalid extension field") |
| 138 |
} |
| 139 |
return extensionField, nil |
| 140 |
} |
| 141 |
|
| 142 |
func validateIdentifier(expr ast.Expr) (string, bool) { |
| 143 |
switch expr.Kind() { |
| 144 |
case ast.IdentKind: |
| 145 |
return expr.AsIdent(), true |
| 146 |
case ast.SelectKind: |
| 147 |
sel := expr.AsSelect() |
| 148 |
if sel.IsTestOnly() { |
| 149 |
return "", false |
| 150 |
} |
| 151 |
opStr, isIdent := validateIdentifier(sel.Operand()) |
| 152 |
if !isIdent { |
| 153 |
return "", false |
| 154 |
} |
| 155 |
return opStr + "." + sel.FieldName(), true |
| 156 |
default: |
| 157 |
return "", false |
| 158 |
} |
| 159 |
} |