Extract singular scalars default value creation to a helper function

PiperOrigin-RevId: 582660976
pull/14753/head
Adrian Sadłocha 1 year ago committed by Copybara-Service
parent 3f636d4072
commit 02c21d566f
  1. 14
      src/google/protobuf/compiler/rust/BUILD.bazel
  2. 88
      src/google/protobuf/compiler/rust/accessors/helpers.cc
  3. 29
      src/google/protobuf/compiler/rust/accessors/helpers.h
  4. 76
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc

@ -66,6 +66,7 @@ cc_library(
strip_include_prefix = "/src", strip_include_prefix = "/src",
deps = [ deps = [
":context", ":context",
":helpers",
":naming", ":naming",
"//src/google/protobuf:protobuf_nowkt", "//src/google/protobuf:protobuf_nowkt",
"//src/google/protobuf/compiler/cpp:names_internal", "//src/google/protobuf/compiler/cpp:names_internal",
@ -142,3 +143,16 @@ cc_test(
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
) )
cc_library(
name = "helpers",
srcs = ["accessors/helpers.cc"],
hdrs = ["accessors/helpers.h"],
strip_include_prefix = "/src",
deps = [
":context",
"//src/google/protobuf:protobuf_nowkt",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/strings",
],
)

@ -0,0 +1,88 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "google/protobuf/compiler/rust/accessors/helpers.h"
#include <cmath>
#include <limits>
#include <string>
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "google/protobuf/compiler/rust/context.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/strtod.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace rust {
std::string DefaultValue(Context<FieldDescriptor> field) {
switch (field.desc().type()) {
case FieldDescriptor::TYPE_DOUBLE:
if (std::isfinite(field.desc().default_value_double())) {
return absl::StrCat(io::SimpleDtoa(field.desc().default_value_double()),
"f64");
} else if (std::isnan(field.desc().default_value_double())) {
return std::string("f64::NAN");
} else if (field.desc().default_value_double() ==
std::numeric_limits<double>::infinity()) {
return std::string("f64::INFINITY");
} else if (field.desc().default_value_double() ==
-std::numeric_limits<double>::infinity()) {
return std::string("f64::NEG_INFINITY");
} else {
ABSL_LOG(FATAL) << "unreachable";
}
case FieldDescriptor::TYPE_FLOAT:
if (std::isfinite(field.desc().default_value_float())) {
return absl::StrCat(io::SimpleFtoa(field.desc().default_value_float()),
"f32");
} else if (std::isnan(field.desc().default_value_float())) {
return std::string("f32::NAN");
} else if (field.desc().default_value_float() ==
std::numeric_limits<float>::infinity()) {
return std::string("f32::INFINITY");
} else if (field.desc().default_value_float() ==
-std::numeric_limits<float>::infinity()) {
return std::string("f32::NEG_INFINITY");
} else {
ABSL_LOG(FATAL) << "unreachable";
}
case FieldDescriptor::TYPE_INT32:
case FieldDescriptor::TYPE_SFIXED32:
case FieldDescriptor::TYPE_SINT32:
return absl::StrFormat("%d", field.desc().default_value_int32());
case FieldDescriptor::TYPE_INT64:
case FieldDescriptor::TYPE_SFIXED64:
case FieldDescriptor::TYPE_SINT64:
return absl::StrFormat("%d", field.desc().default_value_int64());
case FieldDescriptor::TYPE_FIXED64:
case FieldDescriptor::TYPE_UINT64:
return absl::StrFormat("%u", field.desc().default_value_uint64());
case FieldDescriptor::TYPE_FIXED32:
case FieldDescriptor::TYPE_UINT32:
return absl::StrFormat("%u", field.desc().default_value_uint32());
case FieldDescriptor::TYPE_BOOL:
return absl::StrFormat("%v", field.desc().default_value_bool());
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_GROUP:
case FieldDescriptor::TYPE_MESSAGE:
case FieldDescriptor::TYPE_BYTES:
case FieldDescriptor::TYPE_ENUM:
ABSL_LOG(FATAL) << "Non-singular scalar field type passed: "
<< field.desc().type_name();
}
ABSL_LOG(FATAL) << "unreachable";
}
} // namespace rust
} // namespace compiler
} // namespace protobuf
} // namespace google

@ -0,0 +1,29 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef GOOGLE_PROTOBUF_COMPILER_RUST_ACCESSORS_HELPERS_H__
#define GOOGLE_PROTOBUF_COMPILER_RUST_ACCESSORS_HELPERS_H__
#include <string>
#include "google/protobuf/compiler/rust/context.h"
#include "google/protobuf/descriptor.h"
namespace google {
namespace protobuf {
namespace compiler {
namespace rust {
// Returns the field's default value as a Rust literal / identifier.
std::string DefaultValue(Context<FieldDescriptor> field);
} // namespace rust
} // namespace compiler
} // namespace protobuf
} // namespace google
#endif // GOOGLE_PROTOBUF_COMPILER_RUST_ACCESSORS_HELPERS_H__

@ -5,20 +5,13 @@
// license that can be found in the LICENSE file or at // license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd // https://developers.google.com/open-source/licenses/bsd
#include <cmath>
#include <limits>
#include <string>
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/rust/accessors/accessor_generator.h" #include "google/protobuf/compiler/rust/accessors/accessor_generator.h"
#include "google/protobuf/compiler/rust/accessors/helpers.h"
#include "google/protobuf/compiler/rust/context.h" #include "google/protobuf/compiler/rust/context.h"
#include "google/protobuf/compiler/rust/naming.h" #include "google/protobuf/compiler/rust/naming.h"
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include "google/protobuf/io/strtod.h"
namespace google { namespace google {
namespace protobuf { namespace protobuf {
@ -31,72 +24,7 @@ void SingularScalar::InMsgImpl(Context<FieldDescriptor> field) const {
{"field", field.desc().name()}, {"field", field.desc().name()},
{"Scalar", PrimitiveRsTypeName(field.desc())}, {"Scalar", PrimitiveRsTypeName(field.desc())},
{"hazzer_thunk", Thunk(field, "has")}, {"hazzer_thunk", Thunk(field, "has")},
{"default_value", {"default_value", DefaultValue(field)},
[&] {
switch (field.desc().type()) {
case FieldDescriptor::TYPE_DOUBLE:
if (std::isfinite(field.desc().default_value_double())) {
return absl::StrCat(
io::SimpleDtoa(field.desc().default_value_double()),
"f64");
} else if (std::isnan(field.desc().default_value_double())) {
return std::string("f64::NAN");
} else if (field.desc().default_value_double() ==
std::numeric_limits<double>::infinity()) {
return std::string("f64::INFINITY");
} else if (field.desc().default_value_double() ==
-std::numeric_limits<double>::infinity()) {
return std::string("f64::NEG_INFINITY");
} else {
ABSL_LOG(FATAL) << "unreachable";
}
case FieldDescriptor::TYPE_FLOAT:
if (std::isfinite(field.desc().default_value_float())) {
return absl::StrCat(
io::SimpleFtoa(field.desc().default_value_float()),
"f32");
} else if (std::isnan(field.desc().default_value_float())) {
return std::string("f32::NAN");
} else if (field.desc().default_value_float() ==
std::numeric_limits<float>::infinity()) {
return std::string("f32::INFINITY");
} else if (field.desc().default_value_float() ==
-std::numeric_limits<float>::infinity()) {
return std::string("f32::NEG_INFINITY");
} else {
ABSL_LOG(FATAL) << "unreachable";
}
case FieldDescriptor::TYPE_INT32:
case FieldDescriptor::TYPE_SFIXED32:
case FieldDescriptor::TYPE_SINT32:
return absl::StrFormat("%d",
field.desc().default_value_int32());
case FieldDescriptor::TYPE_INT64:
case FieldDescriptor::TYPE_SFIXED64:
case FieldDescriptor::TYPE_SINT64:
return absl::StrFormat("%d",
field.desc().default_value_int64());
case FieldDescriptor::TYPE_FIXED64:
case FieldDescriptor::TYPE_UINT64:
return absl::StrFormat("%u",
field.desc().default_value_uint64());
case FieldDescriptor::TYPE_FIXED32:
case FieldDescriptor::TYPE_UINT32:
return absl::StrFormat("%u",
field.desc().default_value_uint32());
case FieldDescriptor::TYPE_BOOL:
return absl::StrFormat("%v",
field.desc().default_value_bool());
case FieldDescriptor::TYPE_STRING:
case FieldDescriptor::TYPE_GROUP:
case FieldDescriptor::TYPE_MESSAGE:
case FieldDescriptor::TYPE_BYTES:
case FieldDescriptor::TYPE_ENUM:
ABSL_LOG(FATAL) << "Non-singular scalar field type passed: "
<< field.desc().type_name();
}
ABSL_LOG(FATAL) << "unreachable";
}()},
{"getter", {"getter",
[&] { [&] {
field.Emit({}, R"rs( field.Emit({}, R"rs(

Loading…
Cancel
Save