MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / FormatMap

Function FormatMap

extensions/formatting.cc:166–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164}
165
166absl::StatusOr<absl::string_view> FormatMap(
167 const Value& value,
168 const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
169 google::protobuf::MessageFactory* absl_nonnull message_factory,
170 google::protobuf::Arena* absl_nonnull arena,
171 std::string& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) {
172 absl::btree_map<std::string, Value> value_map;
173 std::string value_scratch;
174 CEL_RETURN_IF_ERROR(value.GetMap().ForEach(
175 [&](const Value& key, const Value& value) -> absl::StatusOr<bool> {
176 if (key.kind() != ValueKind::kString &&
177 key.kind() != ValueKind::kBool && key.kind() != ValueKind::kInt &&
178 key.kind() != ValueKind::kUint) {
179 return absl::InvalidArgumentError(
180 absl::StrCat("map keys must be strings, booleans, integers, or "
181 "unsigned integers, was given ",
182 key.GetTypeName()));
183 }
184 value_scratch.clear();
185 CEL_ASSIGN_OR_RETURN(auto key_str,
186 FormatString(key, descriptor_pool, message_factory,
187 arena, value_scratch));
188 value_map.emplace(key_str, value);
189 return true;
190 },
191 descriptor_pool, message_factory, arena));
192
193 scratch.clear();
194 scratch.push_back('{');
195 for (const auto& [key, value] : value_map) {
196 value_scratch.clear();
197 CEL_ASSIGN_OR_RETURN(auto value_str,
198 FormatString(value, descriptor_pool, message_factory,
199 arena, value_scratch));
200 absl::StrAppend(&scratch, key, ": ");
201 absl::StrAppend(&scratch, value_str);
202 absl::StrAppend(&scratch, ", ");
203 }
204 if (scratch.size() > 1) {
205 scratch.resize(scratch.size() - 2);
206 }
207 scratch.push_back('}');
208 return scratch;
209}
210
211absl::StatusOr<absl::string_view> FormatString(
212 const Value& value,

Callers 1

FormatStringFunction · 0.85

Calls 6

clearMethod · 0.80
ForEachMethod · 0.45
GetMapMethod · 0.45
kindMethod · 0.45
GetTypeNameMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected