Coverage Report

Created: 2026-11-24 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/eval/eval/create_map_step.cc
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
#include "eval/eval/create_map_step.h"
16
17
#include <cstddef>
18
#include <cstdint>
19
#include <memory>
20
#include <utility>
21
22
#include "absl/container/flat_hash_set.h"
23
#include "absl/status/status.h"
24
#include "absl/status/statusor.h"
25
#include "absl/strings/string_view.h"
26
#include "common/internal/attribute_trail.h"
27
#include "common/value.h"
28
#include "common/values/map_value_builder.h"
29
#include "eval/eval/evaluator_core.h"
30
#include "eval/eval/expression_step_base.h"
31
#include "eval/eval/expression_step_logic.h"
32
#include "internal/status_macros.h"
33
34
namespace google::api::expr::runtime {
35
36
namespace {
37
38
using ::cel::ErrorValueReturn;
39
using ::cel::MapValueBuilderPtr;
40
using ::cel::Value;
41
using ::cel::common_internal::NewMapValueBuilder;
42
using ::cel::common_internal::NewMutableMapValue;
43
44
// `CreateStruct` implementation for map.
45
class CreateStructStepForMap final : public ExpressionStepBase {
46
 public:
47
  CreateStructStepForMap(size_t entry_count,
48
                         absl::flat_hash_set<int32_t> optional_indices)
49
4.98k
      : entry_count_(entry_count),
50
4.98k
        optional_indices_(std::move(optional_indices)) {}
51
52
  void Evaluate(ExecutionFrame* frame) const override;
53
54
 private:
55
  absl::StatusOr<Value> DoEvaluate(ExecutionFrame* frame) const;
56
57
  size_t entry_count_;
58
  absl::flat_hash_set<int32_t> optional_indices_;
59
};
60
61
absl::StatusOr<Value> CreateStructStepForMap::DoEvaluate(
62
813k
    ExecutionFrame* frame) const {
63
813k
  auto args = frame->value_stack().GetSpan(2 * entry_count_);
64
65
1.57M
  for (const auto& arg : args) {
66
1.57M
    if (arg.IsError()) {
67
65.5k
      return arg;
68
65.5k
    }
69
1.57M
  }
70
71
747k
  if (frame->enable_unknowns()) {
72
0
    if (auto unknown = cel::common_internal::PartiallyIdentifyAndMergeUnknowns(
73
0
            args, frame->value_stack().GetAttributeSpan(args.size()),
74
0
            frame->unknown_tree());
75
0
        unknown.has_value()) {
76
0
      return *unknown;
77
0
    }
78
0
  }
79
80
747k
  MapValueBuilderPtr builder = NewMapValueBuilder(frame->arena());
81
747k
  builder->Reserve(entry_count_);
82
83
1.43M
  for (size_t i = 0; i < entry_count_; i += 1) {
84
720k
    const auto& map_key = args[2 * i];
85
1.47M
    CEL_RETURN_IF_ERROR(cel::CheckMapKey(map_key))
86
1.47M
        .With(ErrorValueReturn(frame->arena()));
87
690k
    const auto& map_value = args[(2 * i) + 1];
88
690k
    if (optional_indices_.contains(static_cast<int32_t>(i))) {
89
0
      if (auto optional_map_value = map_value.AsOptional();
90
0
          optional_map_value) {
91
0
        if (!optional_map_value->HasValue()) {
92
0
          continue;
93
0
        }
94
0
        Value optional_map_value_value;
95
0
        optional_map_value->Value(&optional_map_value_value);
96
0
        if (optional_map_value_value.IsError()) {
97
          // Error should never be in optional, but better safe than sorry.
98
0
          return optional_map_value_value;
99
0
        }
100
0
        CEL_RETURN_IF_ERROR(
101
0
            builder->Put(map_key, std::move(optional_map_value_value)));
102
0
      } else {
103
0
        return cel::TypeConversionError(map_value.DebugString(),
104
0
                                        "optional_type", frame->arena());
105
0
      }
106
690k
    } else {
107
690k
      CEL_RETURN_IF_ERROR(builder->Put(map_key, map_value));
108
690k
    }
109
690k
  }
110
111
717k
  return std::move(*builder).Build();
112
747k
}
113
114
813k
void CreateStructStepForMap::Evaluate(ExecutionFrame* frame) const {
115
813k
  if (frame->value_stack().size() < 2 * entry_count_) {
116
0
    frame->Abort(
117
0
        absl::InternalError("CreateStructStepForMap: stack underflow"));
118
0
    return;
119
0
  }
120
121
813k
  absl::StatusOr<Value> result = DoEvaluate(frame);
122
813k
  if (!result.ok()) {
123
50
    frame->Abort(std::move(result).status());
124
50
    return;
125
50
  }
126
127
813k
  frame->value_stack().PopAndPush(2 * entry_count_, *std::move(result));
128
813k
}
129
130
class MutableMapStep final : public ExpressionStepBase {
131
 public:
132
0
  MutableMapStep() = default;
133
134
0
  void Evaluate(ExecutionFrame* frame) const override {
135
0
    frame->value_stack().Push(cel::CustomMapValue(
136
0
        NewMutableMapValue(frame->arena()), frame->arena()));
137
0
  }
138
};
139
140
}  // namespace
141
142
absl::StatusOr<std::unique_ptr<ExpressionStepLogic>>
143
CreateCreateStructStepForMap(size_t entry_count,
144
4.98k
                             absl::flat_hash_set<int32_t> optional_indices) {
145
  // Make map-creating step.
146
4.98k
  return std::make_unique<CreateStructStepForMap>(entry_count,
147
4.98k
                                                  std::move(optional_indices));
148
4.98k
}
149
150
0
std::unique_ptr<ExpressionStepLogic> CreateMutableMapStep() {
151
0
  return std::make_unique<MutableMapStep>();
152
0
}
153
154
}  // namespace google::api::expr::runtime