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

Function ParseConstantValue

env/env_yaml.cc:423–502  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

421}
422
423absl::StatusOr<Constant> ParseConstantValue(absl::string_view yaml,
424 const YAML::Node& node,
425 ConstantKindCase constant_kind_case,
426 absl::string_view value) {
427 switch (constant_kind_case) {
428 case ConstantKindCase::kNull:
429 if (!value.empty()) {
430 return YamlError(yaml, node, "Failed to parse null constant");
431 }
432 return Constant(nullptr);
433 case ConstantKindCase::kBool:
434 if (absl::EqualsIgnoreCase(value, "true")) {
435 return Constant(true);
436 } else if (absl::EqualsIgnoreCase(value, "false")) {
437 return Constant(false);
438 } else {
439 return YamlError(yaml, node, "Failed to parse bool constant");
440 }
441 case ConstantKindCase::kInt:
442 int64_t int_value;
443 if (!absl::SimpleAtoi(value, &int_value)) {
444 return YamlError(yaml, node, "Failed to parse int constant");
445 }
446 return Constant(int_value);
447 case ConstantKindCase::kUint:
448 uint64_t uint_value;
449 if (absl::EndsWith(value, "u")) {
450 value = value.substr(0, value.size() - 1);
451 }
452 if (!absl::SimpleAtoi(value, &uint_value)) {
453 return YamlError(yaml, node, "Failed to parse uint constant");
454 }
455 return Constant(uint_value);
456 case ConstantKindCase::kDouble:
457 double double_value;
458 if (!absl::SimpleAtod(value, &double_value)) {
459 return YamlError(yaml, node, "Failed to parse double constant");
460 }
461 return Constant(double_value);
462 case ConstantKindCase::kBytes: {
463 if (!IsBinary(node)) {
464 absl::StatusOr<std::string> bytes_literal =
465 internal::ParseBytesLiteral(value);
466 if (bytes_literal.ok()) {
467 return Constant(BytesConstant(*bytes_literal));
468 }
469 }
470 return Constant(BytesConstant(value));
471 }
472 case ConstantKindCase::kString:
473 return Constant(StringConstant(value));
474 case ConstantKindCase::kDuration: {
475 // Duration is deprecated as a builtin type, but still supported for
476 // compatibility.
477 absl::Duration duration_value;
478 if (!absl::ParseDuration(value, &duration_value)) {
479 return YamlError(yaml, node, "Failed to parse duration constant");
480 }

Callers 1

ParseVariableConfigsFunction · 0.85

Calls 10

YamlErrorFunction · 0.85
ConstantClass · 0.85
IsBinaryFunction · 0.85
ParseBytesLiteralFunction · 0.85
BytesConstantClass · 0.85
StringConstantClass · 0.85
ParseDurationFunction · 0.85
emptyMethod · 0.45
sizeMethod · 0.45
okMethod · 0.45

Tested by

no test coverage detected