ext/snowcrash/src/SerializeYAML.cc in redsnow-0.2.0 vs ext/snowcrash/src/SerializeYAML.cc in redsnow-0.2.1

- old
+ new

@@ -15,17 +15,19 @@ /** Normalizes string value for use in YAML and checks whether quotation is need */ static std::string NormalizeStringValue(const std::string& value, bool& needsQuotation) { std::string normalizedValue = value; + if (normalizedValue.find("\"") != std::string::npos) normalizedValue = EscapeDoubleQuotes(normalizedValue); if (value.find("\n") != std::string::npos) normalizedValue = EscapeNewlines(normalizedValue); needsQuotation = (normalizedValue != value); + if (!needsQuotation) { for (std::string::const_iterator it = value.begin() ; it < value.end() ; ++it){ needsQuotation = ReservedCharacters.find(*it) != std::string::npos; if (needsQuotation) break; @@ -35,19 +37,23 @@ return normalizedValue; } /** Insert array item mark */ -static void ArrayItemLeadIn(size_t level, std::ostream &os) +static void ArrayItemLeadIn(size_t level, std::ostream &os, bool withTrailingSpace = true) { if (level < 1) return; for (size_t i = 0; i < level - 1; ++i) os << " "; - os << "- "; + os << "-"; + + if (withTrailingSpace) { + os << " "; + } } /** Serialize key value pair */ static void serialize(const std::string& key, const std::string& value, size_t level, std::ostream &os, bool implicitQuotation = true) { @@ -79,14 +85,67 @@ } else os << key << ":\n"; } +/** Serialize source map without key into output stream */ +static void serialize(const SourceMapBase& set, size_t level, std::ostream &os) +{ + if (!set.sourceMap.empty()) { + size_t i = 0; + os << std::endl; + + for (mdp::RangeSet<mdp::BytesRange>::const_iterator it = set.sourceMap.begin(); it != set.sourceMap.end(); ++i, ++it) { + + ArrayItemLeadIn(level, os, false); + os << "\n"; + ArrayItemLeadIn(level + 1, os); + os << it->location << "\n"; + ArrayItemLeadIn(level + 1, os); + os << it->length << "\n"; + } + } else { + os << " []\n"; + } +} + +/** Serialize a key and source map value into output stream. */ +static void serialize(const std::string& key, const SourceMapBase& value, size_t level, std::ostream &os, bool keyLevelIsZero = false) +{ + if (key.empty()) + return; + + if (!keyLevelIsZero) { + for (size_t i = 0; i < level; ++i) + os << " "; + } + + os << key << ":"; + serialize(value, level + 1, os); +} + +/** Serializes source map collection */ +static void serializeSourceMapCollection(const Collection<SourceMap<KeyValuePair> >::type& collection, size_t level, std::ostream &os) +{ + for (Collection<SourceMap<KeyValuePair> >::const_iterator it = collection.begin(); + it != collection.end(); + ++it) { + + // Array item + ArrayItemLeadIn(level + 1, os, false); + + // Source Map + serialize(*it, level + 2, os); + } +} + /** Serializes key value collection */ static void serializeKeyValueCollection(const Collection<KeyValuePair>::type& collection, size_t level, std::ostream &os) { - for (Collection<KeyValuePair>::const_iterator it = collection.begin(); it != collection.end(); ++it) { + for (Collection<KeyValuePair>::const_iterator it = collection.begin(); + it != collection.end(); + ++it) { // Array item ArrayItemLeadIn(level + 1, os); // Name @@ -106,18 +165,36 @@ return; serializeKeyValueCollection(metadata, 0, os); } +/** Serialize Metadata source map */ +static void serialize(const Collection<SourceMap<Metadata> >::type& metadata, std::ostream &os) +{ + serialize(SerializeKey::Metadata, std::string(), 0, os); + + if (metadata.empty()) + return; + + serializeSourceMapCollection(metadata, 0, os); +} + + /** Serialize Headers */ -static void serialize(const Collection<Header>::type& headers, size_t level, std::ostream &os) +static void serialize(const Headers& headers, size_t level, std::ostream &os) { serializeKeyValueCollection(headers, level, os); } +/** Serialize Headers source map */ +static void serialize(const Collection<SourceMap<Header> >::type& headers, size_t level, std::ostream &os) +{ + serializeSourceMapCollection(headers, level, os); +} + /** Serialize Parameters */ -static void serialize(const Collection<Parameter>::type& parameters, size_t level, std::ostream &os) +static void serialize(const Parameters& parameters, size_t level, std::ostream &os) { for (Collection<Parameter>::const_iterator it = parameters.begin(); it != parameters.end(); ++it) { // Array item ArrayItemLeadIn(level + 1, os); @@ -141,23 +218,65 @@ serialize(SerializeKey::Example, it->exampleValue, level + 1, os); // Values serialize(SerializeKey::Values, std::string(), level + 1, os); - if (!it->values.empty()) { - for (Collection<Value>::const_iterator val_it = it->values.begin(); - val_it != it->values.end(); - ++val_it) { + for (Collection<Value>::const_iterator val_it = it->values.begin(); + val_it != it->values.end(); + ++val_it) { - ArrayItemLeadIn(level + 2, os); + ArrayItemLeadIn(level + 2, os); - serialize(SerializeKey::Value, *val_it, 0, os); - } + serialize(SerializeKey::Value, *val_it, 0, os); } } } +/** Serialize Parameters source map */ +static void serialize(const Collection<SourceMap<Parameter> >::type& parameters, size_t level, std::ostream &os) +{ + for (Collection<SourceMap<Parameter> >::const_iterator it = parameters.begin(); + it != parameters.end(); + ++it) { + + // Array item + ArrayItemLeadIn(level + 1, os); + + // Key / name + serialize(SerializeKey::Name, it->name, level + 1, os, true); + + // Description + serialize(SerializeKey::Description, it->description, level + 1, os); + + // Type + serialize(SerializeKey::Type, it->type, level + 1, os); + + // Required + serialize(SerializeKey::Required, it->use, level + 1, os); + + // Default + serialize(SerializeKey::Default, it->defaultValue, level + 1, os); + + // Example + serialize(SerializeKey::Example, it->exampleValue, level + 1, os); + + // Values + serialize(SerializeKey::Values, std::string(), level + 1, os); + + for (Collection<SourceMap<Value> >::const_iterator val_it = it->values.collection.begin(); + val_it != it->values.collection.end(); + ++val_it) { + + // Array item + ArrayItemLeadIn(level + 2, os); + + // Source Map + serialize(*val_it, level + 3, os); + } + } +} + /** Serialize Payload */ static void serialize(const Payload& payload, size_t level, bool array, std::ostream &os) { for (size_t i = 0; i < level - 1; i++) { os << " "; @@ -165,58 +284,146 @@ if (array) os << "- "; else os << " "; - + // Name serialize(SerializeKey::Name, payload.name, 0, os); - + + // Symbol Reference + if (!payload.symbol.empty()) { + serialize(SerializeKey::Reference, "", level, os); + serialize(SerializeKey::Id, payload.symbol, level + 1, os); + } + // Description serialize(SerializeKey::Description, payload.description, level, os); // Headers serialize(SerializeKey::Headers, std::string(), level, os); + if (!payload.headers.empty()) { serialize(payload.headers, level, os); } // Body serialize(SerializeKey::Body, payload.body, level, os); // Schema serialize(SerializeKey::Schema, payload.schema, level, os); +} +/** Serialize Payload source map */ +static void serialize(const SourceMap<Payload>& payload, size_t level, bool array, std::ostream &os) +{ + for (size_t i = 0; i < level - 1; i++) { + os << " "; + } + + if (array) + os << "- "; + else + os << " "; + + // Name + serialize(SerializeKey::Name, payload.name, level, os, true); + + // Symbol Reference + if (!payload.symbol.sourceMap.empty()) { + serialize(SerializeKey::Reference, payload.symbol, level, os); + } + + // Description + serialize(SerializeKey::Description, payload.description, level, os); + + // Headers + serialize(SerializeKey::Headers, std::string(), level, os); + + if (!payload.headers.collection.empty()) { + serialize(payload.headers.collection, level, os); + } + + // Body + serialize(SerializeKey::Body, payload.body, level, os); + + // Schema + serialize(SerializeKey::Schema, payload.schema, level, os); } -// Serialize Transaction Example +/** Serialize Transaction Example */ static void serialize(const TransactionExample& example, std::ostream &os) { os << " - "; // indent 4 + // Name serialize(SerializeKey::Name, example.name, 0, os); // Description serialize(SerializeKey::Description, example.description, 4, os); // Requests serialize(SerializeKey::Requests, std::string(), 4, os); + if (!example.requests.empty()) { - for (Collection<Request>::const_iterator it = example.requests.begin(); it != example.requests.end(); ++it) { + for (Collection<Request>::const_iterator it = example.requests.begin(); + it != example.requests.end(); + ++it) { + serialize(*it, 5, true, os); } } // Responses serialize(SerializeKey::Responses, std::string(), 4, os); + if (!example.responses.empty()) { - for (Collection<Response>::const_iterator it = example.responses.begin(); it != example.responses.end(); ++it) { + for (Collection<Response>::const_iterator it = example.responses.begin(); + it != example.responses.end(); + ++it) { + serialize(*it, 5, true, os); } } } +/** Serialize Transaction Example source map */ +static void serialize(const SourceMap<TransactionExample>& example, std::ostream &os) +{ + os << " - "; // indent 4 + + // Name + serialize(SerializeKey::Name, example.name, 4, os, true); + + // Description + serialize(SerializeKey::Description, example.description, 4, os); + + // Requests + serialize(SerializeKey::Requests, std::string(), 4, os); + + if (!example.requests.collection.empty()) { + for (Collection<SourceMap<Request> >::const_iterator it = example.requests.collection.begin(); + it != example.requests.collection.end(); + ++it) { + + serialize(*it, 5, true, os); + } + } + + // Responses + serialize(SerializeKey::Responses, std::string(), 4, os); + + if (!example.responses.collection.empty()) { + for (Collection<SourceMap<Response> >::const_iterator it = example.responses.collection.begin(); + it != example.responses.collection.end(); + ++it) { + + serialize(*it, 5, true, os); + } + } +} + /** Serialize Action */ static void serialize(const Action& action, std::ostream &os) { os << " - "; // indent 3 @@ -229,24 +436,60 @@ // HTTP method serialize(SerializeKey::Method, action.method, 3, os); // Parameters serialize(SerializeKey::Parameters, std::string(), 3, os); + if (!action.parameters.empty()) serialize(action.parameters, 3, os); // Examples serialize(SerializeKey::Examples, std::string(), 3, os); + if (!action.examples.empty()) { for (Collection<TransactionExample>::const_iterator it = action.examples.begin(); it != action.examples.end(); ++it) { + serialize(*it, os); } } } +/** Serialize Action source map */ +static void serialize(const SourceMap<Action>& action, std::ostream &os) +{ + os << " - "; // indent 3 + + // Name + serialize(SerializeKey::Name, action.name, 3, os, true); + + // Description + serialize(SerializeKey::Description, action.description, 3, os); + + // HTTP method + serialize(SerializeKey::Method, action.method, 3, os); + + // Parameters + serialize(SerializeKey::Parameters, std::string(), 3, os); + + if (!action.parameters.collection.empty()) + serialize(action.parameters.collection, 3, os); + + // Examples + serialize(SerializeKey::Examples, std::string(), 3, os); + + if (!action.examples.collection.empty()) { + for (Collection<SourceMap<TransactionExample> >::const_iterator it = action.examples.collection.begin(); + it != action.examples.collection.end(); + ++it) { + + serialize(*it, os); + } + } +} + /** Serialize Resource */ static void serialize(const Resource& resource, std::ostream &os) { os << " - "; // indent 2 @@ -259,29 +502,74 @@ // URI Template serialize(SerializeKey::URITemplate, resource.uriTemplate, 2, os); // Model serialize(SerializeKey::Model, std::string(), 2, os); + if (!resource.model.name.empty()) serialize(resource.model, 3, false, os); // Parameters serialize(SerializeKey::Parameters, std::string(), 2, os); + if (!resource.parameters.empty()) serialize(resource.parameters, 2, os); // Actions serialize(SerializeKey::Actions, std::string(), 2, os); if (resource.actions.empty()) return; - for (Collection<Action>::const_iterator it = resource.actions.begin(); it != resource.actions.end(); ++it) { + for (Collection<Action>::const_iterator it = resource.actions.begin(); + it != resource.actions.end(); + ++it) { + serialize(*it, os); } } +/** Serialize Resource source map */ +static void serialize(const SourceMap<Resource>& resource, std::ostream &os) +{ + os << " - "; // indent 2 + + // Name + serialize(SerializeKey::Name, resource.name, 2, os, true); + + // Description + serialize(SerializeKey::Description, resource.description, 2, os); + + // URI Template + serialize(SerializeKey::URITemplate, resource.uriTemplate, 2, os); + + // Model + serialize(SerializeKey::Model, std::string(), 2, os); + + if (!resource.model.name.sourceMap.empty()) + serialize(resource.model, 3, false, os); + + // Parameters + serialize(SerializeKey::Parameters, std::string(), 2, os); + + if (!resource.parameters.collection.empty()) + serialize(resource.parameters.collection, 2, os); + + // Actions + serialize(SerializeKey::Actions, std::string(), 2, os); + + if (resource.actions.collection.empty()) + return; + + for (Collection<SourceMap<Action> >::const_iterator it = resource.actions.collection.begin(); + it != resource.actions.collection.end(); + ++it) { + + serialize(*it, os); + } +} + /** Serialize Resource Group */ static void serialize(const ResourceGroup& group, std::ostream &os) { os << "- "; // indent 1 @@ -295,15 +583,43 @@ serialize(SerializeKey::Resources, std::string(), 1, os); if (group.resources.empty()) return; - for (Collection<Resource>::const_iterator it = group.resources.begin(); it != group.resources.end(); ++it) { + for (Collection<Resource>::const_iterator it = group.resources.begin(); + it != group.resources.end(); + ++it) { + serialize(*it, os); } } +/** Serialize Resource Group source map */ +static void serialize(const SourceMap<ResourceGroup>& group, std::ostream &os) +{ + os << "- "; // indent 1 + + // Name + serialize(SerializeKey::Name, group.name, 1, os, true); + + // Description + serialize(SerializeKey::Description, group.description, 1, os); + + // Resources + serialize(SerializeKey::Resources, std::string(), 1, os); + + if (group.resources.collection.empty()) + return; + + for (Collection<SourceMap<Resource> >::const_iterator it = group.resources.collection.begin(); + it != group.resources.collection.end(); + ++it) { + + serialize(*it, os); + } +} + /** Serialize Blueprint */ static void serialize(const Blueprint& blueprint, std::ostream &os) { // AST Version serialize(SerializeKey::ASTVersion, AST_SERIALIZATION_VERSION, 0, os, false); @@ -317,10 +633,11 @@ // API Description serialize(SerializeKey::Description, blueprint.description, 0, os); // Resource Groups serialize(SerializeKey::ResourceGroups, std::string(), 0, os); + if (blueprint.resourceGroups.empty()) return; for (Collection<ResourceGroup>::type::const_iterator it = blueprint.resourceGroups.begin(); it != blueprint.resourceGroups.end(); @@ -328,9 +645,40 @@ serialize(*it, os); } } +/** Serialize Blueprint source map */ +static void serialize(const SourceMap<Blueprint>& blueprint, std::ostream &os) +{ + // Metadata + serialize(blueprint.metadata.collection, os); + + // API Name + serialize(SerializeKey::Name, blueprint.name, 0, os); + + // API Description + serialize(SerializeKey::Description, blueprint.description, 0, os); + + // Resource Groups + serialize(SerializeKey::ResourceGroups, std::string(), 0, os); + + if (blueprint.resourceGroups.collection.empty()) + return; + + for (Collection<SourceMap<ResourceGroup> >::type::const_iterator it = blueprint.resourceGroups.collection.begin(); + it != blueprint.resourceGroups.collection.end(); + ++it) { + + serialize(*it, os); + } +} + void snowcrash::SerializeYAML(const snowcrash::Blueprint& blueprint, std::ostream &os) +{ + serialize(blueprint, os); +} + +void snowcrash::SerializeSourceMapYAML(const snowcrash::SourceMap<snowcrash::Blueprint>& blueprint, std::ostream &os) { serialize(blueprint, os); }