Skip to content

Commit cbfa925

Browse files
jckingcopybara-github
authored andcommitted
Fix error value handling in create list/map/struct
PiperOrigin-RevId: 736119595
1 parent 1e2eaa5 commit cbfa925

File tree

11 files changed

+707
-505
lines changed

11 files changed

+707
-505
lines changed

common/type_reflector_test.cc

Lines changed: 186 additions & 93 deletions
Large diffs are not rendered by default.

common/value.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUE_H_
1616
#define THIRD_PARTY_CEL_CPP_COMMON_VALUE_H_
1717

18-
#include <algorithm>
18+
#include <cstddef>
1919
#include <cstdint>
2020
#include <cstring>
2121
#include <memory>
@@ -2630,6 +2630,11 @@ static_assert(std::is_nothrow_move_constructible_v<Value>);
26302630
static_assert(std::is_nothrow_move_assignable_v<Value>);
26312631
static_assert(std::is_nothrow_swappable_v<Value>);
26322632

2633+
inline absl::Status ErrorValueAssign::operator()(absl::Status status) const {
2634+
*value_ = ErrorValue(std::move(status));
2635+
return absl::OkStatus();
2636+
}
2637+
26332638
namespace common_internal {
26342639

26352640
template <typename Base>
@@ -2855,9 +2860,11 @@ class ValueBuilder {
28552860
public:
28562861
virtual ~ValueBuilder() = default;
28572862

2858-
virtual absl::Status SetFieldByName(absl::string_view name, Value value) = 0;
2863+
virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByName(
2864+
absl::string_view name, Value value) = 0;
28592865

2860-
virtual absl::Status SetFieldByNumber(int64_t number, Value value) = 0;
2866+
virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByNumber(
2867+
int64_t number, Value value) = 0;
28612868

28622869
virtual absl::StatusOr<Value> Build() && = 0;
28632870
};

common/values/error_value.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_ERROR_VALUE_H_
2020

2121
#include <cstddef>
22+
#include <memory>
2223
#include <new>
2324
#include <ostream>
2425
#include <string>
@@ -217,6 +218,25 @@ class ErrorValueReturn final {
217218
}
218219
};
219220

221+
class ErrorValueAssign final {
222+
public:
223+
ErrorValueAssign() = delete;
224+
225+
explicit ErrorValueAssign(Value& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
226+
: ErrorValueAssign(std::addressof(value)) {}
227+
228+
explicit ErrorValueAssign(
229+
absl::Nonnull<Value*> value ABSL_ATTRIBUTE_LIFETIME_BOUND)
230+
: value_(value) {
231+
ABSL_DCHECK(value != nullptr);
232+
}
233+
234+
absl::Status operator()(absl::Status status) const;
235+
236+
private:
237+
absl::Nonnull<Value*> value_;
238+
};
239+
220240
template <>
221241
struct ArenaTraits<ErrorValue> {
222242
static bool trivially_destructible(const ErrorValue& value) {

common/values/struct_value.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,11 @@ class StructValueBuilder {
483483
public:
484484
virtual ~StructValueBuilder() = default;
485485

486-
virtual absl::Status SetFieldByName(absl::string_view name, Value value) = 0;
486+
virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByName(
487+
absl::string_view name, Value value) = 0;
487488

488-
virtual absl::Status SetFieldByNumber(int64_t number, Value value) = 0;
489+
virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByNumber(
490+
int64_t number, Value value) = 0;
489491

490492
virtual absl::StatusOr<StructValue> Build() && = 0;
491493
};

0 commit comments

Comments
 (0)