| 421 | } |
| 422 | |
| 423 | absl::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 | } |
no test coverage detected