Commit 2444c9eb authored by Kenton Varda's avatar Kenton Varda Committed by GitHub

Merge pull request #536 from zombiezen/convertcanonical

Add canonical format to capnp convert
parents ea89b50d 33cb4f43
...@@ -127,6 +127,7 @@ public: ...@@ -127,6 +127,7 @@ public:
" packed packed binary format (deflates zeroes)\n" " packed packed binary format (deflates zeroes)\n"
" flat binary single segment, no segment table (rare)\n" " flat binary single segment, no segment table (rare)\n"
" flat-packed flat and packed\n" " flat-packed flat and packed\n"
" canonical canonicalized binary single segment, no segment table\n"
" text schema language struct literal format\n" " text schema language struct literal format\n"
" json JSON format\n" " json JSON format\n"
"When using \"text\" or \"json\" format, you must specify <schema-file> and <type> " "When using \"text\" or \"json\" format, you must specify <schema-file> and <type> "
...@@ -641,6 +642,7 @@ private: ...@@ -641,6 +642,7 @@ private:
PACKED, PACKED,
FLAT, FLAT,
FLAT_PACKED, FLAT_PACKED,
CANONICAL,
TEXT, TEXT,
JSON JSON
}; };
...@@ -650,6 +652,7 @@ private: ...@@ -650,6 +652,7 @@ private:
if (name == "packed" ) return Format::PACKED; if (name == "packed" ) return Format::PACKED;
if (name == "flat" ) return Format::FLAT; if (name == "flat" ) return Format::FLAT;
if (name == "flat-packed") return Format::FLAT_PACKED; if (name == "flat-packed") return Format::FLAT_PACKED;
if (name == "canonical" ) return Format::CANONICAL;
if (name == "text" ) return Format::TEXT; if (name == "text" ) return Format::TEXT;
if (name == "json" ) return Format::JSON; if (name == "json" ) return Format::JSON;
...@@ -662,6 +665,7 @@ private: ...@@ -662,6 +665,7 @@ private:
case Format::PACKED : return "packed"; case Format::PACKED : return "packed";
case Format::FLAT : return "flat"; case Format::FLAT : return "flat";
case Format::FLAT_PACKED: return "flat-packed"; case Format::FLAT_PACKED: return "flat-packed";
case Format::CANONICAL : return "canonical";
case Format::TEXT : return "text"; case Format::TEXT : return "text";
case Format::JSON : return "json"; case Format::JSON : return "json";
} }
...@@ -989,7 +993,8 @@ private: ...@@ -989,7 +993,8 @@ private:
capnp::PackedMessageReader message(input, options); capnp::PackedMessageReader message(input, options);
return writeConversion(message.getRoot<AnyStruct>(), output); return writeConversion(message.getRoot<AnyStruct>(), output);
} }
case Format::FLAT: { case Format::FLAT:
case Format::CANONICAL: {
auto allBytes = readAll(input); auto allBytes = readAll(input);
// Technically we don't know if the bytes are aligned so we'd better copy them to a new // Technically we don't know if the bytes are aligned so we'd better copy them to a new
...@@ -1001,6 +1006,9 @@ private: ...@@ -1001,6 +1006,9 @@ private:
kj::ArrayPtr<const word> segments[1] = { words }; kj::ArrayPtr<const word> segments[1] = { words };
SegmentArrayMessageReader message(segments, options); SegmentArrayMessageReader message(segments, options);
if (convertFrom == Format::CANONICAL) {
KJ_REQUIRE(message.isCanonical());
}
return writeConversion(message.getRoot<AnyStruct>(), output); return writeConversion(message.getRoot<AnyStruct>(), output);
} }
case Format::FLAT_PACKED: { case Format::FLAT_PACKED: {
...@@ -1074,6 +1082,11 @@ private: ...@@ -1074,6 +1082,11 @@ private:
packed.write(words.begin(), words.asBytes().size()); packed.write(words.begin(), words.asBytes().size());
return; return;
} }
case Format::CANONICAL: {
auto words = reader.canonicalize();
output.write(words.begin(), words.asBytes().size());
return;
}
case Format::TEXT: { case Format::TEXT: {
TextCodec codec; TextCodec codec;
codec.setPrettyPrint(pretty); codec.setPrettyPrint(pretty);
...@@ -1449,6 +1462,7 @@ private: ...@@ -1449,6 +1462,7 @@ private:
case Format::PACKED : return isPlausiblyPacked (prefix); case Format::PACKED : return isPlausiblyPacked (prefix);
case Format::FLAT : return isPlausiblyFlat (prefix); case Format::FLAT : return isPlausiblyFlat (prefix);
case Format::FLAT_PACKED: return isPlausiblyPackedFlat(prefix); case Format::FLAT_PACKED: return isPlausiblyPackedFlat(prefix);
case Format::CANONICAL : return isPlausiblyFlat (prefix);
case Format::TEXT : return isPlausiblyText (prefix); case Format::TEXT : return isPlausiblyText (prefix);
case Format::JSON : return isPlausiblyJson (prefix); case Format::JSON : return isPlausiblyJson (prefix);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment