|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "checker/internal/proto_type_mask.h" |
| 16 | + |
| 17 | +#include <iterator> |
| 18 | +#include <memory> |
| 19 | +#include <set> |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "absl/container/flat_hash_map.h" |
| 24 | +#include "absl/memory/memory.h" |
| 25 | +#include "absl/status/status.h" |
| 26 | +#include "absl/status/statusor.h" |
| 27 | +#include "absl/strings/str_split.h" |
| 28 | +#include "absl/strings/string_view.h" |
| 29 | +#include "absl/strings/substitute.h" |
| 30 | +#include "absl/types/optional.h" |
| 31 | +#include "common/type.h" |
| 32 | +#include "internal/status_macros.h" |
| 33 | +#include "google/protobuf/descriptor.h" |
| 34 | +#include "util/gtl/iterator_adaptors.h" |
| 35 | + |
| 36 | +using ::google::protobuf::Descriptor; |
| 37 | +using ::google::protobuf::DescriptorPool; |
| 38 | +using ::google::protobuf::FieldDescriptor; |
| 39 | +using TypeMap = absl::flat_hash_map<std::string, std::set<std::string>>; |
| 40 | + |
| 41 | +absl::StatusOr<std::unique_ptr<ProtoTypeMask>> ProtoTypeMask::Create( |
| 42 | + const std::shared_ptr<const DescriptorPool>& descriptor_pool, |
| 43 | + const TypeMap& types_and_field_paths) { |
| 44 | + if (descriptor_pool == nullptr) { |
| 45 | + return absl::InvalidArgumentError( |
| 46 | + "ProtoTypeMask descriptor pool cannot be nullptr"); |
| 47 | + } |
| 48 | + CEL_ASSIGN_OR_RETURN( |
| 49 | + auto types_and_visible_fields, |
| 50 | + ComputeVisibleFieldsMap(descriptor_pool, types_and_field_paths)); |
| 51 | + std::unique_ptr<ProtoTypeMask> proto_type_mask = |
| 52 | + absl::WrapUnique(new ProtoTypeMask(types_and_visible_fields)); |
| 53 | + return proto_type_mask; |
| 54 | +} |
| 55 | + |
| 56 | +bool ProtoTypeMask::FieldIsVisible(absl::string_view type_name, |
| 57 | + absl::string_view field_name) { |
| 58 | + auto iterator = types_and_visible_fields_.find(type_name); |
| 59 | + if (iterator != types_and_visible_fields_.end() && |
| 60 | + !iterator->second.contains(field_name.data())) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +absl::StatusOr<const Descriptor*> ProtoTypeMask::FindMessage( |
| 67 | + const std::shared_ptr<const DescriptorPool>& descriptor_pool, |
| 68 | + absl::string_view type_name) { |
| 69 | + const Descriptor* descriptor = |
| 70 | + descriptor_pool->FindMessageTypeByName(type_name); |
| 71 | + if (descriptor == nullptr) { |
| 72 | + return absl::InvalidArgumentError(absl::Substitute( |
| 73 | + "ProtoTypeMask type not found (type name: '$0')", type_name)); |
| 74 | + } |
| 75 | + return descriptor; |
| 76 | +} |
| 77 | + |
| 78 | +absl::StatusOr<const FieldDescriptor*> ProtoTypeMask::FindField( |
| 79 | + const Descriptor* descriptor, absl::string_view field_name) { |
| 80 | + const FieldDescriptor* field_descriptor = |
| 81 | + descriptor->FindFieldByName(field_name); |
| 82 | + if (field_descriptor == nullptr) { |
| 83 | + return absl::InvalidArgumentError( |
| 84 | + absl::Substitute("ProtoTypeMask could not select field from type " |
| 85 | + "(type name: '$0', field name: '$1')", |
| 86 | + descriptor->full_name(), field_name)); |
| 87 | + } |
| 88 | + return field_descriptor; |
| 89 | +} |
| 90 | + |
| 91 | +absl::StatusOr<const Descriptor*> ProtoTypeMask::GetMessage( |
| 92 | + const FieldDescriptor* field_descriptor) { |
| 93 | + cel::MessageTypeField field(field_descriptor); |
| 94 | + cel::Type type = field.GetType(); |
| 95 | + absl::optional<cel::MessageType> message_type = type.AsMessage(); |
| 96 | + if (!message_type.has_value()) { |
| 97 | + return absl::InvalidArgumentError( |
| 98 | + absl::Substitute("ProtoTypeMask subfield is not a message type " |
| 99 | + "(field name: '$0', type: '$1')", |
| 100 | + field_descriptor->name(), type.name())); |
| 101 | + } |
| 102 | + return &(*message_type.value()); |
| 103 | +} |
| 104 | + |
| 105 | +absl::Status ProtoTypeMask::AddAllHiddenFields( |
| 106 | + TypeMap& types_and_visible_fields, absl::string_view type_name) { |
| 107 | + auto result = types_and_visible_fields.find(type_name); |
| 108 | + if (result != types_and_visible_fields.end()) { |
| 109 | + if (!result->second.empty()) { |
| 110 | + return absl::InvalidArgumentError(absl::Substitute( |
| 111 | + "ProtoTypeMask cannot insert all hidden fields when the type has " |
| 112 | + "already been inserted with a visible field (type name: '$0')", |
| 113 | + type_name)); |
| 114 | + } |
| 115 | + return absl::OkStatus(); |
| 116 | + } |
| 117 | + types_and_visible_fields.insert({type_name.data(), {}}); |
| 118 | + return absl::OkStatus(); |
| 119 | +} |
| 120 | + |
| 121 | +absl::Status ProtoTypeMask::AddVisibleField(TypeMap& types_and_visible_fields, |
| 122 | + absl::string_view type_name, |
| 123 | + absl::string_view field_name) { |
| 124 | + auto result = types_and_visible_fields.find(type_name); |
| 125 | + if (result != types_and_visible_fields.end()) { |
| 126 | + if (result->second.empty()) { |
| 127 | + return absl::InvalidArgumentError( |
| 128 | + absl::Substitute("ProtoTypeMask cannot insert a visible field when " |
| 129 | + "the type has already been " |
| 130 | + "inserted with all hidden fields (type name: " |
| 131 | + "'$0', field name: '$1')", |
| 132 | + type_name, field_name)); |
| 133 | + } |
| 134 | + result->second.insert(field_name.data()); |
| 135 | + return absl::OkStatus(); |
| 136 | + } |
| 137 | + types_and_visible_fields.insert({type_name.data(), {field_name.data()}}); |
| 138 | + return absl::OkStatus(); |
| 139 | +} |
| 140 | + |
| 141 | +absl::StatusOr<TypeMap> ProtoTypeMask::ComputeVisibleFieldsMap( |
| 142 | + const std::shared_ptr<const DescriptorPool>& descriptor_pool, |
| 143 | + const TypeMap& types_and_field_paths) { |
| 144 | + TypeMap types_and_visible_fields; |
| 145 | + for (absl::string_view type_name : gtl::key_view(types_and_field_paths)) { |
| 146 | + CEL_ASSIGN_OR_RETURN(const Descriptor* descriptor, |
| 147 | + FindMessage(descriptor_pool, type_name)); |
| 148 | + std::set<std::string> field_paths = |
| 149 | + types_and_field_paths.find(type_name)->second; |
| 150 | + if (field_paths.empty()) { |
| 151 | + CEL_RETURN_IF_ERROR(AddAllHiddenFields(types_and_visible_fields, |
| 152 | + descriptor->full_name())); |
| 153 | + } |
| 154 | + for (absl::string_view field_path : field_paths) { |
| 155 | + std::vector<std::string> field_names = absl::StrSplit(field_path, '.'); |
| 156 | + for (auto iterator = field_names.begin(); iterator != field_names.end(); |
| 157 | + ++iterator) { |
| 158 | + CEL_ASSIGN_OR_RETURN(const FieldDescriptor* field_descriptor, |
| 159 | + FindField(descriptor, *iterator)); |
| 160 | + CEL_RETURN_IF_ERROR(AddVisibleField( |
| 161 | + types_and_visible_fields, descriptor->full_name(), *iterator)); |
| 162 | + if (std::next(iterator) != field_names.end()) { |
| 163 | + CEL_ASSIGN_OR_RETURN(descriptor, GetMessage(field_descriptor)); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + return types_and_visible_fields; |
| 169 | +} |
0 commit comments