| 1 | // Copyright 2018 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 pb |
| 16 | |
| 17 | import ( |
| 18 | "google.golang.org/protobuf/reflect/protoreflect" |
| 19 | |
| 20 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 21 | emptypb "google.golang.org/protobuf/types/known/emptypb" |
| 22 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | // CheckedPrimitives map from proto field descriptor type to expr.Type. |
| 27 | CheckedPrimitives = map[protoreflect.Kind]*exprpb.Type{ |
| 28 | protoreflect.BoolKind: checkedBool, |
| 29 | protoreflect.BytesKind: checkedBytes, |
| 30 | protoreflect.DoubleKind: checkedDouble, |
| 31 | protoreflect.FloatKind: checkedDouble, |
| 32 | protoreflect.Int32Kind: checkedInt, |
| 33 | protoreflect.Int64Kind: checkedInt, |
| 34 | protoreflect.Sint32Kind: checkedInt, |
| 35 | protoreflect.Sint64Kind: checkedInt, |
| 36 | protoreflect.Uint32Kind: checkedUint, |
| 37 | protoreflect.Uint64Kind: checkedUint, |
| 38 | protoreflect.Fixed32Kind: checkedUint, |
| 39 | protoreflect.Fixed64Kind: checkedUint, |
| 40 | protoreflect.Sfixed32Kind: checkedInt, |
| 41 | protoreflect.Sfixed64Kind: checkedInt, |
| 42 | protoreflect.StringKind: checkedString} |
| 43 | |
| 44 | // CheckedWellKnowns map from qualified proto type name to expr.Type for |
| 45 | // well-known proto types. |
| 46 | CheckedWellKnowns = map[string]*exprpb.Type{ |
| 47 | // Wrapper types. |
| 48 | "google.protobuf.BoolValue": checkedWrap(checkedBool), |
| 49 | "google.protobuf.BytesValue": checkedWrap(checkedBytes), |
| 50 | "google.protobuf.DoubleValue": checkedWrap(checkedDouble), |
| 51 | "google.protobuf.FloatValue": checkedWrap(checkedDouble), |
| 52 | "google.protobuf.Int64Value": checkedWrap(checkedInt), |
| 53 | "google.protobuf.Int32Value": checkedWrap(checkedInt), |
| 54 | "google.protobuf.UInt64Value": checkedWrap(checkedUint), |
| 55 | "google.protobuf.UInt32Value": checkedWrap(checkedUint), |
| 56 | "google.protobuf.StringValue": checkedWrap(checkedString), |
| 57 | // Well-known types. |
| 58 | "google.protobuf.Any": checkedAny, |
| 59 | "google.protobuf.Duration": checkedDuration, |
| 60 | "google.protobuf.Timestamp": checkedTimestamp, |
| 61 | // Json types. |
| 62 | "google.protobuf.ListValue": checkedListDyn, |
| 63 | "google.protobuf.NullValue": checkedNull, |
| 64 | "google.protobuf.Struct": checkedMapStringDyn, |
| 65 | "google.protobuf.Value": checkedDyn, |
| 66 | } |
| 67 | |
| 68 | // common types |
| 69 | checkedDyn = &exprpb.Type{TypeKind: &exprpb.Type_Dyn{Dyn: &emptypb.Empty{}}} |
| 70 | // Wrapper and primitive types. |
| 71 | checkedBool = checkedPrimitive(exprpb.Type_BOOL) |
| 72 | checkedBytes = checkedPrimitive(exprpb.Type_BYTES) |
| 73 | checkedDouble = checkedPrimitive(exprpb.Type_DOUBLE) |
| 74 | checkedInt = checkedPrimitive(exprpb.Type_INT64) |
| 75 | checkedString = checkedPrimitive(exprpb.Type_STRING) |
| 76 | checkedUint = checkedPrimitive(exprpb.Type_UINT64) |
| 77 | // Well-known type equivalents. |
| 78 | checkedAny = checkedWellKnown(exprpb.Type_ANY) |
| 79 | checkedDuration = checkedWellKnown(exprpb.Type_DURATION) |
| 80 | checkedTimestamp = checkedWellKnown(exprpb.Type_TIMESTAMP) |
| 81 | // Json-based type equivalents. |
| 82 | checkedNull = &exprpb.Type{ |
| 83 | TypeKind: &exprpb.Type_Null{ |
| 84 | Null: structpb.NullValue_NULL_VALUE}} |
| 85 | checkedListDyn = &exprpb.Type{ |
| 86 | TypeKind: &exprpb.Type_ListType_{ |
| 87 | ListType: &exprpb.Type_ListType{ElemType: checkedDyn}}} |
| 88 | checkedMapStringDyn = &exprpb.Type{ |
| 89 | TypeKind: &exprpb.Type_MapType_{ |
| 90 | MapType: &exprpb.Type_MapType{ |
| 91 | KeyType: checkedString, |
| 92 | ValueType: checkedDyn}}} |
| 93 | ) |