Skip to content

Commit

Permalink
iceberg: introduce json utils
Browse files Browse the repository at this point in the history
  • Loading branch information
andrwng committed Jul 16, 2024
1 parent b4cc302 commit 18aa9e6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/v/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ v_cc_library(
SRCS
${avro_hdrs}
datatypes.cc
json_utils.cc
DEPS
v::container
)
Expand Down
72 changes: 72 additions & 0 deletions src/v/iceberg/json_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

#include "json/document.h"

#include <fmt/format.h>

#include <stdexcept>

namespace iceberg {

std::optional<std::reference_wrapper<const json::Value>>
parse_optional(const json::Value& v, const char* member_name) {
if (!v.IsObject()) {
throw std::invalid_argument(
fmt::format("Expected JSON object to parse field '{}'", member_name));
}
auto iter = v.FindMember(member_name);
if (iter == v.MemberEnd()) {
return std::nullopt;
}
return iter->value;
}

const json::Value&
parse_required(const json::Value& v, const char* member_name) {
if (!v.IsObject()) {
throw std::invalid_argument(
fmt::format("Expected JSON object to parse field '{}'", member_name));
}
auto iter = v.FindMember(member_name);
if (iter == v.MemberEnd()) {
throw std::invalid_argument(
fmt::format("No member named '{}'", member_name));
}
return iter->value;
}

ss::sstring parse_required_str(const json::Value& v, const char* member_name) {
const auto& str_json = parse_required(v, member_name);
if (!str_json.IsString()) {
throw std::invalid_argument(
fmt::format("Expected string for field '{}'", member_name));
}
return str_json.GetString();
}

int32_t parse_required_i32(const json::Value& v, const char* member_name) {
const auto& int_json = parse_required(v, member_name);
if (!int_json.IsInt()) {
throw std::invalid_argument(
fmt::format("Expected integer for field '{}'", member_name));
}
return int_json.GetInt();
}

bool parse_required_bool(const json::Value& v, const char* member_name) {
const auto& bool_json = parse_required(v, member_name);
if (!bool_json.IsBool()) {
throw std::invalid_argument(
fmt::format("Expected bool for field '{}'", member_name));
}
return bool_json.GetBool();
}

} // namespace iceberg
27 changes: 27 additions & 0 deletions src/v/iceberg/json_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0
#pragma once

#include "json/document.h"

namespace iceberg {

std::optional<std::reference_wrapper<const json::Value>>
parse_optional(const json::Value& v, const char* member_name);

const json::Value&
parse_required(const json::Value& v, const char* member_name);

ss::sstring parse_required_str(const json::Value& v, const char* member_name);

int32_t parse_required_i32(const json::Value& v, const char* member_name);

bool parse_required_bool(const json::Value& v, const char* member_name);

} // namespace iceberg

0 comments on commit 18aa9e6

Please sign in to comment.