Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend bf utility to support multiple payloads #288

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp/inc/bond/protocol/detail/rapidjson_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RapidJsonInputStream
}

private:
Buffer* input;
typename std::remove_reference<Buffer>::type* input;
uint8_t current;
size_t count;
};
Expand Down
8 changes: 4 additions & 4 deletions examples/cpp/core/bf/cmd_arg.bond
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ struct Options
[help("output file")]
1: string output = "stdout";

[help("guess | marshal | compact | compact2 | fast | simple | simple2")]
2: Protocol from = guess;
[help("guess | marshal | compact | compact2 | fast | simple | simple2, default guess")]
2: list<Protocol> from;

[help("json | compact | compact2 | fast | simple | simple2")]
3: Protocol to = json;

[help("include values for omitted optional fields when transcoding to json format with input schema)")]
[help("include values for omitted optional fields when transcoding to json format with input schema")]
4: bool all_fields;

[help("file with marshaled schema of the input; required when input format is simple*")]
5: string schema;
5: list<string> schema;

[help("input file")]
[naked("")]
Expand Down
63 changes: 46 additions & 17 deletions examples/cpp/core/bf/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ bond::SchemaDef LoadSchema(const std::string& file)

char c;
tryJson.Read(c);

return (c == '{')
? bond::Deserialize<bond::SchemaDef>(bond::SimpleJsonReader<InputFile>(input))
: bond::Unmarshal<bond::SchemaDef>(input);
Expand All @@ -111,9 +111,9 @@ bond::SchemaDef LoadSchema(const std::string& file)
template <typename Reader, typename Writer>
void TranscodeFromTo(Reader& reader, Writer& writer, const Options& options)
{
if (!options.schema.empty())
if (!options.schema.empty() && !options.schema.front().empty())
{
bond::SchemaDef schema(LoadSchema(options.schema));
bond::SchemaDef schema(LoadSchema(options.schema.front()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like it handles an empty string like the old code used to.
Based on my reading and testing of boost::escaped_list_separator, which is used for the cmd arg parsing, it will return an empty string for the first item in an input like --schema=,second.schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

bond::bonded<void, typename bond::ProtocolReader<typename Reader::Buffer> >(reader, bond::RuntimeSchema(schema)).Serialize(writer);
}
else
Expand All @@ -126,9 +126,9 @@ void TranscodeFromTo(Reader& reader, Writer& writer, const Options& options)
template <typename Writer>
void TranscodeFromTo(InputFile& input, Writer& writer, const Options& options)
{
if (!options.schema.empty())
if (!options.schema.empty() && !options.schema.front().empty())
{
bond::SchemaDef schema(LoadSchema(options.schema));
bond::SchemaDef schema(LoadSchema(options.schema.front()));
bond::SelectProtocolAndApply(bond::RuntimeSchema(schema), input, SerializeTo(writer));
}
else
Expand Down Expand Up @@ -193,23 +193,31 @@ bool TranscodeFrom(Reader reader, const Options& options)
}
}


bool Transcode(InputFile& input, const Options& options)
template <typename Input>
bool Transcode(Input input, const Options& options)
{
switch (options.from)
bf::Protocol from = options.from.empty() ? guess : options.from.front();

if (from == guess)
{
from = Guess(input);
std::cerr << std::endl << "Guessed " << ToString(from) << std::endl;
}

switch (from)
{
case marshal:
return TranscodeFrom(input, options);
case compact:
return TranscodeFrom(bond::CompactBinaryReader<InputFile>(input), options);
return TranscodeFrom(bond::CompactBinaryReader<Input>(input), options);
case compact2:
return TranscodeFrom(bond::CompactBinaryReader<InputFile>(input, bond::v2), options);
return TranscodeFrom(bond::CompactBinaryReader<Input>(input, bond::v2), options);
case fast:
return TranscodeFrom(bond::FastBinaryReader<InputFile>(input), options);
return TranscodeFrom(bond::FastBinaryReader<Input>(input), options);
case simple:
return TranscodeFrom(bond::SimpleBinaryReader<InputFile>(input), options);
return TranscodeFrom(bond::SimpleBinaryReader<Input>(input), options);
case simple2:
return TranscodeFrom(bond::SimpleBinaryReader<InputFile>(input, bond::v2), options);
return TranscodeFrom(bond::SimpleBinaryReader<Input>(input, bond::v2), options);
default:
return false;
}
Expand All @@ -226,11 +234,32 @@ int main(int argc, char** argv)
{
InputFile input(options.file);

if (options.from == guess)
std::cerr << "Guessed " << ToString(options.from = Guess(input)) << std::endl;
do
{
// In order to decode multiple payloads from a file we need to
// use InputFile& however that usage doesn't support marshalled
// bonded<T> in untagged protocols. As a compromise we use
// InputFile for the last payload and InputFile& otherwise.
if (options.schema.size() > 1 || options.from.size() > 1)
{
if (!Transcode<InputFile&>(input, options))
return 1;
}
else
{
if (!Transcode<InputFile>(input, options))
return 1;
}

if (!options.schema.empty())
options.schema.pop_front();

if (!options.from.empty())
options.from.pop_front();
}
while (!options.schema.empty() || !options.from.empty());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An extension, if you're interested, would be to have a way to specify that the last from/schema pair should be used to continually transcode until EOF.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea but let me think about interface for this separately...


if (Transcode(input, options))
return 0;
return 0;
}
}
catch(const std::exception& error)
Expand Down