Skip to content

Commit c5b9ae1

Browse files
authored
Merge pull request #168 from google/cpp-sync
Use ABSL_LOG macros over LOG macros
2 parents 51bbf36 + c1a562a commit c5b9ae1

31 files changed

+838
-396
lines changed

base/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ cc_library(
208208
"@com_google_absl//absl/types:optional",
209209
"@com_google_absl//absl/types:span",
210210
"@com_google_absl//absl/types:variant",
211+
"@com_google_absl//absl/utility",
211212
"@com_googlesource_code_re2//:re2",
212213
],
213214
)

base/type.cc

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ absl::Span<const absl::string_view> Type::aliases() const {
101101
return static_cast<const ListType*>(this)->aliases();
102102
case Kind::kMap:
103103
return static_cast<const MapType*>(this)->aliases();
104+
case Kind::kWrapper:
105+
return static_cast<const WrapperType*>(this)->aliases();
104106
default:
105107
// Everything else does not support aliases.
106108
return absl::Span<const absl::string_view>();

base/types/wrapper_type.cc

+34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "base/types/wrapper_type.h"
1616

1717
#include "absl/base/optimization.h"
18+
#include "absl/strings/string_view.h"
19+
#include "absl/types/span.h"
1820

1921
namespace cel {
2022

@@ -46,6 +48,20 @@ absl::string_view WrapperType::name() const {
4648
}
4749
}
4850

51+
absl::Span<const absl::string_view> WrapperType::aliases() const {
52+
switch (base_internal::Metadata::GetInlineVariant<Kind>(*this)) {
53+
case Kind::kDouble:
54+
return static_cast<const DoubleWrapperType*>(this)->aliases();
55+
case Kind::kInt:
56+
return static_cast<const IntWrapperType*>(this)->aliases();
57+
case Kind::kUint:
58+
return static_cast<const UintWrapperType*>(this)->aliases();
59+
default:
60+
// The other wrappers do not have aliases.
61+
return absl::Span<const absl::string_view>();
62+
}
63+
}
64+
4965
const Handle<Type>& WrapperType::wrapped() const {
5066
switch (base_internal::Metadata::GetInlineVariant<Kind>(*this)) {
5167
case Kind::kBool:
@@ -66,4 +82,22 @@ const Handle<Type>& WrapperType::wrapped() const {
6682
}
6783
}
6884

85+
absl::Span<const absl::string_view> DoubleWrapperType::aliases() const {
86+
static constexpr absl::string_view kAliases[] = {
87+
"google.protobuf.FloatValue"};
88+
return absl::MakeConstSpan(kAliases);
89+
}
90+
91+
absl::Span<const absl::string_view> IntWrapperType::aliases() const {
92+
static constexpr absl::string_view kAliases[] = {
93+
"google.protobuf.Int32Value"};
94+
return absl::MakeConstSpan(kAliases);
95+
}
96+
97+
absl::Span<const absl::string_view> UintWrapperType::aliases() const {
98+
static constexpr absl::string_view kAliases[] = {
99+
"google.protobuf.UInt32Value"};
100+
return absl::MakeConstSpan(kAliases);
101+
}
102+
69103
} // namespace cel

base/types/wrapper_type.h

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "absl/base/attributes.h"
2323
#include "absl/log/absl_check.h"
2424
#include "absl/strings/string_view.h"
25+
#include "absl/types/span.h"
2526
#include "base/internal/data.h"
2627
#include "base/kind.h"
2728
#include "base/type.h"
@@ -69,13 +70,17 @@ class WrapperType : public Type, base_internal::InlineData {
6970
const Handle<Type>& wrapped() const;
7071

7172
private:
73+
friend class Type;
7274
friend class BoolWrapperType;
7375
friend class BytesWrapperType;
7476
friend class DoubleWrapperType;
7577
friend class IntWrapperType;
7678
friend class StringWrapperType;
7779
friend class UintWrapperType;
7880

81+
// See Type::aliases().
82+
absl::Span<const absl::string_view> aliases() const;
83+
7984
using Base::Base;
8085
};
8186

@@ -189,6 +194,7 @@ class DoubleWrapperType final : public WrapperType {
189194
const Handle<DoubleType>& wrapped() const { return DoubleType::Get(); }
190195

191196
private:
197+
friend class WrapperType;
192198
friend class TypeFactory;
193199
template <size_t Size, size_t Align>
194200
friend struct base_internal::AnyData;
@@ -202,6 +208,9 @@ class DoubleWrapperType final : public WrapperType {
202208
<< base_internal::kInlineVariantShift);
203209

204210
constexpr DoubleWrapperType() : WrapperType(kMetadata) {}
211+
212+
// See Type::aliases().
213+
absl::Span<const absl::string_view> aliases() const;
205214
};
206215

207216
class IntWrapperType final : public WrapperType {
@@ -226,6 +235,7 @@ class IntWrapperType final : public WrapperType {
226235
const Handle<IntType>& wrapped() const { return IntType::Get(); }
227236

228237
private:
238+
friend class WrapperType;
229239
friend class TypeFactory;
230240
template <size_t Size, size_t Align>
231241
friend struct base_internal::AnyData;
@@ -239,6 +249,9 @@ class IntWrapperType final : public WrapperType {
239249
<< base_internal::kInlineVariantShift);
240250

241251
constexpr IntWrapperType() : WrapperType(kMetadata) {}
252+
253+
// See Type::aliases().
254+
absl::Span<const absl::string_view> aliases() const;
242255
};
243256

244257
class StringWrapperType final : public WrapperType {
@@ -300,6 +313,7 @@ class UintWrapperType final : public WrapperType {
300313
const Handle<UintType>& wrapped() const { return UintType::Get(); }
301314

302315
private:
316+
friend class WrapperType;
303317
friend class TypeFactory;
304318
template <size_t Size, size_t Align>
305319
friend struct base_internal::AnyData;
@@ -313,6 +327,9 @@ class UintWrapperType final : public WrapperType {
313327
<< base_internal::kInlineVariantShift);
314328

315329
constexpr UintWrapperType() : WrapperType(kMetadata) {}
330+
331+
// See Type::aliases().
332+
absl::Span<const absl::string_view> aliases() const;
316333
};
317334

318335
extern template class Handle<WrapperType>;

base/values/list_value_builder.h

+96-46
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
#include "absl/base/attributes.h"
2424
#include "absl/status/status.h"
2525
#include "absl/status/statusor.h"
26+
#include "absl/types/variant.h"
27+
#include "absl/utility/utility.h"
2628
#include "base/memory.h"
2729
#include "base/value_factory.h"
2830
#include "base/values/list_value.h"
31+
#include "internal/overloaded.h"
2932

3033
namespace cel {
3134

@@ -75,6 +78,53 @@ class ListValueBuilder;
7578

7679
namespace base_internal {
7780

81+
// ComposableListType is a variant which represents either the ListType or the
82+
// element Type for creating a ListType.
83+
template <typename T>
84+
using ComposableListType = absl::variant<Handle<T>, Handle<ListType>>;
85+
86+
// Create a ListType from ComposableListType.
87+
template <typename T>
88+
absl::StatusOr<Handle<ListType>> ComposeListType(
89+
ValueFactory& value_factory, ComposableListType<T>&& composable) {
90+
return absl::visit(
91+
internal::Overloaded{
92+
[&value_factory](
93+
Handle<T>&& element) -> absl::StatusOr<Handle<ListType>> {
94+
return value_factory.type_factory().CreateListType(
95+
std::move(element));
96+
},
97+
[](Handle<ListType>&& list) -> absl::StatusOr<Handle<ListType>> {
98+
return std::move(list);
99+
},
100+
},
101+
std::move(composable));
102+
}
103+
104+
template <typename List, typename DebugStringer>
105+
std::string ComposeListValueDebugString(const List& list,
106+
const DebugStringer& debug_stringer) {
107+
std::string out;
108+
out.push_back('[');
109+
auto current = list.begin();
110+
if (current != list.end()) {
111+
out.append(debug_stringer(*current));
112+
++current;
113+
for (; current != list.end(); ++current) {
114+
out.append(", ");
115+
out.append(debug_stringer(*current));
116+
}
117+
}
118+
out.push_back(']');
119+
return out;
120+
}
121+
122+
struct ComposedListType {
123+
explicit ComposedListType() = default;
124+
};
125+
126+
inline constexpr ComposedListType kComposedListType{};
127+
78128
// Implementation of ListValueBuilder. Specialized to store some value types as
79129
// C++ primitives, avoiding Handle overhead. Anything that does not have a C++
80130
// primitive is stored as Handle<Value>.
@@ -92,22 +142,22 @@ class ListValueBuilderImpl<T, void> : public ListValueBuilderInterface {
92142
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
93143
Handle<typename ValueTraits<T>::type_type> type)
94144
: ListValueBuilderInterface(value_factory),
95-
type_(std::move(type)),
145+
type_(absl::in_place_type<Handle<typename ValueTraits<T>::type_type>>,
146+
std::move(type)),
147+
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
148+
149+
ListValueBuilderImpl(
150+
ComposedListType,
151+
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
152+
Handle<ListType> type)
153+
: ListValueBuilderInterface(value_factory),
154+
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
96155
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
97156

98157
std::string DebugString() const override {
99-
size_t count = size();
100-
std::string out;
101-
out.push_back('[');
102-
if (count != 0) {
103-
out.append(storage_[0]->DebugString());
104-
for (size_t index = 1; index < count; index++) {
105-
out.append(", ");
106-
out.append(storage_[index]->DebugString());
107-
}
108-
}
109-
out.push_back(']');
110-
return out;
158+
return ComposeListValueDebugString(
159+
storage_,
160+
[](const Handle<Value>& value) { return value->DebugString(); });
111161
}
112162

113163
absl::Status Add(Handle<Value> value) override {
@@ -127,14 +177,14 @@ class ListValueBuilderImpl<T, void> : public ListValueBuilderInterface {
127177

128178
absl::StatusOr<Handle<ListValue>> Build() && override {
129179
CEL_ASSIGN_OR_RETURN(auto type,
130-
value_factory().type_factory().CreateListType(type_));
180+
ComposeListType(value_factory(), std::move(type_)));
131181
return value_factory()
132182
.template CreateListValue<base_internal::DynamicListValue>(
133183
std::move(type), std::move(storage_));
134184
}
135185

136186
private:
137-
Handle<typename ValueTraits<T>::type_type> type_;
187+
ComposableListType<typename ValueTraits<T>::type_type> type_;
138188
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
139189
};
140190

@@ -147,22 +197,21 @@ class ListValueBuilderImpl<Value, void> : public ListValueBuilderInterface {
147197
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
148198
Handle<Type> type)
149199
: ListValueBuilderInterface(value_factory),
150-
type_(std::move(type)),
200+
type_(absl::in_place_type<Handle<Type>>, std::move(type)),
201+
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
202+
203+
ListValueBuilderImpl(
204+
ComposedListType,
205+
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
206+
Handle<ListType> type)
207+
: ListValueBuilderInterface(value_factory),
208+
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
151209
storage_(Allocator<Handle<Value>>{value_factory.memory_manager()}) {}
152210

153211
std::string DebugString() const override {
154-
size_t count = size();
155-
std::string out;
156-
out.push_back('[');
157-
if (count != 0) {
158-
out.append(storage_[0]->DebugString());
159-
for (size_t index = 1; index < count; index++) {
160-
out.append(", ");
161-
out.append(storage_[index]->DebugString());
162-
}
163-
}
164-
out.push_back(']');
165-
return out;
212+
return ComposeListValueDebugString(
213+
storage_,
214+
[](const Handle<Value>& value) { return value->DebugString(); });
166215
}
167216

168217
absl::Status Add(Handle<Value> value) override {
@@ -178,14 +227,14 @@ class ListValueBuilderImpl<Value, void> : public ListValueBuilderInterface {
178227

179228
absl::StatusOr<Handle<ListValue>> Build() && override {
180229
CEL_ASSIGN_OR_RETURN(auto type,
181-
value_factory().type_factory().CreateListType(type_));
230+
ComposeListType(value_factory(), std::move(type_)));
182231
return value_factory()
183232
.template CreateListValue<base_internal::DynamicListValue>(
184233
std::move(type), std::move(storage_));
185234
}
186235

187236
private:
188-
Handle<Type> type_;
237+
ComposableListType<Type> type_;
189238
std::vector<Handle<Value>, Allocator<Handle<Value>>> storage_;
190239
};
191240

@@ -198,23 +247,22 @@ class ListValueBuilderImpl : public ListValueBuilderInterface {
198247
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
199248
Handle<typename ValueTraits<T>::type_type> type)
200249
: ListValueBuilderInterface(value_factory),
201-
type_(std::move(type)),
250+
type_(absl::in_place_type<Handle<typename ValueTraits<T>::type_type>>,
251+
std::move(type)),
252+
storage_(Allocator<U>{value_factory.memory_manager()}) {}
253+
254+
ListValueBuilderImpl(
255+
ComposedListType,
256+
ABSL_ATTRIBUTE_LIFETIME_BOUND ValueFactory& value_factory,
257+
Handle<ListType> type)
258+
: ListValueBuilderInterface(value_factory),
259+
type_(absl::in_place_type<Handle<ListType>>, std::move(type)),
202260
storage_(Allocator<U>{value_factory.memory_manager()}) {}
203261

204262
std::string DebugString() const override {
205-
using value_traits = ValueTraits<T>;
206-
size_t count = size();
207-
std::string out;
208-
out.push_back('[');
209-
if (count != 0) {
210-
out.append(value_traits::DebugString(storage_[0]));
211-
for (size_t index = 1; index < count; index++) {
212-
out.append(", ");
213-
out.append(value_traits::DebugString(storage_[index]));
214-
}
215-
}
216-
out.push_back(']');
217-
return out;
263+
return ComposeListValueDebugString(storage_, [](const U& value) {
264+
return ValueTraits<T>::DebugString(value);
265+
});
218266
}
219267

220268
absl::Status Add(Handle<Value> value) override {
@@ -236,14 +284,14 @@ class ListValueBuilderImpl : public ListValueBuilderInterface {
236284

237285
absl::StatusOr<Handle<ListValue>> Build() && override {
238286
CEL_ASSIGN_OR_RETURN(auto type,
239-
value_factory().type_factory().CreateListType(type_));
287+
ComposeListType(value_factory(), std::move(type_)));
240288
return value_factory()
241289
.template CreateListValue<base_internal::StaticListValue<T>>(
242290
std::move(type), std::move(storage_));
243291
}
244292

245293
private:
246-
Handle<typename ValueTraits<T>::type_type> type_;
294+
ComposableListType<typename ValueTraits<T>::type_type> type_;
247295
std::vector<U, Allocator<U>> storage_;
248296
};
249297

@@ -257,6 +305,8 @@ class ListValueBuilder final
257305
using Impl = base_internal::ListValueBuilderImpl<
258306
T, typename base_internal::ValueTraits<T>::underlying_type>;
259307

308+
static_assert(!std::is_same_v<T, ListValue>);
309+
260310
public:
261311
using Impl::Impl;
262312
};

0 commit comments

Comments
 (0)