redsnow 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +34 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +20 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +21 -0
  8. data/README.md +62 -0
  9. data/Rakefile +36 -0
  10. data/Vagrantfile +20 -0
  11. data/ext/snowcrash/Makefile +64 -0
  12. data/ext/snowcrash/Vagrantfile +20 -0
  13. data/ext/snowcrash/bin/snowcrash +0 -0
  14. data/ext/snowcrash/common.gypi +163 -0
  15. data/ext/snowcrash/config.gypi +10 -0
  16. data/ext/snowcrash/config.mk +5 -0
  17. data/ext/snowcrash/configure +213 -0
  18. data/ext/snowcrash/provisioning.sh +15 -0
  19. data/ext/snowcrash/snowcrash.gyp +141 -0
  20. data/ext/snowcrash/src/ActionParser.h +503 -0
  21. data/ext/snowcrash/src/AssetParser.h +215 -0
  22. data/ext/snowcrash/src/BlockUtility.h +186 -0
  23. data/ext/snowcrash/src/Blueprint.h +283 -0
  24. data/ext/snowcrash/src/BlueprintParser.h +347 -0
  25. data/ext/snowcrash/src/BlueprintParserCore.h +190 -0
  26. data/ext/snowcrash/src/BlueprintSection.h +140 -0
  27. data/ext/snowcrash/src/BlueprintUtility.h +126 -0
  28. data/ext/snowcrash/src/CBlueprint.cc +600 -0
  29. data/ext/snowcrash/src/CBlueprint.h +354 -0
  30. data/ext/snowcrash/src/CSourceAnnotation.cc +140 -0
  31. data/ext/snowcrash/src/CSourceAnnotation.h +106 -0
  32. data/ext/snowcrash/src/CodeBlockUtility.h +189 -0
  33. data/ext/snowcrash/src/DescriptionSectionUtility.h +156 -0
  34. data/ext/snowcrash/src/HTTP.cc +46 -0
  35. data/ext/snowcrash/src/HTTP.h +105 -0
  36. data/ext/snowcrash/src/HeaderParser.h +289 -0
  37. data/ext/snowcrash/src/ListBlockUtility.h +273 -0
  38. data/ext/snowcrash/src/ListUtility.h +95 -0
  39. data/ext/snowcrash/src/MarkdownBlock.cc +176 -0
  40. data/ext/snowcrash/src/MarkdownBlock.h +93 -0
  41. data/ext/snowcrash/src/MarkdownParser.cc +266 -0
  42. data/ext/snowcrash/src/MarkdownParser.h +88 -0
  43. data/ext/snowcrash/src/ParameterDefinitonParser.h +570 -0
  44. data/ext/snowcrash/src/ParametersParser.h +252 -0
  45. data/ext/snowcrash/src/Parser.cc +71 -0
  46. data/ext/snowcrash/src/Parser.h +29 -0
  47. data/ext/snowcrash/src/ParserCore.cc +120 -0
  48. data/ext/snowcrash/src/ParserCore.h +82 -0
  49. data/ext/snowcrash/src/PayloadParser.h +672 -0
  50. data/ext/snowcrash/src/Platform.h +54 -0
  51. data/ext/snowcrash/src/RegexMatch.h +32 -0
  52. data/ext/snowcrash/src/ResourceGroupParser.h +195 -0
  53. data/ext/snowcrash/src/ResourceParser.h +584 -0
  54. data/ext/snowcrash/src/SectionUtility.h +142 -0
  55. data/ext/snowcrash/src/Serialize.cc +52 -0
  56. data/ext/snowcrash/src/Serialize.h +69 -0
  57. data/ext/snowcrash/src/SerializeJSON.cc +601 -0
  58. data/ext/snowcrash/src/SerializeJSON.h +21 -0
  59. data/ext/snowcrash/src/SerializeYAML.cc +336 -0
  60. data/ext/snowcrash/src/SerializeYAML.h +21 -0
  61. data/ext/snowcrash/src/SourceAnnotation.h +177 -0
  62. data/ext/snowcrash/src/StringUtility.h +109 -0
  63. data/ext/snowcrash/src/SymbolTable.h +83 -0
  64. data/ext/snowcrash/src/UriTemplateParser.cc +195 -0
  65. data/ext/snowcrash/src/UriTemplateParser.h +243 -0
  66. data/ext/snowcrash/src/Version.h +39 -0
  67. data/ext/snowcrash/src/csnowcrash.cc +23 -0
  68. data/ext/snowcrash/src/csnowcrash.h +38 -0
  69. data/ext/snowcrash/src/posix/RegexMatch.cc +99 -0
  70. data/ext/snowcrash/src/snowcrash.cc +18 -0
  71. data/ext/snowcrash/src/snowcrash.h +41 -0
  72. data/ext/snowcrash/src/snowcrash/snowcrash.cc +170 -0
  73. data/ext/snowcrash/src/win/RegexMatch.cc +78 -0
  74. data/ext/snowcrash/sundown/CONTRIBUTING.md +10 -0
  75. data/ext/snowcrash/sundown/Makefile +83 -0
  76. data/ext/snowcrash/sundown/Makefile.win +33 -0
  77. data/ext/snowcrash/sundown/examples/smartypants.c +72 -0
  78. data/ext/snowcrash/sundown/examples/sundown.c +80 -0
  79. data/ext/snowcrash/sundown/html/houdini.h +37 -0
  80. data/ext/snowcrash/sundown/html/houdini_href_e.c +108 -0
  81. data/ext/snowcrash/sundown/html/houdini_html_e.c +84 -0
  82. data/ext/snowcrash/sundown/html/html.c +647 -0
  83. data/ext/snowcrash/sundown/html/html.h +77 -0
  84. data/ext/snowcrash/sundown/html/html_smartypants.c +389 -0
  85. data/ext/snowcrash/sundown/html_block_names.txt +25 -0
  86. data/ext/snowcrash/sundown/src/autolink.c +297 -0
  87. data/ext/snowcrash/sundown/src/autolink.h +51 -0
  88. data/ext/snowcrash/sundown/src/buffer.c +225 -0
  89. data/ext/snowcrash/sundown/src/buffer.h +96 -0
  90. data/ext/snowcrash/sundown/src/html_blocks.h +206 -0
  91. data/ext/snowcrash/sundown/src/markdown.c +2701 -0
  92. data/ext/snowcrash/sundown/src/markdown.h +147 -0
  93. data/ext/snowcrash/sundown/src/src_map.c +200 -0
  94. data/ext/snowcrash/sundown/src/src_map.h +58 -0
  95. data/ext/snowcrash/sundown/src/stack.c +81 -0
  96. data/ext/snowcrash/sundown/src/stack.h +29 -0
  97. data/ext/snowcrash/sundown/sundown.def +20 -0
  98. data/ext/snowcrash/tools/gyp/AUTHORS +11 -0
  99. data/ext/snowcrash/tools/gyp/DEPS +24 -0
  100. data/ext/snowcrash/tools/gyp/OWNERS +1 -0
  101. data/ext/snowcrash/tools/gyp/PRESUBMIT.py +120 -0
  102. data/ext/snowcrash/tools/gyp/buildbot/buildbot_run.py +190 -0
  103. data/ext/snowcrash/tools/gyp/codereview.settings +10 -0
  104. data/ext/snowcrash/tools/gyp/data/win/large-pdb-shim.cc +12 -0
  105. data/ext/snowcrash/tools/gyp/gyp +8 -0
  106. data/ext/snowcrash/tools/gyp/gyp.bat +5 -0
  107. data/ext/snowcrash/tools/gyp/gyp_main.py +18 -0
  108. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSNew.py +340 -0
  109. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSProject.py +208 -0
  110. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSSettings.py +1063 -0
  111. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSToolFile.py +58 -0
  112. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUserFile.py +147 -0
  113. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSUtil.py +267 -0
  114. data/ext/snowcrash/tools/gyp/pylib/gyp/MSVSVersion.py +409 -0
  115. data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.py +537 -0
  116. data/ext/snowcrash/tools/gyp/pylib/gyp/__init__.pyc +0 -0
  117. data/ext/snowcrash/tools/gyp/pylib/gyp/common.py +521 -0
  118. data/ext/snowcrash/tools/gyp/pylib/gyp/common.pyc +0 -0
  119. data/ext/snowcrash/tools/gyp/pylib/gyp/easy_xml.py +157 -0
  120. data/ext/snowcrash/tools/gyp/pylib/gyp/flock_tool.py +49 -0
  121. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.py +0 -0
  122. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/__init__.pyc +0 -0
  123. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/android.py +1069 -0
  124. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/cmake.py +1143 -0
  125. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/dump_dependency_json.py +81 -0
  126. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/eclipse.py +335 -0
  127. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypd.py +87 -0
  128. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/gypsh.py +56 -0
  129. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.py +2181 -0
  130. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/make.pyc +0 -0
  131. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/msvs.py +3335 -0
  132. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/ninja.py +2156 -0
  133. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.py +1224 -0
  134. data/ext/snowcrash/tools/gyp/pylib/gyp/generator/xcode.pyc +0 -0
  135. data/ext/snowcrash/tools/gyp/pylib/gyp/input.py +2809 -0
  136. data/ext/snowcrash/tools/gyp/pylib/gyp/input.pyc +0 -0
  137. data/ext/snowcrash/tools/gyp/pylib/gyp/mac_tool.py +510 -0
  138. data/ext/snowcrash/tools/gyp/pylib/gyp/msvs_emulation.py +972 -0
  139. data/ext/snowcrash/tools/gyp/pylib/gyp/ninja_syntax.py +160 -0
  140. data/ext/snowcrash/tools/gyp/pylib/gyp/ordered_dict.py +289 -0
  141. data/ext/snowcrash/tools/gyp/pylib/gyp/win_tool.py +292 -0
  142. data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.py +1440 -0
  143. data/ext/snowcrash/tools/gyp/pylib/gyp/xcode_emulation.pyc +0 -0
  144. data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.py +2889 -0
  145. data/ext/snowcrash/tools/gyp/pylib/gyp/xcodeproj_file.pyc +0 -0
  146. data/ext/snowcrash/tools/gyp/pylib/gyp/xml_fix.py +69 -0
  147. data/ext/snowcrash/tools/gyp/pylintrc +307 -0
  148. data/ext/snowcrash/tools/gyp/samples/samples +81 -0
  149. data/ext/snowcrash/tools/gyp/samples/samples.bat +5 -0
  150. data/ext/snowcrash/tools/gyp/setup.py +19 -0
  151. data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.pbfilespec +27 -0
  152. data/ext/snowcrash/tools/gyp/tools/Xcode/Specifications/gyp.xclangspec +226 -0
  153. data/ext/snowcrash/tools/gyp/tools/emacs/gyp.el +252 -0
  154. data/ext/snowcrash/tools/gyp/tools/graphviz.py +100 -0
  155. data/ext/snowcrash/tools/gyp/tools/pretty_gyp.py +155 -0
  156. data/ext/snowcrash/tools/gyp/tools/pretty_sln.py +168 -0
  157. data/ext/snowcrash/tools/gyp/tools/pretty_vcproj.py +329 -0
  158. data/ext/snowcrash/tools/homebrew/snowcrash.rb +11 -0
  159. data/ext/snowcrash/vcbuild.bat +184 -0
  160. data/lib/redsnow.rb +31 -0
  161. data/lib/redsnow/binding.rb +132 -0
  162. data/lib/redsnow/blueprint.rb +365 -0
  163. data/lib/redsnow/object.rb +18 -0
  164. data/lib/redsnow/parseresult.rb +107 -0
  165. data/lib/redsnow/version.rb +4 -0
  166. data/provisioning.sh +20 -0
  167. data/redsnow.gemspec +35 -0
  168. data/test/_helper.rb +15 -0
  169. data/test/fixtures/sample-api-ast.json +97 -0
  170. data/test/fixtures/sample-api.apib +20 -0
  171. data/test/redsnow_binding_test.rb +35 -0
  172. data/test/redsnow_parseresult_test.rb +50 -0
  173. data/test/redsnow_test.rb +285 -0
  174. metadata +358 -0
@@ -0,0 +1,142 @@
1
+ //
2
+ // SectionUtility.h
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 11/11/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #ifndef SNOWCRASH_SECTIONUTILITY_H
10
+ #define SNOWCRASH_SECTIONUTILITY_H
11
+
12
+ #include "BlockUtility.h"
13
+ #include "ListBlockUtility.h"
14
+ #include "CodeBlockUtility.h"
15
+
16
+ namespace snowcrash {
17
+
18
+ /**
19
+ * Compute expected indentation of a section
20
+ */
21
+ FORCEINLINE size_t SectionIndentationLevel(const BlueprintSection& section)
22
+ {
23
+ switch (section.type) {
24
+ case ParameterValuesSectionType:
25
+ return 2;
26
+
27
+ case BodySectionType:
28
+ case SchemaSectionType:
29
+ case HeadersSectionType: // NOTE: Headers can be potentially both level 0 and 1
30
+ return 1;
31
+
32
+ default:
33
+ return 0;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * \brief Report & skip blocks in a foreign section.
39
+ * \param section Section including the foreign section.
40
+ * \param cur A cursor pointing to the beigin of the foreign section.
41
+ * \return Standard section parse result pointing AFTER the last block parsed.
42
+ */
43
+ template <class T>
44
+ FORCEINLINE ParseSectionResult HandleForeignSection(const BlueprintSection& section,
45
+ const BlockIterator& cur,
46
+ const SourceData& sourceData) {
47
+
48
+ ParseSectionResult result = std::make_pair(Result(), cur);
49
+ BlockIterator sectionCur = cur;
50
+
51
+ std::stringstream ss;
52
+ SourceCharactersBlock sourceBlock;
53
+
54
+ // Handle list / list item separately
55
+ if (sectionCur->type == ListItemBlockBeginType ||
56
+ sectionCur->type == ListBlockBeginType) {
57
+
58
+ // Skip the list / list item
59
+ if (sectionCur->type == ListBlockBeginType) {
60
+ sectionCur = SkipToClosingBlock(sectionCur, section.bounds.second, ListBlockBeginType, ListBlockEndType);
61
+ }
62
+ else {
63
+ sectionCur = SkipToClosingBlock(sectionCur, section.bounds.second, ListItemBlockBeginType, ListItemBlockEndType);
64
+ }
65
+ SectionType type = ClassifyChildrenListBlock<T>(cur, section.bounds.second);
66
+
67
+ // Assemble the message
68
+ ss << "ignoring ";
69
+ ss << ((type != UndefinedSectionType) ? SectionName(type) : "unrecognized");
70
+ ss << " list";
71
+ if (cur->type == ListItemBlockBeginType)
72
+ ss << " item";
73
+
74
+ if (type != UndefinedSectionType &&
75
+ section.hasParent()) {
76
+ BlueprintSection recognizedSection(type, std::make_pair(cur, sectionCur), section.parent());
77
+ size_t level = SectionIndentationLevel(recognizedSection);
78
+
79
+ if (level) {
80
+ ss << ", " << SectionName(type) << " list ";
81
+ if (cur->type == ListItemBlockBeginType)
82
+ ss << "item ";
83
+ ss << "is expected to be indented by ";
84
+ ss << level * 4 << " spaces or " << level << " tab";
85
+ }
86
+ }
87
+
88
+ sourceBlock = CharacterMapForBlock(sectionCur, cur, section.bounds, sourceData);
89
+ sectionCur = CloseList(sectionCur, section.bounds.second);
90
+ }
91
+ else if (sectionCur->type == QuoteBlockBeginType) {
92
+
93
+ // Skip quote or just one block
94
+ ss << "ignoring unrecognized quote";
95
+ sectionCur = SkipToClosingBlock(sectionCur, section.bounds.second, QuoteBlockBeginType, QuoteBlockEndType);
96
+ sourceBlock = CharacterMapForBlock(sectionCur, cur, section.bounds, sourceData);
97
+
98
+ if (!CheckCursor(section, sectionCur, sourceData, result.first))
99
+ return result;
100
+
101
+ ++sectionCur;
102
+ }
103
+ else {
104
+ // Skip just one block
105
+ ss << "ignoring unrecognized block";
106
+ sourceBlock = CharacterMapForBlock(sectionCur, cur, section.bounds, sourceData);
107
+
108
+ ++sectionCur;
109
+ }
110
+
111
+
112
+ if (section.hasParent()) {
113
+ // Additional hint for some sections
114
+ switch (section.parent().type) {
115
+ case ParametersSectionType:
116
+ ss << ", expected parameter discussion, e.g. '<parameter name> ... lorem ipsum'";
117
+ break;
118
+
119
+ case ParameterDefinitionSectionType:
120
+ ss << ", expected enumeration of parameter values, e.g. 'Values' followed by a nested list of possbile values";
121
+ break;
122
+
123
+ case ActionSectionType:
124
+ ss << ", expected request, response, parameters, headers or a defintion of the next action, resource or a group";
125
+ break;
126
+
127
+ default:
128
+ break;
129
+ }
130
+ }
131
+
132
+ // WARN: Ignoring unrecognized block
133
+ result.first.warnings.push_back(Warning(ss.str(),
134
+ IgnoringWarning,
135
+ sourceBlock));
136
+
137
+ result.second = sectionCur;
138
+ return result;
139
+ }
140
+ }
141
+
142
+ #endif
@@ -0,0 +1,52 @@
1
+ //
2
+ // Serialize.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 5/3/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include <string>
10
+ #include <sstream>
11
+ #include <algorithm>
12
+ #include <iterator>
13
+ #include "Serialize.h"
14
+ #include "StringUtility.h"
15
+
16
+ using namespace snowcrash;
17
+
18
+ const std::string SerializeKey::ASTVersion = "_version";
19
+ const std::string SerializeKey::Metadata = "metadata";
20
+ const std::string SerializeKey::Name = "name";
21
+ const std::string SerializeKey::Description = "description";
22
+ const std::string SerializeKey::ResourceGroups = "resourceGroups";
23
+ const std::string SerializeKey::Resources = "resources";
24
+ const std::string SerializeKey::URI = "uri";
25
+ const std::string SerializeKey::URITemplate = "uriTemplate";
26
+ const std::string SerializeKey::Actions = "actions";
27
+ const std::string SerializeKey::Action = "action";
28
+ const std::string SerializeKey::Method = "method";
29
+ const std::string SerializeKey::Examples = "examples";
30
+ const std::string SerializeKey::Requests = "requests";
31
+ const std::string SerializeKey::Responses = "responses";
32
+ const std::string SerializeKey::Body = "body";
33
+ const std::string SerializeKey::Schema = "schema";
34
+ const std::string SerializeKey::Headers = "headers";
35
+ const std::string SerializeKey::Model = "model";
36
+ const std::string SerializeKey::Value = "value";
37
+ const std::string SerializeKey::Parameters = "parameters";
38
+ const std::string SerializeKey::Type = "type";
39
+ const std::string SerializeKey::Required = "required";
40
+ const std::string SerializeKey::Default = "default";
41
+ const std::string SerializeKey::Example = "example";
42
+ const std::string SerializeKey::Values = "values";
43
+
44
+ std::string snowcrash::EscapeNewlines(const std::string& input)
45
+ {
46
+ return ReplaceString(input, "\n", "\\n");
47
+ }
48
+
49
+ std::string snowcrash::EscapeDoubleQuotes(const std::string& input)
50
+ {
51
+ return ReplaceString(input, "\"", "\\\"");
52
+ }
@@ -0,0 +1,69 @@
1
+ //
2
+ // Serialize.h
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 5/3/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #ifndef SNOWCRASH_SERIALIZE_H
10
+ #define SNOWCRASH_SERIALIZE_H
11
+
12
+ #include <string>
13
+
14
+ /** Version of API Blueprint AST serialization */
15
+ #define AST_SERIALIZATION_VERSION "2.0"
16
+
17
+ namespace snowcrash {
18
+
19
+ /**
20
+ * \brief Escape new lines in input string.
21
+ * \param input A string to escape its new lines.
22
+ * \return A new string with lines escaped.
23
+ *
24
+ * This function replaces all occurences of "\n" with "\\n".
25
+ */
26
+ std::string EscapeNewlines(const std::string& input);
27
+
28
+ /**
29
+ * \brief Escape every double quote in input string.
30
+ * \param input A string to escape its double quotes.
31
+ * \return A new string with double quotes escaped.
32
+ */
33
+ std::string EscapeDoubleQuotes(const std::string& input);
34
+
35
+ /**
36
+ * AST entities serialization keys
37
+ */
38
+ struct SerializeKey {
39
+ static const std::string ASTVersion;
40
+ static const std::string Metadata;
41
+ static const std::string Name;
42
+ static const std::string Description;
43
+ static const std::string ResourceGroups;
44
+ static const std::string Resources;
45
+ static const std::string URI;
46
+ static const std::string URITemplate;
47
+ static const std::string Actions;
48
+ static const std::string Action;
49
+ static const std::string Examples;
50
+ static const std::string Transaction;
51
+ static const std::string Method;
52
+ static const std::string Requests;
53
+ static const std::string Responses;
54
+ static const std::string Body;
55
+ static const std::string Schema;
56
+ static const std::string Headers;
57
+ static const std::string Model;
58
+ static const std::string Value;
59
+ static const std::string Parameters;
60
+ static const std::string Type;
61
+ static const std::string Required;
62
+ static const std::string Default;
63
+ static const std::string Example;
64
+ static const std::string Values;
65
+
66
+ };
67
+ }
68
+
69
+ #endif
@@ -0,0 +1,601 @@
1
+ //
2
+ // SerializeJSON.cc
3
+ // snowcrash
4
+ //
5
+ // Created by Zdenek Nemec on 4/27/13.
6
+ // Copyright (c) 2013 Apiary Inc. All rights reserved.
7
+ //
8
+
9
+ #include "SerializeJSON.h"
10
+ #include "Serialize.h"
11
+
12
+ using namespace snowcrash;
13
+
14
+ static const std::string IndentBlock = " ";
15
+ static const std::string NewLineItemBlock = ",\n";
16
+
17
+ /**
18
+ * \brief Serialize a JSON string.
19
+ * \param value JSON string to serialize
20
+ * \param os An output stream to serialize into
21
+ */
22
+ static void serialize(const std::string& value, std::ostream &os)
23
+ {
24
+ os << "\"" << value << "\"";
25
+ }
26
+
27
+ /**
28
+ * \brief Inserts indentation into an output stream.
29
+ * \param level Level of indentation
30
+ * \param os An output stream to serialize into
31
+ */
32
+ static void indent(size_t level, std::ostream &os)
33
+ {
34
+ for (size_t i = 0; i < level; ++i) {
35
+ os << IndentBlock;
36
+ }
37
+ }
38
+
39
+ /**
40
+ * \brief Serialize key value pair into output stream.
41
+ * \param key Key to serialize
42
+ * \param value Value to serialize
43
+ * \param level Indentation level
44
+ * \param object Flag to indicate whether the pair should be serialized as an object
45
+ * \param os An output stream to serialize into
46
+ */
47
+ static void serialize(const std::string& key, const std::string& value, size_t level, bool object, std::ostream &os)
48
+ {
49
+ indent(level, os);
50
+
51
+ if (object) {
52
+ os << "{\n";
53
+ indent(level + 1, os);
54
+ }
55
+
56
+ serialize(key, os);
57
+ os << ": ";
58
+
59
+ std::string normValue = EscapeDoubleQuotes(value);
60
+ if (normValue.find("\n") != std::string::npos)
61
+ serialize(EscapeNewlines(normValue), os);
62
+ else
63
+ serialize(normValue, os);
64
+
65
+ if (object) {
66
+ os << "\n";
67
+ indent(level, os);
68
+ os << "}";
69
+ }
70
+ }
71
+
72
+ /**
73
+ * \brief Serialize key boolean value pair into output stream
74
+ * \param key Key to serialize
75
+ * \param value Value to serialize
76
+ * \param level Indentation level
77
+ * \param os An output stream to serialize into
78
+ */
79
+ static void serialize(const std::string& key, bool value, size_t level, std::ostream &os)
80
+ {
81
+ indent(level, os);
82
+ serialize(key, os);
83
+ os << ": ";
84
+ os << ((value) ? "true" : "false");
85
+ }
86
+
87
+ /**
88
+ * \brief Serialize key value pair as an object into output stream
89
+ * \param key Key to serialize
90
+ * \param value Value to serialize
91
+ * \param level Indentation level
92
+ * \param os An output stream to serialize into
93
+ */
94
+ static void serialize(const std::string& key, const std::string& value, size_t level, std::ostream &os)
95
+ {
96
+ indent(level, os);
97
+
98
+ os << "{\n";
99
+
100
+ serialize(SerializeKey::Name, key, level + 1, false, os);
101
+ os << NewLineItemBlock;
102
+
103
+ serialize(SerializeKey::Value, value, level + 1, false, os);
104
+ os << "\n";
105
+
106
+ indent(level, os);
107
+ os << "}";
108
+ }
109
+
110
+ /**
111
+ * \brief Serialize an array of key value pairs.
112
+ * \param collection Collection to serialize
113
+ * \param level Level of indentation
114
+ * \param os An output stream to serialize into
115
+ */
116
+ static void serializeKeyValueCollection(const Collection<KeyValuePair>::type& collection, size_t level, std::ostream &os)
117
+ {
118
+ os << "[";
119
+
120
+ if (!collection.empty()) {
121
+ os << "\n";
122
+ size_t i = 0;
123
+ for (Collection<KeyValuePair>::const_iterator it = collection.begin(); it != collection.end(); ++i, ++it) {
124
+
125
+ if (i > 0 && i < collection.size())
126
+ os << NewLineItemBlock;
127
+
128
+
129
+ serialize(it->first, it->second, level + 1, os);
130
+ }
131
+
132
+ os << "\n";
133
+ indent(level, os);
134
+ }
135
+
136
+ os << "]";
137
+ }
138
+ /**
139
+ * \brief Serialize Metadata into output stream.
140
+ * \param metadata Metadata to serialize
141
+ * \param os An output stream to serialize into
142
+ */
143
+ static void serialize(const Collection<Metadata>::type& metadata, std::ostream &os)
144
+ {
145
+ indent(1, os);
146
+ serialize(SerializeKey::Metadata, os);
147
+ os << ": ";
148
+
149
+ serializeKeyValueCollection(metadata, 1, os);
150
+
151
+ os << NewLineItemBlock;
152
+ }
153
+
154
+ /**
155
+ * \brief Serialize Parameters into output stream.
156
+ * \param prarameters Parameters to serialize.
157
+ * \param level Level of indentation.
158
+ * \param os An output stream to serialize into.
159
+ */
160
+ static void serialize(const Collection<Parameter>::type& parameters, size_t level, std::ostream &os)
161
+ {
162
+ indent(level, os);
163
+ serialize(SerializeKey::Parameters, os);
164
+ os << ": [";
165
+
166
+ if (!parameters.empty()) {
167
+ os << "\n";
168
+ size_t i = 0;
169
+ for (Collection<Parameter>::const_iterator it = parameters.begin(); it != parameters.end(); ++i, ++it) {
170
+
171
+ if (i > 0 && i < parameters.size())
172
+ os << NewLineItemBlock;
173
+
174
+ indent(level + 1, os);
175
+ os << "{\n";
176
+
177
+ // Name
178
+ serialize(SerializeKey::Name, it->name, level + 2, false, os);
179
+ os << NewLineItemBlock;
180
+
181
+ // Description
182
+ serialize(SerializeKey::Description, it->description, level + 2, false, os);
183
+ os << NewLineItemBlock;
184
+
185
+ // Type
186
+ serialize(SerializeKey::Type, it->type, level + 2, false, os);
187
+ os << NewLineItemBlock;
188
+
189
+ // Requried
190
+ serialize(SerializeKey::Required, (it->use != OptionalParameterUse), level + 2, os);
191
+ os << NewLineItemBlock;
192
+
193
+ // Default
194
+ serialize(SerializeKey::Default, it->defaultValue, level + 2, false, os);
195
+ os << NewLineItemBlock;
196
+
197
+ // Example
198
+ serialize(SerializeKey::Example, it->exampleValue, level + 2, false, os);
199
+ os << NewLineItemBlock;
200
+
201
+ // Values
202
+ indent(level + 2, os);
203
+ serialize(SerializeKey::Values, os);
204
+ os << ": [";
205
+
206
+ if (!it->values.empty()) {
207
+ os << "\n";
208
+ size_t j = 0;
209
+ for (Collection<Value>::const_iterator val_it = it->values.begin(); val_it != it->values.end(); ++j, ++val_it) {
210
+
211
+ if (j > 0 && j < it->values.size())
212
+ os << NewLineItemBlock;
213
+
214
+ serialize(SerializeKey::Value, *val_it, level + 3, true, os);
215
+ }
216
+
217
+ os << "\n";
218
+ indent(level + 2, os);
219
+ }
220
+
221
+ // Close Values
222
+ os << "]";
223
+
224
+ // Close Key / name object
225
+ os << "\n";
226
+ indent(level + 1, os);
227
+ os << "}";
228
+ }
229
+
230
+ os << std::endl;
231
+ indent(level, os);
232
+ }
233
+
234
+ os << "]";
235
+ }
236
+
237
+ /**
238
+ * \brief Serialize HTTP headers into output stream.
239
+ * \param headers Headers to serialize.
240
+ * \param level Level of indentation.
241
+ * \param os An output stream to serialize into.
242
+ */
243
+ static void serialize(const Collection<Header>::type& headers, size_t level, std::ostream &os)
244
+ {
245
+ indent(level, os);
246
+ serialize(SerializeKey::Headers, os);
247
+ os << ": ";
248
+
249
+ serializeKeyValueCollection(headers, level, os);
250
+ }
251
+
252
+ /**
253
+ * \brief Serialize a payload into output stream.
254
+ * \param payload A payload to serialize.
255
+ * \param os An output stream to serialize into.
256
+ */
257
+ static void serialize(const Payload& payload, size_t level, std::ostream &os)
258
+ {
259
+ os << "{\n";
260
+
261
+ // Name
262
+ serialize(SerializeKey::Name, payload.name, level + 1, false, os);
263
+ os << NewLineItemBlock;
264
+
265
+ // Description
266
+ serialize(SerializeKey::Description, payload.description, level + 1, false, os);
267
+ os << NewLineItemBlock;
268
+
269
+ // Headers
270
+ serialize(payload.headers, level + 1, os);
271
+ os << NewLineItemBlock;
272
+
273
+ // Body
274
+ serialize(SerializeKey::Body, payload.body, level + 1, false, os);
275
+ os << NewLineItemBlock;
276
+
277
+ // Schema
278
+ serialize(SerializeKey::Schema, payload.schema, level + 1, false, os);
279
+
280
+ os << "\n";
281
+ indent(level, os);
282
+ os << "}";
283
+ }
284
+
285
+ /**
286
+ * \brief Serialize a transaction example into output stream.
287
+ * \param transaction A transaction example to serialize.
288
+ * \param os An output stream to serialize into.
289
+ */
290
+ static void serialize(const TransactionExample& example, std::ostream &os)
291
+ {
292
+ indent(8, os);
293
+ os << "{\n";
294
+
295
+ // Name
296
+ serialize(SerializeKey::Name, example.name, 9, false, os);
297
+ os << NewLineItemBlock;
298
+
299
+ // Description
300
+ serialize(SerializeKey::Description, example.description, 9, false, os);
301
+ os << NewLineItemBlock;
302
+
303
+ // Requests
304
+ indent(9, os);
305
+ serialize(SerializeKey::Requests, os);
306
+ os << ": ";
307
+ os << "[";
308
+
309
+ if (!example.requests.empty()) {
310
+ os << "\n";
311
+ size_t i = 0;
312
+ for (Collection<Request>::const_iterator it = example.requests.begin();
313
+ it != example.requests.end();
314
+ ++i, ++it) {
315
+
316
+ if (i > 0 && i < example.requests.size())
317
+ os << NewLineItemBlock;
318
+
319
+ indent(10, os);
320
+ serialize(*it, 10, os);
321
+ }
322
+
323
+ os << "\n";
324
+ indent(9, os);
325
+ }
326
+ os << "]";
327
+ os << NewLineItemBlock;
328
+
329
+ // Responses
330
+ indent(9, os);
331
+ serialize(SerializeKey::Responses, os);
332
+ os << ": ";
333
+ os << "[";
334
+
335
+ if (!example.responses.empty()) {
336
+ os << "\n";
337
+ size_t i = 0;
338
+ for (Collection<Response>::const_iterator it = example.responses.begin();
339
+ it != example.responses.end();
340
+ ++i, ++it) {
341
+
342
+ if (i > 0 && i < example.responses.size())
343
+ os << NewLineItemBlock;
344
+
345
+ indent(10, os);
346
+ serialize(*it, 10, os);
347
+ }
348
+
349
+ os << "\n";
350
+ indent(9, os);
351
+ }
352
+ os << "]";
353
+
354
+ // Close the transaction
355
+ os << "\n";
356
+ indent(8, os);
357
+ os << "}";
358
+ }
359
+
360
+ /**
361
+ * \brief Serialize an action into output stream.
362
+ * \param action The action to serialize.
363
+ * \param os An output stream to serialize into.
364
+ */
365
+ static void serialize(const Action& action, std::ostream &os)
366
+ {
367
+ indent(6, os);
368
+ os << "{\n";
369
+
370
+ // Name
371
+ serialize(SerializeKey::Name, action.name, 7, false, os);
372
+ os << NewLineItemBlock;
373
+
374
+ // Description
375
+ serialize(SerializeKey::Description, action.description, 7, false, os);
376
+ os << NewLineItemBlock;
377
+
378
+ // HTTP Method
379
+ serialize(SerializeKey::Method, action.method, 7, false, os);
380
+ os << NewLineItemBlock;
381
+
382
+ // Parameters
383
+ serialize(action.parameters, 7, os);
384
+ os << NewLineItemBlock;
385
+
386
+ // Transactions
387
+ indent(7, os);
388
+ serialize(SerializeKey::Examples, os);
389
+ os << ": ";
390
+ os << "[";
391
+
392
+ if (!action.examples.empty()) {
393
+ os << "\n";
394
+ size_t i = 0;
395
+ for (Collection<TransactionExample>::const_iterator it = action.examples.begin();
396
+ it != action.examples.end();
397
+ ++i, ++it) {
398
+
399
+ if (i > 0 && i < action.examples.size())
400
+ os << NewLineItemBlock;
401
+
402
+ serialize(*it, os);
403
+ }
404
+
405
+ os << "\n";
406
+ indent(7, os);
407
+ }
408
+
409
+ os << "]";
410
+
411
+ // Close the action
412
+ os << "\n";
413
+ indent(6, os);
414
+ os << "}";
415
+ }
416
+
417
+ /**
418
+ * \brief Serialize a resources into output stream.
419
+ * \param resource A resource to serialize
420
+ * \param os An output stream to serialize into
421
+ */
422
+ static void serialize(const Resource& resource, std::ostream &os)
423
+ {
424
+ indent(4, os);
425
+ os << "{\n";
426
+
427
+ // Name
428
+ serialize(SerializeKey::Name, resource.name, 5, false, os);
429
+ os << NewLineItemBlock;
430
+
431
+ // Description
432
+ serialize(SerializeKey::Description, resource.description, 5, false, os);
433
+ os << NewLineItemBlock;
434
+
435
+ // URI template
436
+ serialize(SerializeKey::URITemplate, resource.uriTemplate, 5, false, os);
437
+ os << NewLineItemBlock;
438
+
439
+ // Model
440
+ indent(5, os);
441
+ serialize(SerializeKey::Model, os);
442
+ if (resource.model.name.empty()) {
443
+ os << ": {}";
444
+ }
445
+ else {
446
+ os << ": ";
447
+
448
+ serialize(resource.model, 5, os);
449
+ }
450
+ os << NewLineItemBlock;
451
+
452
+ // Parameters
453
+ serialize(resource.parameters, 5, os);
454
+ os << NewLineItemBlock;
455
+
456
+ // Actions
457
+ indent(5, os);
458
+ serialize(SerializeKey::Actions, os);
459
+ os << ": ";
460
+ os << "[";
461
+
462
+ if (!resource.actions.empty()) {
463
+ os << "\n";
464
+
465
+ size_t i = 0;
466
+ for (Collection<Action>::const_iterator it = resource.actions.begin();
467
+ it != resource.actions.end();
468
+ ++i, ++it) {
469
+
470
+ if (i > 0 && i < resource.actions.size())
471
+ os << NewLineItemBlock;
472
+
473
+ serialize(*it, os);
474
+ }
475
+
476
+ os << "\n";
477
+ indent(5, os);
478
+ }
479
+ os << "]";
480
+
481
+ // Close the resource
482
+ os << "\n";
483
+ indent(4, os);
484
+ os << "}";
485
+ }
486
+
487
+ /**
488
+ * \brief Serialize a group of resources into output stream.
489
+ * \param resourceGroup A group to serialize.
490
+ * \brief os An output stream to serialize into.
491
+ */
492
+ static void serialize(const ResourceGroup& resourceGroup, std::ostream &os)
493
+ {
494
+ indent(2, os);
495
+ os << "{\n";
496
+
497
+ // Name
498
+ serialize(SerializeKey::Name, resourceGroup.name, 3, false, os);
499
+ os << NewLineItemBlock;
500
+
501
+ // Description
502
+ serialize(SerializeKey::Description, resourceGroup.description, 3, false, os);
503
+ os << NewLineItemBlock;
504
+
505
+ // Resources
506
+ indent(3, os);
507
+ serialize(SerializeKey::Resources, os);
508
+ os << ": ";
509
+ os << "[";
510
+
511
+ if (!resourceGroup.resources.empty()) {
512
+ os << "\n";
513
+ size_t i = 0;
514
+ for (Collection<Resource>::const_iterator it = resourceGroup.resources.begin();
515
+ it != resourceGroup.resources.end();
516
+ ++i, ++it) {
517
+
518
+ if (i > 0 && i < resourceGroup.resources.size())
519
+ os << NewLineItemBlock;
520
+
521
+ serialize(*it, os);
522
+ }
523
+
524
+ if (!resourceGroup.resources.empty()) {
525
+ os << "\n";
526
+ indent(3, os);
527
+ }
528
+ }
529
+
530
+ os << "]";
531
+
532
+ // Close the group
533
+ os << "\n";
534
+ indent(2, os);
535
+ os << "}";
536
+ }
537
+
538
+ /**
539
+ * \brief Serialize Resource Group into output stream.
540
+ * \param resourceGroup Resource Groups to serialize.
541
+ * \param os An output stream to serialize into.
542
+ */
543
+ static void serialize(const Collection<ResourceGroup>::type& resourceGroups, std::ostream &os)
544
+ {
545
+ indent(1, os);
546
+ serialize(SerializeKey::ResourceGroups, os);
547
+ os << ": ";
548
+ os << "[";
549
+
550
+ if (!resourceGroups.empty()) {
551
+ os << "\n";
552
+ size_t i = 0;
553
+ for (Collection<ResourceGroup>::const_iterator it = resourceGroups.begin(); it != resourceGroups.end(); ++i, ++it) {
554
+
555
+ if (i > 0 && i < resourceGroups.size())
556
+ os << NewLineItemBlock;
557
+
558
+ serialize(*it, os);
559
+ }
560
+
561
+ os << "\n";
562
+ indent(1, os);
563
+ }
564
+
565
+ os << "]";
566
+ }
567
+
568
+ /**
569
+ * \brief Serialize a blueprint into output stream.
570
+ * \param blueprint The blueprint to serialize.
571
+ * \param os An output stream to serialize into.
572
+ */
573
+ static void serialize(const Blueprint& blueprint, std::ostream &os)
574
+ {
575
+ os << "{\n";
576
+
577
+ // AST Version
578
+ serialize(SerializeKey::ASTVersion, AST_SERIALIZATION_VERSION, 1, false, os);
579
+ os << NewLineItemBlock;
580
+
581
+ // Metadata
582
+ serialize(blueprint.metadata, os);
583
+
584
+ // Name
585
+ serialize(SerializeKey::Name, blueprint.name, 1, false, os);
586
+ os << NewLineItemBlock;
587
+
588
+ // Description
589
+ serialize(SerializeKey::Description, blueprint.description, 1, false, os);
590
+ os << NewLineItemBlock;
591
+
592
+ // Resource Groups
593
+ serialize(blueprint.resourceGroups, os);
594
+
595
+ os << "\n}\n";
596
+ }
597
+
598
+ void snowcrash::SerializeJSON(const snowcrash::Blueprint& blueprint, std::ostream &os)
599
+ {
600
+ serialize(blueprint, os);
601
+ }