-
Notifications
You must be signed in to change notification settings - Fork 325
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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())); | ||
bond::bonded<void, typename bond::ProtocolReader<typename Reader::Buffer> >(reader, bond::RuntimeSchema(schema)).Serialize(writer); | ||
} | ||
else | ||
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch