Skip to content

Commit

Permalink
ParsedMapFieldValue stub out MapValue
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 671413590
  • Loading branch information
jcking authored and copybara-github committed Sep 6, 2024
1 parent 24237d2 commit 387bae9
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 1 deletion.
136 changes: 136 additions & 0 deletions common/values/parsed_map_field_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,139 @@
// limitations under the License.

#include "common/values/parsed_map_field_value.h"

#include <cstddef>
#include <memory>
#include <string>
#include <utility>

#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
#include "absl/log/die_if_null.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "common/json.h"
#include "common/value.h"
#include "internal/status_macros.h"
#include "google/protobuf/message.h"

namespace cel {

std::string ParsedMapFieldValue::DebugString() const {
if (ABSL_PREDICT_FALSE(field_ == nullptr)) {
return "INVALID";
}
return "VALID";
}

absl::Status ParsedMapFieldValue::SerializeTo(AnyToJsonConverter& converter,
absl::Cord& value) const {
return absl::UnimplementedError("SerializeTo is not yet implemented");
}

absl::StatusOr<Json> ParsedMapFieldValue::ConvertToJson(
AnyToJsonConverter& converter) const {
return absl::UnimplementedError("ConvertToJson is not yet implemented");
}

absl::StatusOr<JsonObject> ParsedMapFieldValue::ConvertToJsonObject(
AnyToJsonConverter& converter) const {
return absl::UnimplementedError("ConvertToJsonObject is not yet implemented");
}

absl::Status ParsedMapFieldValue::Equal(ValueManager& value_manager,
const Value& other,
Value& result) const {
return absl::UnimplementedError("Equal is not yet implemented");
}

absl::StatusOr<Value> ParsedMapFieldValue::Equal(ValueManager& value_manager,
const Value& other) const {
Value result;
CEL_RETURN_IF_ERROR(Equal(value_manager, other, result));
return result;
}

bool ParsedMapFieldValue::IsZeroValue() const { return IsEmpty(); }

bool ParsedMapFieldValue::IsEmpty() const { return Size() == 0; }

size_t ParsedMapFieldValue::Size() const {
ABSL_DCHECK(*this);
if (ABSL_PREDICT_FALSE(field_ == nullptr)) {
return 0;
}
return static_cast<size_t>(
GetReflectionOrDie()->FieldSize(*message_, field_));
}

absl::Status ParsedMapFieldValue::Get(ValueManager& value_manager,
const Value& key, Value& result) const {
return absl::UnimplementedError("Get is not yet implemented");
}

absl::StatusOr<Value> ParsedMapFieldValue::Get(ValueManager& value_manager,
const Value& key) const {
Value result;
CEL_RETURN_IF_ERROR(Get(value_manager, key, result));
return result;
}

absl::StatusOr<bool> ParsedMapFieldValue::Find(ValueManager& value_manager,
const Value& key,
Value& result) const {
return absl::UnimplementedError("Find is not yet implemented");
}

absl::StatusOr<std::pair<Value, bool>> ParsedMapFieldValue::Find(
ValueManager& value_manager, const Value& key) const {
Value result;
CEL_ASSIGN_OR_RETURN(auto found, Find(value_manager, key, result));
if (found) {
return std::pair{std::move(result), found};
}
return std::pair{NullValue(), found};
}

absl::Status ParsedMapFieldValue::Has(ValueManager& value_manager,
const Value& key, Value& result) const {
return absl::UnimplementedError("Has is not yet implemented");
}

absl::StatusOr<Value> ParsedMapFieldValue::Has(ValueManager& value_manager,
const Value& key) const {
Value result;
CEL_RETURN_IF_ERROR(Has(value_manager, key, result));
return result;
}

absl::Status ParsedMapFieldValue::ListKeys(ValueManager& value_manager,
ListValue& result) const {
return absl::UnimplementedError("ListKeys is not yet implemented");
}

absl::StatusOr<ListValue> ParsedMapFieldValue::ListKeys(
ValueManager& value_manager) const {
ListValue result;
CEL_RETURN_IF_ERROR(ListKeys(value_manager, result));
return result;
}

absl::Status ParsedMapFieldValue::ForEach(ValueManager& value_manager,
ForEachCallback callback) const {
return absl::UnimplementedError("ForEach is not yet implemented");
}

absl::StatusOr<absl::Nonnull<std::unique_ptr<ValueIterator>>>
ParsedMapFieldValue::NewIterator(ValueManager& value_manager) const {
return absl::UnimplementedError("NewIterator is not yet implemented");
}

absl::Nonnull<const google::protobuf::Reflection*>
ParsedMapFieldValue::GetReflectionOrDie() const {
return ABSL_DIE_IF_NULL(message_->GetReflection()); // Crash OK
}

} // namespace cel
60 changes: 60 additions & 0 deletions common/values/parsed_map_field_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_PARSED_MAP_FIELD_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_PARSED_MAP_FIELD_VALUE_H_

#include <cstddef>
#include <memory>
#include <string>
#include <utility>

#include "google/protobuf/any.pb.h"
#include "google/protobuf/struct.pb.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "common/json.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/value_kind.h"
#include "common/values/map_value_interface.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

namespace cel {

class Value;
class ValueManager;
class ValueIterator;
class ListValue;

// ParsedMapFieldValue is a MapValue over a map field of a parsed protocol
// buffer message.
Expand Down Expand Up @@ -64,6 +75,53 @@ class ParsedMapFieldValue final {

static MapType GetRuntimeType() { return MapType(); }

std::string DebugString() const;

absl::Status SerializeTo(AnyToJsonConverter& converter,
absl::Cord& value) const;

absl::StatusOr<Json> ConvertToJson(AnyToJsonConverter& converter) const;

absl::StatusOr<JsonObject> ConvertToJsonObject(
AnyToJsonConverter& converter) const;

absl::Status Equal(ValueManager& value_manager, const Value& other,
Value& result) const;
absl::StatusOr<Value> Equal(ValueManager& value_manager,
const Value& other) const;

bool IsZeroValue() const;

bool IsEmpty() const;

size_t Size() const;

absl::Status Get(ValueManager& value_manager, const Value& key,
Value& result) const;
absl::StatusOr<Value> Get(ValueManager& value_manager,
const Value& key) const;

absl::StatusOr<bool> Find(ValueManager& value_manager, const Value& key,
Value& result) const;
absl::StatusOr<std::pair<Value, bool>> Find(ValueManager& value_manager,
const Value& key) const;

absl::Status Has(ValueManager& value_manager, const Value& key,
Value& result) const;
absl::StatusOr<Value> Has(ValueManager& value_manager,
const Value& key) const;

absl::Status ListKeys(ValueManager& value_manager, ListValue& result) const;
absl::StatusOr<ListValue> ListKeys(ValueManager& value_manager) const;

using ForEachCallback = typename MapValueInterface::ForEachCallback;

absl::Status ForEach(ValueManager& value_manager,
ForEachCallback callback) const;

absl::StatusOr<absl::Nonnull<std::unique_ptr<ValueIterator>>> NewIterator(
ValueManager& value_manager) const;

// Returns `true` if `ParsedMapFieldValue` is in a valid state.
explicit operator bool() const { return field_ != nullptr; }

Expand All @@ -75,6 +133,8 @@ class ParsedMapFieldValue final {
}

private:
absl::Nonnull<const google::protobuf::Reflection*> GetReflectionOrDie() const;

Owned<const google::protobuf::Message> message_;
absl::Nullable<const google::protobuf::FieldDescriptor*> field_ = nullptr;
};
Expand Down
Loading

0 comments on commit 387bae9

Please sign in to comment.