structured_proto.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright 2024 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "absl/log/internal/structured_proto.h"
  16. #include <cstdint>
  17. #include "absl/base/config.h"
  18. #include "absl/log/internal/proto.h"
  19. #include "absl/types/span.h"
  20. #include "absl/types/variant.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace log_internal {
  24. namespace {
  25. // Handles protobuf-encoding a type contained inside
  26. // `StructuredProtoField::Varint`.
  27. struct VarintEncoderVisitor final {
  28. template <typename T>
  29. bool operator()(T value) const {
  30. return EncodeVarint(field_number, value, &buf);
  31. }
  32. uint64_t field_number;
  33. absl::Span<char>& buf;
  34. };
  35. // Handles protobuf-encoding a type contained inside
  36. // `StructuredProtoField::I64`.
  37. struct I64EncoderVisitor final {
  38. bool operator()(uint64_t value) const {
  39. return Encode64Bit(field_number, value, &buf);
  40. }
  41. bool operator()(int64_t value) const {
  42. return Encode64Bit(field_number, value, &buf);
  43. }
  44. bool operator()(double value) const {
  45. return EncodeDouble(field_number, value, &buf);
  46. }
  47. uint64_t field_number;
  48. absl::Span<char>& buf;
  49. };
  50. // Handles protobuf-encoding a type contained inside
  51. // `StructuredProtoField::I32`.
  52. struct I32EncoderVisitor final {
  53. bool operator()(uint32_t value) const {
  54. return Encode32Bit(field_number, value, &buf);
  55. }
  56. bool operator()(int32_t value) const {
  57. return Encode32Bit(field_number, value, &buf);
  58. }
  59. bool operator()(float value) const {
  60. return EncodeFloat(field_number, value, &buf);
  61. }
  62. uint64_t field_number;
  63. absl::Span<char>& buf;
  64. };
  65. // Handles protobuf-encoding a type contained inside `StructuredProtoField`.
  66. struct EncoderVisitor final {
  67. bool operator()(StructuredProtoField::Varint varint) {
  68. return absl::visit(VarintEncoderVisitor{field_number, buf}, varint);
  69. }
  70. bool operator()(StructuredProtoField::I64 i64) {
  71. return absl::visit(I64EncoderVisitor{field_number, buf}, i64);
  72. }
  73. bool operator()(StructuredProtoField::LengthDelimited length_delimited) {
  74. // No need for a visitor, since `StructuredProtoField::LengthDelimited` is
  75. // just `absl::Span<const char>`.
  76. return EncodeBytes(field_number, length_delimited, &buf);
  77. }
  78. bool operator()(StructuredProtoField::I32 i32) {
  79. return absl::visit(I32EncoderVisitor{field_number, buf}, i32);
  80. }
  81. uint64_t field_number;
  82. absl::Span<char>& buf;
  83. };
  84. } // namespace
  85. bool EncodeStructuredProtoField(StructuredProtoField field,
  86. absl::Span<char>& buf) {
  87. return absl::visit(EncoderVisitor{field.field_number, buf}, field.value);
  88. }
  89. } // namespace log_internal
  90. ABSL_NAMESPACE_END
  91. } // namespace absl