Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
470 changes: 470 additions & 0 deletions common/typedef/BUILD

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions common/typedef/json_schema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_JSON_SCHEMA_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_JSON_SCHEMA_H_

#include <optional>
#include <string>

#include "absl/strings/string_view.h"
#include "common/typedef/schema.h"

namespace cel {

inline constexpr absl::string_view kJsonSchemaName = "json";

class JsonSchemaObjectProperties final : public SchemaObjectProperties {
public:
std::optional<std::string> name;
};

class JsonSchemaFieldProperties final : public SchemaFieldProperties {
public:
std::optional<std::string> name;
};

class JsonSchemaEnumProperties final : public SchemaEnumProperties {
public:
std::optional<std::string> style;
std::optional<bool> omit_unspecified;
};

class JsonSchemaEnumConstantProperties final
: public SchemaEnumConstantProperties {
public:
std::optional<std::string> name;
};

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_JSON_SCHEMA_H_
58 changes: 58 additions & 0 deletions common/typedef/json_schema.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

edition = "2023";

package cel.types;

import "common/typedef/typedef.proto";

option cc_enable_arenas = true;

// JSON Schema properties attached to an Object.
message JsonSchemaObjectProperties {
string name = 1;
}

// JSON Schema properties attached to an Object.Field.
message JsonSchemaFieldProperties {
string name = 1;
}

// JSON Schema properties attached to an Enum.
message JsonSchemaEnumProperties {
string style = 1;
bool omit_unspecified = 2;
}

// JSON Schema properties attached to an Enum.EnumConstant.
message JsonSchemaEnumConstantProperties {
string name = 1;
}

extend Object {
JsonSchemaObjectProperties json_object_schema = 1000;
}

extend Object.Field {
JsonSchemaFieldProperties json_field_schema = 1000;
}

extend Enum {
JsonSchemaEnumProperties json_enum_schema = 1000;
}

extend Enum.EnumConstant {
JsonSchemaEnumConstantProperties json_enum_constant_schema = 1000;
}
165 changes: 165 additions & 0 deletions common/typedef/json_schema_proto.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/typedef/json_schema_proto.h"

#include <memory>

#include "absl/base/nullability.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "common/typedef/json_schema.h"
#include "common/typedef/json_schema.pb.h"
#include "common/typedef/schema.h"
#include "common/typedef/typedef.pb.h"

namespace cel {

absl::StatusOr<std::unique_ptr<SchemaObjectProperties>>
JsonSchemaProto::DecodeObjectProperties(const types::Object& proto) const {
if (!proto.HasExtension(types::json_object_schema)) {
return nullptr;
}
const types::JsonSchemaObjectProperties& ext =
proto.GetExtension(types::json_object_schema);
auto result = std::make_unique<JsonSchemaObjectProperties>();
if (ext.has_name()) {
result->name = ext.name();
}
return result;
}

absl::StatusOr<std::unique_ptr<SchemaFieldProperties>>
JsonSchemaProto::DecodeFieldProperties(
const types::Object::Field& proto) const {
if (!proto.HasExtension(types::json_field_schema)) {
return nullptr;
}
const types::JsonSchemaFieldProperties& ext =
proto.GetExtension(types::json_field_schema);
auto result = std::make_unique<JsonSchemaFieldProperties>();
if (ext.has_name()) {
result->name = ext.name();
}
return result;
}

absl::StatusOr<std::unique_ptr<SchemaEnumProperties>>
JsonSchemaProto::DecodeEnumProperties(const types::Enum& proto) const {
if (!proto.HasExtension(types::json_enum_schema)) {
return nullptr;
}
const types::JsonSchemaEnumProperties& ext =
proto.GetExtension(types::json_enum_schema);
auto result = std::make_unique<JsonSchemaEnumProperties>();
if (ext.has_style()) {
result->style = ext.style();
}
if (ext.has_omit_unspecified()) {
result->omit_unspecified = ext.omit_unspecified();
}
return result;
}

absl::StatusOr<std::unique_ptr<SchemaEnumConstantProperties>>
JsonSchemaProto::DecodeEnumConstantProperties(
const types::Enum::EnumConstant& proto) const {
if (!proto.HasExtension(types::json_enum_constant_schema)) {
return nullptr;
}
const types::JsonSchemaEnumConstantProperties& ext =
proto.GetExtension(types::json_enum_constant_schema);
auto result = std::make_unique<JsonSchemaEnumConstantProperties>();
if (ext.has_name()) {
result->name = ext.name();
}
return result;
}

absl::Status JsonSchemaProto::EncodeObjectProperties(
const SchemaObjectProperties& properties,
types::Object* absl_nonnull proto) const {
const auto* props =
dynamic_cast<const JsonSchemaObjectProperties*>(&properties);
if (props == nullptr) {
return absl::InvalidArgumentError(
"Invalid object properties type for 'json' schema");
}
types::JsonSchemaObjectProperties* ext =
proto->MutableExtension(types::json_object_schema);
ext->Clear();
if (props->name.has_value()) {
ext->set_name(*props->name);
}
return absl::OkStatus();
}

absl::Status JsonSchemaProto::EncodeFieldProperties(
const SchemaFieldProperties& properties,
types::Object::Field* absl_nonnull proto) const {
const auto* props =
dynamic_cast<const JsonSchemaFieldProperties*>(&properties);
if (props == nullptr) {
return absl::InvalidArgumentError(
"Invalid field properties type for 'json' schema");
}
types::JsonSchemaFieldProperties* ext =
proto->MutableExtension(types::json_field_schema);
ext->Clear();
if (props->name.has_value()) {
ext->set_name(*props->name);
}
return absl::OkStatus();
}

absl::Status JsonSchemaProto::EncodeEnumProperties(
const SchemaEnumProperties& properties,
types::Enum* absl_nonnull proto) const {
const auto* props =
dynamic_cast<const JsonSchemaEnumProperties*>(&properties);
if (props == nullptr) {
return absl::InvalidArgumentError(
"Invalid enum properties type for 'json' schema");
}
types::JsonSchemaEnumProperties* ext =
proto->MutableExtension(types::json_enum_schema);
ext->Clear();
if (props->style.has_value()) {
ext->set_style(*props->style);
}
if (props->omit_unspecified.has_value()) {
ext->set_omit_unspecified(*props->omit_unspecified);
}
return absl::OkStatus();
}

absl::Status JsonSchemaProto::EncodeEnumConstantProperties(
const SchemaEnumConstantProperties& properties,
types::Enum::EnumConstant* absl_nonnull proto) const {
const auto* props =
dynamic_cast<const JsonSchemaEnumConstantProperties*>(&properties);
if (props == nullptr) {
return absl::InvalidArgumentError(
"Invalid enum constant properties type for 'json' schema");
}
types::JsonSchemaEnumConstantProperties* ext =
proto->MutableExtension(types::json_enum_constant_schema);
ext->Clear();
if (props->name.has_value()) {
ext->set_name(*props->name);
}
return absl::OkStatus();
}

} // namespace cel
72 changes: 72 additions & 0 deletions common/typedef/json_schema_proto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_JSON_SCHEMA_PROTO_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_JSON_SCHEMA_PROTO_H_

#include <memory>

#include "absl/base/nullability.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "common/typedef/json_schema.h"
#include "common/typedef/schema.h"
#include "common/typedef/schema_proto.h"
#include "common/typedef/typedef.pb.h"

namespace cel {

// SchemaProto implementation for JSON Schema ("json").
class JsonSchemaProto : public SchemaProto {
public:
static constexpr absl::string_view kName = kJsonSchemaName;

JsonSchemaProto() = default;

absl::string_view name() const override { return kName; }

absl::StatusOr<std::unique_ptr<SchemaObjectProperties>>
DecodeObjectProperties(const types::Object& proto) const override;

absl::StatusOr<std::unique_ptr<SchemaFieldProperties>> DecodeFieldProperties(
const types::Object::Field& proto) const override;

absl::StatusOr<std::unique_ptr<SchemaEnumProperties>> DecodeEnumProperties(
const types::Enum& proto) const override;

absl::StatusOr<std::unique_ptr<SchemaEnumConstantProperties>>
DecodeEnumConstantProperties(
const types::Enum::EnumConstant& proto) const override;

absl::Status EncodeObjectProperties(
const SchemaObjectProperties& properties,
types::Object* absl_nonnull proto) const override;

absl::Status EncodeFieldProperties(
const SchemaFieldProperties& properties,
types::Object::Field* absl_nonnull proto) const override;

absl::Status EncodeEnumProperties(
const SchemaEnumProperties& properties,
types::Enum* absl_nonnull proto) const override;

absl::Status EncodeEnumConstantProperties(
const SchemaEnumConstantProperties& properties,
types::Enum::EnumConstant* absl_nonnull proto) const override;
};

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_JSON_SCHEMA_PROTO_H_
Loading
Loading