forked from apiaryio/drafter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialize.h
283 lines (236 loc) · 9.28 KB
/
Serialize.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Serialize.h
// drafter
//
// Created by Zdenek Nemec on 5/3/13.
// Copyright (c) 2013 Apiary Inc. All rights reserved.
//
#ifndef DRAFTER_SERIALIZE_H
#define DRAFTER_SERIALIZE_H
#include <string>
#include "BlueprintSourcemap.h"
#include "sos.h"
#include "NodeInfo.h"
#include "refract/Element.h"
#include "refract/Registry.h"
/** Version of API Blueprint serialization */
#define AST_SERIALIZATION_VERSION "4.0"
#define PARSE_RESULT_SERIALIZATION_VERSION "2.2"
// Forward declaration
namespace snowcrash
{
template <typename T>
struct ParseResult;
}
namespace drafter
{
enum SerializeFormat
{
JSONFormat = 0, // JSON Format
YAMLFormat, // YAML Format
UnknownFormat = -1
};
// Options struct for drafter
struct WrapperOptions {
const bool generateSourceMap;
const bool expandMSON;
WrapperOptions(const bool generateSourceMap, const bool expandMSON)
: generateSourceMap(generateSourceMap), expandMSON(expandMSON)
{
}
WrapperOptions(const bool generateSourceMap) : generateSourceMap(generateSourceMap), expandMSON(false) {}
WrapperOptions() : generateSourceMap(false), expandMSON(false) {}
};
/**
* AST and Refract entities serialization keys
*/
struct SerializeKey {
static const std::string Metadata;
static const std::string Reference;
static const std::string Id;
static const std::string Name;
static const std::string Description;
static const std::string DataStructure;
static const std::string DataStructures;
static const std::string ResourceGroup;
static const std::string ResourceGroups;
static const std::string Resource;
static const std::string Resources;
static const std::string URI;
static const std::string URITemplate;
static const std::string Assets;
static const std::string Actions;
static const std::string Action;
static const std::string Relation;
static const std::string Attributes;
static const std::string Examples;
static const std::string Transaction;
static const std::string Method;
static const std::string Requests;
static const std::string Responses;
static const std::string Body;
static const std::string Schema;
static const std::string Headers;
static const std::string Model;
static const std::string Value;
static const std::string Parameters;
static const std::string Type;
static const std::string Required;
static const std::string Default;
static const std::string Enumerations;
static const std::string Nullable;
static const std::string Example;
static const std::string Values;
static const std::string Source;
static const std::string Resolved;
static const std::string Literal;
static const std::string Variable;
static const std::string TypeDefinition;
static const std::string TypeSpecification;
static const std::string NestedTypes;
static const std::string Sections;
static const std::string Class;
static const std::string Content;
static const std::string ValueDefinition;
static const std::string Element;
static const std::string Role;
static const std::string Version;
static const std::string Ast;
static const std::string Sourcemap;
static const std::string Error;
static const std::string Warning;
static const std::string Warnings;
static const std::string AnnotationCode;
static const std::string AnnotationMessage;
static const std::string AnnotationLocation;
static const std::string AnnotationLocationIndex;
static const std::string AnnotationLocationLength;
// Refract meta
static const std::string Meta;
static const std::string Title;
static const std::string Classes;
// Refract MSON attributes
static const std::string Samples;
static const std::string TypeAttributes;
// Refract MSON attribute "typeAttibute" values
static const std::string Optional;
static const std::string Fixed;
static const std::string FixedType;
// Literal to Bool
static const std::string True;
// Refract MSON generic element
static const std::string Generic;
// Refract (nontyped) element names
static const std::string Enum;
static const std::string Ref;
// Refract Ref Element - keys/values
static const std::string Href;
static const std::string Path;
// API Namespace
static const std::string Category;
static const std::string Copy;
static const std::string API;
static const std::string User;
static const std::string Transition;
static const std::string HrefVariables;
static const std::string HTTPHeaders;
static const std::string HTTPTransaction;
static const std::string ContentType;
static const std::string HTTPResponse;
static const std::string HTTPRequest;
static const std::string StatusCode;
static const std::string Asset;
static const std::string MessageBody;
static const std::string MessageBodySchema;
static const std::string Data;
// Parse Result Namespace
static const std::string ParseResult;
static const std::string Annotation;
static const std::string SourceMap;
};
template <typename T>
std::pair<bool, T> LiteralTo(const mson::Literal&);
template <>
std::pair<bool, refract::dsd::Boolean> LiteralTo<refract::dsd::Boolean>(const mson::Literal& literal);
template <>
std::pair<bool, refract::dsd::Number> LiteralTo<refract::dsd::Number>(const mson::Literal& literal);
template <>
std::pair<bool, refract::dsd::String> LiteralTo<refract::dsd::String>(const mson::Literal& literal);
/**
* \brief functor pattern to translate _collection_ into sos::Array on serialization
* \requests for collection - must define typedef member ::const_iterator
*
* usage:
*
* sos::Array elements = WrapCollection<mson::Element>()(getSomeListOfElements(), WrapMSONElement));
*
* operator()(const T& collection, Functor &wrapper)
* \param collection - it come typicaly from snowcrash
* \param wrapper - adaptee element before push to collection
* you have to write your own, for example \see SeriallizeAST.cc
*
*/
template <typename T, typename R = sos::Array>
struct WrapCollection {
typedef T value_type;
template <typename Collection, typename Functor>
R operator()(const Collection& collection, Functor& wrapper) const
{
typedef typename Collection::const_iterator iterator_type;
R array;
for (iterator_type it = collection.begin(); it != collection.end(); ++it) {
array.push(wrapper(*it));
}
return array;
}
template <typename Collection, typename Functor, typename Arg1>
R operator()(const Collection& collection, Functor& wrapper, Arg1 arg1) const
{
typedef typename Collection::const_iterator iterator_type;
R array;
for (iterator_type it = collection.begin(); it != collection.end(); ++it) {
array.push(wrapper(*it, arg1));
}
return array;
}
template <typename Collection, typename Functor, typename Arg1, typename Arg2>
R operator()(const Collection& collection, Functor& wrapper, Arg1 arg1, Arg2 arg2) const
{
typedef typename Collection::const_iterator iterator_type;
R array;
for (iterator_type it = collection.begin(); it != collection.end(); ++it) {
array.push(wrapper(*it, arg1, arg2));
}
return array;
}
};
template <typename T, typename R = sos::Array>
struct WrapCollectionIf {
// When we want to use predicate, let's use a dummy argument to distinguish it
template <typename Collection, typename Functor, typename Predicate>
R operator()(const Collection& collection, Functor& wrapper, Predicate& predicate) const
{
typedef typename Collection::const_iterator iterator_type;
R array;
for (iterator_type it = collection.begin(); it != collection.end(); ++it) {
if (predicate(*it)) {
array.push(wrapper(*it));
}
}
return array;
}
template <typename Collection, typename Functor, typename Predicate, typename Arg1>
R operator()(const Collection& collection, Functor& wrapper, Predicate& predicate, Arg1& arg1) const
{
typedef typename Collection::const_iterator iterator_type;
R array;
for (iterator_type it = collection.begin(); it != collection.end(); ++it) {
if (predicate(*it)) {
array.push(wrapper(*it, arg1));
}
}
return array;
}
};
}
#endif