| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 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 | // Protos returns a cel.EnvOption to configure extended macros and functions for |
| 25 | // proto manipulation. |
| 26 | // |
| 27 | // Note, all macros use the 'proto' namespace; however, at the time of macro |
| 28 | // expansion the namespace looks just like any other identifier. If you are |
| 29 | // currently using a variable named 'proto', the macro will likely work just as |
| 30 | // intended; however, there is some chance for collision. |
| 31 | // |
| 32 | // # Protos.GetExt |
| 33 | // |
| 34 | // Macro which generates a select expression that retrieves an extension field |
| 35 | // from the input proto2 syntax message. If the field is not set, the default |
| 36 | // value forthe extension field is returned according to safe-traversal semantics. |
| 37 | // |
| 38 | // proto.getExt(<msg>, <fully.qualified.extension.name>) -> <field-type> |
| 39 | // |
| 40 | // Examples: |
| 41 | // |
| 42 | // proto.getExt(msg, google.expr.proto2.test.int32_ext) // returns int value |
| 43 | // |
| 44 | // # Protos.HasExt |
| 45 | // |
| 46 | // Macro which generates a test-only select expression that determines whether |
| 47 | // an extension field is set on a proto2 syntax message. |
| 48 | // |
| 49 | // proto.hasExt(<msg>, <fully.qualified.extension.name>) -> <bool> |
| 50 | // |
| 51 | // Examples: |
| 52 | // |
| 53 | // proto.hasExt(msg, google.expr.proto2.test.int32_ext) // returns true || false |
| 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 | // ProtosOption declares a functional operator for configuring protobuf utilities. |
| 63 | type ProtosOption func(*protoLib) *protoLib |
| 64 | |
| 65 | // ProtosVersion sets the library version for extensions for protobuf utilities. |
| 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 | // LibraryName implements the SingletonLibrary interface method. |
| 84 | func (protoLib) LibraryName() string { |
| 85 | return "cel.lib.ext.protos" |
| 86 | } |
| 87 | |
| 88 | // CompileOptions implements the Library interface method. |
| 89 | func (protoLib) CompileOptions() []cel.EnvOption { |
| 90 | return []cel.EnvOption{ |
| 91 | cel.Macros( |
| 92 | // proto.getExt(msg, select_expression) |
| 93 | cel.ReceiverMacro(getExtension, 2, getProtoExt), |
| 94 | // proto.hasExt(msg, select_expression) |
| 95 | cel.ReceiverMacro(hasExtension, 2, hasProtoExt), |
| 96 | ), |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // ProgramOptions implements the Library interface method. |
| 101 | func (protoLib) ProgramOptions() []cel.ProgramOption { |
| 102 | return []cel.ProgramOption{} |
| 103 | } |
| 104 | |
| 105 | // hasProtoExt generates a test-only select expression for a fully-qualified extension name on a protobuf message. |
| 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 | // getProtoExt generates a select expression for a fully-qualified extension name on a protobuf message. |
| 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 | } |