diff --git a/lib/config.proto b/lib/config.proto index a8de4daf..1c21b079 100644 --- a/lib/config.proto +++ b/lib/config.proto @@ -28,8 +28,6 @@ message FileEncoding { } message Config { - optional string thing = 1 [(help) = "this is some help", (parameter) = "-q", (parameter) = "--q"]; - repeated DiskEncoding disk = 2; optional FileEncoding file = 3; optional Decoder decoder = 4 [(help) = "Options for the flux decoder process"]; diff --git a/lib/proto.cc b/lib/proto.cc index 5716ee25..13456fa4 100644 --- a/lib/proto.cc +++ b/lib/proto.cc @@ -48,7 +48,20 @@ void setProtoByString(google::protobuf::Message* message, const std::string& pat Error() << fmt::format("config field '{}' in '{}' is not a message", item, path); const auto* reflection = message->GetReflection(); - message = reflection->MutableMessage(message, field); + switch (field->label()) + { + case google::protobuf::FieldDescriptor::LABEL_OPTIONAL: + message = reflection->MutableMessage(message, field); + break; + + case google::protobuf::FieldDescriptor::LABEL_REPEATED: + if (reflection->FieldSize(*message, field) == 0) + message = reflection->AddMessage(message, field); + else + message = reflection->MutableRepeatedMessage(message, field, 0); + break; + } + descriptor = message->GetDescriptor(); } diff --git a/mkninja.sh b/mkninja.sh index 6d810e72..3094eaaa 100644 --- a/mkninja.sh +++ b/mkninja.sh @@ -172,12 +172,14 @@ runtest() { shift buildlibrary lib$prog.a \ + -Idep/snowhouse/include \ "$@" buildprogram $OBJDIR/$prog \ lib$prog.a \ libbackend.a \ libproto.a \ + libtestproto.a \ libagg.a \ libfmt.a @@ -334,6 +336,9 @@ buildsimpleprogram brother240tool \ libemu.a \ libfmt.a \ +buildproto libtestproto.a \ + tests/testproto.proto \ + runtest agg-test tests/agg.cc runtest amiga-test tests/amiga.cc runtest bitaccumulator-test tests/bitaccumulator.cc diff --git a/tests/proto.cc b/tests/proto.cc index 56b9e75e..4d1539f2 100644 --- a/tests/proto.cc +++ b/tests/proto.cc @@ -1,25 +1,35 @@ #include "globals.h" #include "bytes.h" -#include "lib/config.pb.h" +#include "tests/testproto.pb.h" #include "proto.h" +#include "snowhouse/snowhouse.h" #include #include #include -static void test_proto(void) +using namespace snowhouse; + +static void test_setting(void) { - Config config; - setProtoByString(&config, "file.filename", "foo"); + TestProto config; + setProtoByString(&config, "i64", "-1"); + setProtoByString(&config, "i32", "-2"); + setProtoByString(&config, "u64", "3"); + setProtoByString(&config, "u32", "4"); + setProtoByString(&config, "d", "5.5"); + setProtoByString(&config, "m.s", "string"); + setProtoByString(&config, "r.s", "val1"); + setProtoByString(&config, "r.s", "val2"); std::string s; google::protobuf::TextFormat::PrintToString(config, &s); s = std::regex_replace(s, std::regex("[ \t\n]+"), " "); - assert(s == "file { filename: \"foo\" } "); + AssertThat(s, Equals("i64: -1 i32: -2 u64: 3 u32: 4 d: 5.5 m { s: \"string\" } r { s: \"val2\" } ")); } int main(int argc, const char* argv[]) { - test_proto(); + test_setting(); return 0; } diff --git a/tests/testproto.proto b/tests/testproto.proto new file mode 100644 index 00000000..8c7a8a55 --- /dev/null +++ b/tests/testproto.proto @@ -0,0 +1,16 @@ +syntax = "proto2"; + +message TestProto { + message SubMessage { + optional string s = 1; + } + + optional int64 i64 = 1; + optional int32 i32 = 2; + optional uint64 u64 = 3; + optional uint32 u32 = 4; + optional double d = 5; + optional SubMessage m = 6; + repeated SubMessage r = 7; +} +