54inline std::string yaml_path(
const std::string& parent,
const std::string& key)
56 return parent.empty() ? key : parent +
"." + key;
60 const std::string& path);
63inline bool is_key_value_list(
const google::protobuf::FieldDescriptor*
field)
67 if (!
field->is_repeated() ||
68 field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
70 const auto* type =
field->message_type();
71 return type->field_count() == 2 && type->FindFieldByName(
"key") &&
72 type->FindFieldByName(
"value");
76 const google::protobuf::FieldDescriptor*
field,
77 const std::string& path)
79 using google::protobuf::FieldDescriptor;
80 const auto* refl =
msg.GetReflection();
81 const bool repeated =
field->is_repeated();
82 const std::string text = node.as<std::string>();
84 auto fail = [&](
const std::string& what)
85 {
throw goby::Exception(path +
": " + what +
" (got '" + text +
"')"); };
89 switch (
field->cpp_type())
91 case FieldDescriptor::CPPTYPE_INT32:
92 repeated ? refl->AddInt32(&
msg,
field, node.as<std::int32_t>())
93 : refl->SetInt32(&
msg,
field, node.as<std::int32_t>());
95 case FieldDescriptor::CPPTYPE_INT64:
96 repeated ? refl->AddInt64(&
msg,
field, node.as<std::int64_t>())
97 : refl->SetInt64(&
msg,
field, node.as<std::int64_t>());
99 case FieldDescriptor::CPPTYPE_UINT32:
100 repeated ? refl->AddUInt32(&
msg,
field, node.as<std::uint32_t>())
101 : refl->SetUInt32(&
msg,
field, node.as<std::uint32_t>());
103 case FieldDescriptor::CPPTYPE_UINT64:
104 repeated ? refl->AddUInt64(&
msg,
field, node.as<std::uint64_t>())
105 : refl->SetUInt64(&
msg,
field, node.as<std::uint64_t>());
107 case FieldDescriptor::CPPTYPE_DOUBLE:
108 repeated ? refl->AddDouble(&
msg,
field, node.as<
double>())
109 : refl->SetDouble(&
msg,
field, node.as<
double>());
111 case FieldDescriptor::CPPTYPE_FLOAT:
112 repeated ? refl->AddFloat(&
msg,
field, node.as<
float>())
113 : refl->SetFloat(&
msg,
field, node.as<
float>());
115 case FieldDescriptor::CPPTYPE_BOOL:
116 repeated ? refl->AddBool(&
msg,
field, node.as<
bool>())
117 : refl->SetBool(&
msg,
field, node.as<
bool>());
119 case FieldDescriptor::CPPTYPE_STRING:
120 repeated ? refl->AddString(&
msg,
field, text) : refl->SetString(&
msg,
field, text);
122 case FieldDescriptor::CPPTYPE_ENUM:
124 const auto* value =
field->enum_type()->FindValueByName(text);
125 if (!value && std::regex_match(text, std::regex(
"-?[0-9]+")))
126 value =
field->enum_type()->FindValueByNumber(std::stoi(text));
128 fail(
"not a value of enum " +
field->enum_type()->full_name());
129 repeated ? refl->AddEnum(&
msg,
field, value) : refl->SetEnum(&
msg,
field, value);
132 case FieldDescriptor::CPPTYPE_MESSAGE:
133 fail(
"expected a map for message field " +
field->message_type()->full_name());
136 catch (
const YAML::Exception&)
138 fail(
"not a valid " + std::string(
field->cpp_type_name()));
143 const google::protobuf::FieldDescriptor*
field,
const std::string& path)
145 using google::protobuf::FieldDescriptor;
146 const auto* refl =
msg.GetReflection();
151 if (detail::is_key_value_list(
field) && node.IsMap())
155 const auto* entry_desc =
field->message_type();
156 for (
const auto& kv : node)
158 const std::string key = kv.first.as<std::string>();
159 auto& entry = *refl->AddMessage(&
msg,
field);
160 yaml_scalar_to_field(kv.first, entry, entry_desc->FindFieldByName(
"key"),
162 yaml_to_field(kv.second, entry, entry_desc->FindFieldByName(
"value"), path +
"." + key);
167 if (
field->is_repeated())
170 if (!node.IsSequence())
172 yaml_to_field(YAML::Node(YAML::NodeType::Sequence),
msg,
field, path);
173 if (
field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE)
174 yaml_to_protobuf(node, *refl->AddMessage(&
msg,
field), path);
176 yaml_scalar_to_field(node,
msg,
field, path);
180 for (
const auto& item : node)
182 const std::string item_path = path +
"[" + std::to_string(i++) +
"]";
183 if (
field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE)
184 yaml_to_protobuf(item, *refl->AddMessage(&
msg,
field), item_path);
185 else if (item.IsScalar())
186 yaml_scalar_to_field(item,
msg,
field, item_path);
191 else if (
field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE)
193 yaml_to_protobuf(node, *refl->MutableMessage(&
msg,
field), path);
195 else if (node.IsScalar())
197 yaml_scalar_to_field(node,
msg,
field, path);
206 const std::string& path)
211 throw goby::Exception((path.empty() ? std::string(
"top level") : path) +
212 ": expected a map for message " +
msg.GetDescriptor()->full_name());
214 const auto* desc =
msg.GetDescriptor();
217 if (desc->field_count() == 1 && is_key_value_list(desc->field(0)) &&
218 !(node[desc->field(0)->name()] || node[desc->field(0)->camelcase_name()]))
220 yaml_to_field(node,
msg, desc->field(0), path);
224 for (
const auto& kv : node)
226 const std::string key = kv.first.as<std::string>();
227 const auto*
field = desc->FindFieldByName(key);
229 field = desc->FindFieldByCamelcaseName(key);
232 std::to_string(kv.first.Mark().line + 1) +
233 "): no such field in " + desc->full_name());
234 yaml_to_field(kv.second,
msg,
field, yaml_path(path, key));
248 root = YAML::Load(yaml);
250 catch (
const YAML::Exception& e)
254 detail::yaml_to_protobuf(root,
msg,
"");
273 void AddError(
int line,
int column,
const std::string& message)
override
275 errors.push_back(
"line " + std::to_string(line + 1) +
" column " +
276 std::to_string(column + 1) +
": " + message);
292 auto ends_with = [&](
const std::string& suffix)
294 return path.size() >= suffix.size() &&
295 path.compare(path.size() - suffix.size(), suffix.size(), suffix) == 0;
298 if (ends_with(
".yml") || ends_with(
".yaml"))
301 yaml_to_protobuf(contents,
msg);
303 throw goby::Exception(
"YAML support is not compiled in (install libyaml-cpp-dev and "
304 "build with -Denable_yaml=ON), or use a JSON or TextFormat file");
307 else if (ends_with(
".json"))
309 google::protobuf::util::JsonParseOptions opts;
310 auto status = google::protobuf::util::JsonStringToMessage(contents, &
msg, opts);
316 google::protobuf::TextFormat::Parser parser;
318 parser.RecordErrorsTo(&error_collector);
319 if (!parser.MergeFromString(contents, &
msg) || !error_collector.
errors.empty())
321 std::string what =
"TextFormat parse error";
322 for (
const auto& e : error_collector.
errors) what +=
"; " + e;
331 std::ifstream fin(path);
334 std::stringstream ss;