The dotted path setter should now work properly.

This commit is contained in:
David Given
2021-05-08 23:56:45 +02:00
parent 89165369b1
commit 74cb332706
5 changed files with 51 additions and 9 deletions

View File

@@ -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"];

View File

@@ -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();
}

View File

@@ -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

View File

@@ -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 <google/protobuf/text_format.h>
#include <assert.h>
#include <regex>
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;
}

16
tests/testproto.proto Normal file
View File

@@ -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;
}