Coverage Report

Created: 2026-11-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/values/map_value_builder.h
Line
Count
Source
1
// Copyright 2024 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
//     https://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 THIRD_PARTY_CEL_CPP_COMMON_VALUES_MAP_VALUE_BUILDER_H_
16
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_MAP_VALUE_BUILDER_H_
17
18
#include <cstddef>
19
20
#include "absl/base/attributes.h"
21
#include "absl/base/nullability.h"
22
#include "absl/status/status.h"
23
#include "absl/status/statusor.h"
24
#include "common/native_type.h"
25
#include "common/value.h"
26
#include "eval/public/cel_value.h"
27
#include "google/protobuf/arena.h"
28
#include "google/protobuf/descriptor.h"
29
#include "google/protobuf/message.h"
30
31
namespace cel {
32
33
class ValueFactory;
34
35
namespace common_internal {
36
37
// Special implementation of map which is both a modern map and legacy map. Do
38
// not try this at home. This should only be implemented in
39
// `map_value_builder.cc`.
40
class CompatMapValue : public CustomMapValueInterface,
41
                       public google::api::expr::runtime::CelMap {
42
 public:
43
  virtual google::protobuf::Arena* GetOwningArena() const = 0;
44
45
 private:
46
21
  NativeTypeId GetNativeTypeId() const final {
47
21
    return NativeTypeId::For<CompatMapValue>();
48
21
  }
49
};
50
51
const CompatMapValue* absl_nonnull EmptyCompatMapValue();
52
53
absl::StatusOr<const CompatMapValue* absl_nonnull> MakeCompatMapValue(
54
    const CustomMapValue& value,
55
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
56
    google::protobuf::MessageFactory* absl_nonnull message_factory,
57
    google::protobuf::Arena* absl_nonnull arena);
58
59
// Extension of ParsedMapValueInterface which is also mutable. Accessing this
60
// like a normal map before all entries are finished being inserted is a bug.
61
// This is primarily used by the runtime to efficiently implement comprehensions
62
// which accumulate results into a map.
63
//
64
// IMPORTANT: This type is only meant to be utilized by the runtime.
65
class MutableMapValue : public CustomMapValueInterface {
66
 public:
67
  virtual absl::Status Put(Value key, Value value) const = 0;
68
69
0
  virtual void Reserve(size_t capacity) const {}
70
71
 private:
72
0
  NativeTypeId GetNativeTypeId() const override {
73
0
    return NativeTypeId::For<MutableMapValue>();
74
0
  }
75
};
76
77
// Special implementation of map which is both a modern map, legacy map, and
78
// mutable.
79
//
80
// NOTE: We do not extend CompatMapValue to avoid having to use virtual
81
// inheritance and `dynamic_cast`.
82
class MutableCompatMapValue : public MutableMapValue,
83
                              public google::api::expr::runtime::CelMap {
84
 public:
85
  virtual google::protobuf::Arena* GetOwningArena() const = 0;
86
87
 private:
88
0
  NativeTypeId GetNativeTypeId() const final {
89
0
    return NativeTypeId::For<MutableCompatMapValue>();
90
0
  }
91
};
92
93
MutableMapValue* absl_nonnull NewMutableMapValue(
94
    google::protobuf::Arena* absl_nonnull arena);
95
96
bool IsMutableMapValue(const Value& value);
97
bool IsMutableMapValue(const MapValue& value);
98
99
const MutableMapValue* absl_nullable AsMutableMapValue(
100
    const Value& value ABSL_ATTRIBUTE_LIFETIME_BOUND);
101
const MutableMapValue* absl_nullable AsMutableMapValue(
102
    const MapValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND);
103
104
const MutableMapValue& GetMutableMapValue(
105
    const Value& value ABSL_ATTRIBUTE_LIFETIME_BOUND);
106
const MutableMapValue& GetMutableMapValue(
107
    const MapValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND);
108
109
absl_nonnull cel::MapValueBuilderPtr NewMapValueBuilder(
110
    google::protobuf::Arena* absl_nonnull arena);
111
112
}  // namespace common_internal
113
114
}  // namespace cel
115
116
#endif  // THIRD_PARTY_CEL_CPP_COMMON_VALUES_MAP_VALUE_BUILDER_H_