Fokko commented on code in PR #31: URL: https://github.com/apache/iceberg-cpp/pull/31#discussion_r1940544261
########## src/iceberg/type.cc: ########## @@ -0,0 +1,312 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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 "iceberg/type.h" + +#include <format> +#include <iterator> +#include <stdexcept> + +#include "iceberg/util/formatter.h" + +namespace iceberg { + +StructType::StructType(std::vector<SchemaField> fields) : fields_(std::move(fields)) { + size_t index = 0; + for (const auto& field : fields_) { + auto [it, inserted] = field_id_to_index_.try_emplace(field.field_id(), index); + if (!inserted) { + throw std::runtime_error( + std::format("StructType: duplicate field ID {} (field indices {} and {})", + field.field_id(), it->second, index)); + } + + ++index; + } +} + +TypeId StructType::type_id() const { return TypeId::kStruct; } +std::string StructType::ToString() const { + std::string repr = "struct<\n"; + for (const auto& field : fields_) { + std::format_to(std::back_inserter(repr), " {}\n", field); + } + repr += ">"; + return repr; +} +std::span<const SchemaField> StructType::fields() const { return fields_; } +std::optional<std::reference_wrapper<const SchemaField>> StructType::GetFieldById( + int32_t field_id) const { + auto it = field_id_to_index_.find(field_id); + if (it == field_id_to_index_.end()) return std::nullopt; + return fields_[it->second]; +} +std::optional<std::reference_wrapper<const SchemaField>> StructType::GetFieldByIndex( + int32_t index) const { + if (index < 0 || index >= static_cast<int>(fields_.size())) { + return std::nullopt; + } + return fields_[index]; +} +std::optional<std::reference_wrapper<const SchemaField>> StructType::GetFieldByName( + std::string_view name) const { + // TODO: what is the right behavior if there are duplicate names? (Are Review Comment: The spec describes the table format itself, for the behavioral things, we tend to check the Java implementation (considered the reference implementation). If we would need to formalize all this as well, then the spec would become unreadable. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org