1 // Copyright 2020 gRPC authors. 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 #ifndef GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_CEL_VALUE_H 16 #define GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_CEL_VALUE_H 17 18 // CelValue is a holder, capable of storing all kinds of data 19 // supported by CEL. 20 // CelValue defines explicitly typed/named getters/setters. 21 // When storing pointers to objects, CelValue does not accept ownership 22 // to them and does not control their lifecycle. Instead objects are expected 23 // to be either external to expression evaluation, and controlled beyond the 24 // scope or to be allocated and associated with some allocation/ownership 25 // controller (Arena). 26 // Usage examples: 27 // (a) For primitive types: 28 // CelValue value = CelValue::CreateInt64(1); 29 // (b) For string: 30 // std::string* msg("test"); 31 // CelValue value = CelValue::CreateString(msg); 32 33 #include <grpc/support/port_platform.h> 34 #include <stdint.h> 35 36 #include <memory> 37 #include <string> 38 #include <utility> 39 40 #include "absl/strings/string_view.h" 41 #include "absl/types/span.h" 42 43 namespace grpc_core { 44 namespace mock_cel { 45 46 // Break cyclic dependencies for container types. 47 class CelMap { 48 public: 49 CelMap() = default; 50 }; 51 52 // This is a temporary stub implementation of CEL APIs. 53 // Once gRPC imports the CEL library, this class will be removed. 54 class CelValue { 55 public: 56 // Default constructor. 57 // Creates CelValue with null data type. CelValue()58 CelValue() : CelValue(nullptr) {} 59 60 // We will use factory methods instead of public constructors 61 // The reason for this is the high risk of implicit type conversions 62 // between bool/int/pointer types. 63 // We rely on copy elision to avoid extra copying. CreateNull()64 static CelValue CreateNull() { return CelValue(nullptr); } 65 CreateInt64(int64_t)66 static CelValue CreateInt64(int64_t /*value*/) { return CreateNull(); } 67 CreateUint64(uint64_t)68 static CelValue CreateUint64(uint64_t /*value*/) { return CreateNull(); } 69 CreateStringView(absl::string_view)70 static CelValue CreateStringView(absl::string_view /*value*/) { 71 return CreateNull(); 72 } 73 CreateString(const std::string *)74 static CelValue CreateString(const std::string* /*str*/) { 75 return CreateNull(); 76 } 77 CreateMap(const CelMap *)78 static CelValue CreateMap(const CelMap* /*value*/) { return CreateNull(); } 79 80 private: 81 // Constructs CelValue wrapping value supplied as argument. 82 // Value type T should be supported by specification of ValueHolder. 83 template <class T> CelValue(T)84 explicit CelValue(T /*value*/) {} 85 }; 86 87 // CelMap implementation that uses STL map container as backing storage. 88 class ContainerBackedMapImpl : public CelMap { 89 public: 90 ContainerBackedMapImpl() = default; 91 Create(absl::Span<std::pair<CelValue,CelValue>>)92 static std::unique_ptr<CelMap> Create( 93 absl::Span<std::pair<CelValue, CelValue>> /*key_values*/) { 94 return std::make_unique<ContainerBackedMapImpl>(); 95 } 96 }; 97 98 } // namespace mock_cel 99 } // namespace grpc_core 100 101 #endif // GRPC_SRC_CORE_LIB_SECURITY_AUTHORIZATION_MOCK_CEL_CEL_VALUE_H 102