quicktype 18.0.5 → 19.0.4

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. package/README.md +1 -1
  2. package/dist/{cli/CompressedJSONFromStream.d.ts → CompressedJSONFromStream.d.ts} +1 -1
  3. package/dist/{cli/CompressedJSONFromStream.js → CompressedJSONFromStream.js} +2 -2
  4. package/dist/{cli/GraphQLIntrospection.d.ts → GraphQLIntrospection.d.ts} +0 -0
  5. package/dist/{cli/GraphQLIntrospection.js → GraphQLIntrospection.js} +1 -1
  6. package/dist/{cli/TypeSource.d.ts → TypeSource.d.ts} +2 -2
  7. package/dist/{cli/TypeSource.js → TypeSource.js} +0 -0
  8. package/dist/{cli/URLGrammar.d.ts → URLGrammar.d.ts} +0 -0
  9. package/dist/{cli/URLGrammar.js → URLGrammar.js} +1 -1
  10. package/dist/{cli/index.d.ts → index.d.ts} +1 -1
  11. package/dist/{cli/index.js → index.js} +4 -5
  12. package/package.json +61 -77
  13. package/dist/quicktype-core/Annotation.d.ts +0 -8
  14. package/dist/quicktype-core/Annotation.js +0 -16
  15. package/dist/quicktype-core/ConvenienceRenderer.d.ts +0 -138
  16. package/dist/quicktype-core/ConvenienceRenderer.js +0 -668
  17. package/dist/quicktype-core/DateTime.d.ts +0 -10
  18. package/dist/quicktype-core/DateTime.js +0 -57
  19. package/dist/quicktype-core/DeclarationIR.d.ts +0 -14
  20. package/dist/quicktype-core/DeclarationIR.js +0 -167
  21. package/dist/quicktype-core/EncodedMarkovChain.d.ts +0 -1
  22. package/dist/quicktype-core/EncodedMarkovChain.js +0 -4
  23. package/dist/quicktype-core/GatherNames.d.ts +0 -2
  24. package/dist/quicktype-core/GatherNames.js +0 -333
  25. package/dist/quicktype-core/Graph.d.ts +0 -12
  26. package/dist/quicktype-core/Graph.js +0 -220
  27. package/dist/quicktype-core/GraphRewriting.d.ts +0 -87
  28. package/dist/quicktype-core/GraphRewriting.js +0 -421
  29. package/dist/quicktype-core/MakeTransformations.d.ts +0 -4
  30. package/dist/quicktype-core/MakeTransformations.js +0 -193
  31. package/dist/quicktype-core/MarkovChain.d.ts +0 -15
  32. package/dist/quicktype-core/MarkovChain.js +0 -154
  33. package/dist/quicktype-core/Messages.d.ts +0 -294
  34. package/dist/quicktype-core/Messages.js +0 -105
  35. package/dist/quicktype-core/Naming.d.ts +0 -64
  36. package/dist/quicktype-core/Naming.js +0 -356
  37. package/dist/quicktype-core/Renderer.d.ts +0 -50
  38. package/dist/quicktype-core/Renderer.js +0 -274
  39. package/dist/quicktype-core/RendererOptions.d.ts +0 -71
  40. package/dist/quicktype-core/RendererOptions.js +0 -141
  41. package/dist/quicktype-core/Run.d.ts +0 -174
  42. package/dist/quicktype-core/Run.js +0 -393
  43. package/dist/quicktype-core/Source.d.ts +0 -65
  44. package/dist/quicktype-core/Source.js +0 -204
  45. package/dist/quicktype-core/TargetLanguage.d.ts +0 -34
  46. package/dist/quicktype-core/TargetLanguage.js +0 -66
  47. package/dist/quicktype-core/Transformers.d.ts +0 -209
  48. package/dist/quicktype-core/Transformers.js +0 -836
  49. package/dist/quicktype-core/Type.d.ts +0 -206
  50. package/dist/quicktype-core/Type.js +0 -716
  51. package/dist/quicktype-core/TypeBuilder.d.ts +0 -61
  52. package/dist/quicktype-core/TypeBuilder.js +0 -324
  53. package/dist/quicktype-core/TypeGraph.d.ts +0 -75
  54. package/dist/quicktype-core/TypeGraph.js +0 -420
  55. package/dist/quicktype-core/TypeUtils.d.ts +0 -33
  56. package/dist/quicktype-core/TypeUtils.js +0 -230
  57. package/dist/quicktype-core/UnifyClasses.d.ts +0 -16
  58. package/dist/quicktype-core/UnifyClasses.js +0 -159
  59. package/dist/quicktype-core/UnionBuilder.d.ts +0 -49
  60. package/dist/quicktype-core/UnionBuilder.js +0 -282
  61. package/dist/quicktype-core/attributes/AccessorNames.d.ts +0 -18
  62. package/dist/quicktype-core/attributes/AccessorNames.js +0 -182
  63. package/dist/quicktype-core/attributes/Constraints.d.ts +0 -38
  64. package/dist/quicktype-core/attributes/Constraints.js +0 -172
  65. package/dist/quicktype-core/attributes/Description.d.ts +0 -9
  66. package/dist/quicktype-core/attributes/Description.js +0 -97
  67. package/dist/quicktype-core/attributes/EnumValues.d.ts +0 -8
  68. package/dist/quicktype-core/attributes/EnumValues.js +0 -31
  69. package/dist/quicktype-core/attributes/StringTypes.d.ts +0 -28
  70. package/dist/quicktype-core/attributes/StringTypes.js +0 -189
  71. package/dist/quicktype-core/attributes/TypeAttributes.d.ts +0 -34
  72. package/dist/quicktype-core/attributes/TypeAttributes.js +0 -119
  73. package/dist/quicktype-core/attributes/TypeNames.d.ts +0 -46
  74. package/dist/quicktype-core/attributes/TypeNames.js +0 -274
  75. package/dist/quicktype-core/attributes/URIAttributes.d.ts +0 -8
  76. package/dist/quicktype-core/attributes/URIAttributes.js +0 -85
  77. package/dist/quicktype-core/index.d.ts +0 -48
  78. package/dist/quicktype-core/index.js +0 -180
  79. package/dist/quicktype-core/input/CompressedJSON.d.ts +0 -69
  80. package/dist/quicktype-core/input/CompressedJSON.js +0 -276
  81. package/dist/quicktype-core/input/FetchingJSONSchemaStore.d.ts +0 -6
  82. package/dist/quicktype-core/input/FetchingJSONSchemaStore.js +0 -18
  83. package/dist/quicktype-core/input/Inference.d.ts +0 -22
  84. package/dist/quicktype-core/input/Inference.js +0 -240
  85. package/dist/quicktype-core/input/Inputs.d.ts +0 -48
  86. package/dist/quicktype-core/input/Inputs.js +0 -145
  87. package/dist/quicktype-core/input/JSONSchemaInput.d.ts +0 -91
  88. package/dist/quicktype-core/input/JSONSchemaInput.js +0 -1041
  89. package/dist/quicktype-core/input/JSONSchemaStore.d.ts +0 -8
  90. package/dist/quicktype-core/input/JSONSchemaStore.js +0 -36
  91. package/dist/quicktype-core/input/PostmanCollection.d.ts +0 -5
  92. package/dist/quicktype-core/input/PostmanCollection.js +0 -60
  93. package/dist/quicktype-core/input/io/NodeIO.d.ts +0 -3
  94. package/dist/quicktype-core/input/io/NodeIO.js +0 -87
  95. package/dist/quicktype-core/input/io/get-stream/buffer-stream.d.ts +0 -2
  96. package/dist/quicktype-core/input/io/get-stream/buffer-stream.js +0 -45
  97. package/dist/quicktype-core/input/io/get-stream/index.d.ts +0 -9
  98. package/dist/quicktype-core/input/io/get-stream/index.js +0 -52
  99. package/dist/quicktype-core/language/All.d.ts +0 -3
  100. package/dist/quicktype-core/language/All.js +0 -57
  101. package/dist/quicktype-core/language/CPlusPlus.d.ts +0 -241
  102. package/dist/quicktype-core/language/CPlusPlus.js +0 -1847
  103. package/dist/quicktype-core/language/CSharp.d.ts +0 -201
  104. package/dist/quicktype-core/language/CSharp.js +0 -1850
  105. package/dist/quicktype-core/language/Crystal.d.ts +0 -37
  106. package/dist/quicktype-core/language/Crystal.js +0 -337
  107. package/dist/quicktype-core/language/Dart.d.ts +0 -66
  108. package/dist/quicktype-core/language/Dart.js +0 -576
  109. package/dist/quicktype-core/language/Elm.d.ts +0 -58
  110. package/dist/quicktype-core/language/Elm.js +0 -522
  111. package/dist/quicktype-core/language/Golang.d.ts +0 -51
  112. package/dist/quicktype-core/language/Golang.js +0 -460
  113. package/dist/quicktype-core/language/Haskell.d.ts +0 -56
  114. package/dist/quicktype-core/language/Haskell.js +0 -411
  115. package/dist/quicktype-core/language/JSONSchema.d.ts +0 -31
  116. package/dist/quicktype-core/language/JSONSchema.js +0 -169
  117. package/dist/quicktype-core/language/Java.d.ts +0 -125
  118. package/dist/quicktype-core/language/Java.js +0 -1081
  119. package/dist/quicktype-core/language/JavaScript.d.ts +0 -68
  120. package/dist/quicktype-core/language/JavaScript.js +0 -435
  121. package/dist/quicktype-core/language/JavaScriptPropTypes.d.ts +0 -40
  122. package/dist/quicktype-core/language/JavaScriptPropTypes.js +0 -213
  123. package/dist/quicktype-core/language/JavaScriptUnicodeMaps.d.ts +0 -2
  124. package/dist/quicktype-core/language/JavaScriptUnicodeMaps.js +0 -134
  125. package/dist/quicktype-core/language/Kotlin.d.ts +0 -109
  126. package/dist/quicktype-core/language/Kotlin.js +0 -832
  127. package/dist/quicktype-core/language/Objective-C.d.ts +0 -79
  128. package/dist/quicktype-core/language/Objective-C.js +0 -777
  129. package/dist/quicktype-core/language/Php.d.ts +0 -80
  130. package/dist/quicktype-core/language/Php.js +0 -702
  131. package/dist/quicktype-core/language/Pike.d.ts +0 -37
  132. package/dist/quicktype-core/language/Pike.js +0 -276
  133. package/dist/quicktype-core/language/Python.d.ts +0 -107
  134. package/dist/quicktype-core/language/Python.js +0 -969
  135. package/dist/quicktype-core/language/Rust.d.ts +0 -56
  136. package/dist/quicktype-core/language/Rust.js +0 -309
  137. package/dist/quicktype-core/language/Swift.d.ts +0 -96
  138. package/dist/quicktype-core/language/Swift.js +0 -1211
  139. package/dist/quicktype-core/language/TypeScriptFlow.d.ts +0 -77
  140. package/dist/quicktype-core/language/TypeScriptFlow.js +0 -286
  141. package/dist/quicktype-core/language/ruby/index.d.ts +0 -52
  142. package/dist/quicktype-core/language/ruby/index.js +0 -557
  143. package/dist/quicktype-core/language/ruby/keywords.d.ts +0 -3
  144. package/dist/quicktype-core/language/ruby/keywords.js +0 -358
  145. package/dist/quicktype-core/rewrites/CombineClasses.d.ts +0 -3
  146. package/dist/quicktype-core/rewrites/CombineClasses.js +0 -125
  147. package/dist/quicktype-core/rewrites/ExpandStrings.d.ts +0 -4
  148. package/dist/quicktype-core/rewrites/ExpandStrings.js +0 -143
  149. package/dist/quicktype-core/rewrites/FlattenStrings.d.ts +0 -3
  150. package/dist/quicktype-core/rewrites/FlattenStrings.js +0 -45
  151. package/dist/quicktype-core/rewrites/FlattenUnions.d.ts +0 -3
  152. package/dist/quicktype-core/rewrites/FlattenUnions.js +0 -47
  153. package/dist/quicktype-core/rewrites/InferMaps.d.ts +0 -3
  154. package/dist/quicktype-core/rewrites/InferMaps.js +0 -117
  155. package/dist/quicktype-core/rewrites/ReplaceObjectType.d.ts +0 -3
  156. package/dist/quicktype-core/rewrites/ReplaceObjectType.js +0 -60
  157. package/dist/quicktype-core/rewrites/ResolveIntersections.d.ts +0 -3
  158. package/dist/quicktype-core/rewrites/ResolveIntersections.js +0 -244
  159. package/dist/quicktype-core/support/Acronyms.d.ts +0 -10
  160. package/dist/quicktype-core/support/Acronyms.js +0 -1128
  161. package/dist/quicktype-core/support/Chance.d.ts +0 -38
  162. package/dist/quicktype-core/support/Chance.js +0 -2146
  163. package/dist/quicktype-core/support/Converters.d.ts +0 -6
  164. package/dist/quicktype-core/support/Converters.js +0 -16
  165. package/dist/quicktype-core/support/Strings.d.ts +0 -44
  166. package/dist/quicktype-core/support/Strings.js +0 -525
  167. package/dist/quicktype-core/support/Support.d.ts +0 -29
  168. package/dist/quicktype-core/support/Support.js +0 -162
  169. package/dist/quicktype-graphql-input/GraphQLSchema.d.ts +0 -645
  170. package/dist/quicktype-graphql-input/GraphQLSchema.js +0 -39
  171. package/dist/quicktype-graphql-input/index.d.ts +0 -17
  172. package/dist/quicktype-graphql-input/index.js +0 -407
  173. package/dist/quicktype-typescript-input/index.d.ts +0 -2
  174. package/dist/quicktype-typescript-input/index.js +0 -98
@@ -1,557 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.RubyRenderer = exports.RubyTargetLanguage = exports.rubyOptions = exports.Strictness = void 0;
27
- const unicode = require("unicode-properties");
28
- const Source_1 = require("../../Source");
29
- const Naming_1 = require("../../Naming");
30
- const ConvenienceRenderer_1 = require("../../ConvenienceRenderer");
31
- const TargetLanguage_1 = require("../../TargetLanguage");
32
- const RendererOptions_1 = require("../../RendererOptions");
33
- const keywords = __importStar(require("./keywords"));
34
- const forbiddenForObjectProperties = Array.from(new Set([...keywords.keywords, ...keywords.reservedProperties]));
35
- const Type_1 = require("../../Type");
36
- const TypeUtils_1 = require("../../TypeUtils");
37
- const Strings_1 = require("../../support/Strings");
38
- function unicodeEscape(codePoint) {
39
- return "\\u{" + (0, Strings_1.intToHex)(codePoint, 0) + "}";
40
- }
41
- const stringEscape = (0, Strings_1.utf32ConcatMap)((0, Strings_1.escapeNonPrintableMapper)(Strings_1.isPrintable, unicodeEscape));
42
- var Strictness;
43
- (function (Strictness) {
44
- Strictness["Strict"] = "Strict::";
45
- Strictness["Coercible"] = "Coercible::";
46
- Strictness["None"] = "Types::";
47
- })(Strictness = exports.Strictness || (exports.Strictness = {}));
48
- exports.rubyOptions = {
49
- justTypes: new RendererOptions_1.BooleanOption("just-types", "Plain types only", false),
50
- strictness: new RendererOptions_1.EnumOption("strictness", "Type strictness", [
51
- ["strict", Strictness.Strict],
52
- ["coercible", Strictness.Coercible],
53
- ["none", Strictness.None]
54
- ]),
55
- namespace: new RendererOptions_1.StringOption("namespace", "Specify a wrapping Namespace", "NAME", "", "secondary")
56
- };
57
- class RubyTargetLanguage extends TargetLanguage_1.TargetLanguage {
58
- constructor() {
59
- super("Ruby", ["ruby"], "rb");
60
- }
61
- getOptions() {
62
- return [exports.rubyOptions.justTypes, exports.rubyOptions.strictness, exports.rubyOptions.namespace];
63
- }
64
- get supportsOptionalClassProperties() {
65
- return true;
66
- }
67
- get defaultIndentation() {
68
- return " ";
69
- }
70
- makeRenderer(renderContext, untypedOptionValues) {
71
- return new RubyRenderer(this, renderContext, (0, RendererOptions_1.getOptionValues)(exports.rubyOptions, untypedOptionValues));
72
- }
73
- }
74
- exports.RubyTargetLanguage = RubyTargetLanguage;
75
- const isStartCharacter = Strings_1.isLetterOrUnderscore;
76
- function isPartCharacter(utf16Unit) {
77
- const category = unicode.getCategory(utf16Unit);
78
- return ["Nd", "Pc", "Mn", "Mc"].indexOf(category) >= 0 || isStartCharacter(utf16Unit);
79
- }
80
- const legalizeName = (0, Strings_1.legalizeCharacters)(isPartCharacter);
81
- function simpleNameStyle(original, uppercase) {
82
- if (/^[0-9]+$/.test(original)) {
83
- original = original + "N";
84
- }
85
- const words = (0, Strings_1.splitIntoWords)(original);
86
- return (0, Strings_1.combineWords)(words, legalizeName, uppercase ? Strings_1.firstUpperWordStyle : Strings_1.allLowerWordStyle, uppercase ? Strings_1.firstUpperWordStyle : Strings_1.allLowerWordStyle, Strings_1.allUpperWordStyle, Strings_1.allUpperWordStyle, "", isStartCharacter);
87
- }
88
- function memberNameStyle(original) {
89
- const words = (0, Strings_1.splitIntoWords)(original);
90
- return (0, Strings_1.combineWords)(words, legalizeName, Strings_1.allLowerWordStyle, Strings_1.allLowerWordStyle, Strings_1.allLowerWordStyle, Strings_1.allLowerWordStyle, "_", isStartCharacter);
91
- }
92
- class RubyRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
93
- _options;
94
- constructor(targetLanguage, renderContext, _options) {
95
- super(targetLanguage, renderContext);
96
- this._options = _options;
97
- }
98
- get commentLineStart() {
99
- return "# ";
100
- }
101
- get needsTypeDeclarationBeforeUse() {
102
- return true;
103
- }
104
- canBeForwardDeclared(t) {
105
- return "class" === t.kind;
106
- }
107
- forbiddenNamesForGlobalNamespace() {
108
- return keywords.globals.concat(["Types", "JSON", "Dry", "Constructor", "Self"]);
109
- }
110
- forbiddenForObjectProperties(_c, _classNamed) {
111
- return { names: forbiddenForObjectProperties, includeGlobalForbidden: true };
112
- }
113
- makeNamedTypeNamer() {
114
- return new Naming_1.Namer("types", n => simpleNameStyle(n, true), []);
115
- }
116
- namerForObjectProperty() {
117
- return new Naming_1.Namer("properties", memberNameStyle, []);
118
- }
119
- makeUnionMemberNamer() {
120
- return new Naming_1.Namer("properties", memberNameStyle, []);
121
- }
122
- makeEnumCaseNamer() {
123
- return new Naming_1.Namer("enum-cases", n => simpleNameStyle(n, true), []);
124
- }
125
- dryType(t, isOptional = false) {
126
- const optional = isOptional ? ".optional" : "";
127
- return (0, TypeUtils_1.matchType)(t, _anyType => ["Types::Any", optional], _nullType => ["Types::Nil", optional], _boolType => ["Types::Bool", optional], _integerType => ["Types::Integer", optional], _doubleType => ["Types::Double", optional], _stringType => ["Types::String", optional], arrayType => ["Types.Array(", this.dryType(arrayType.items), ")", optional], classType => [this.nameForNamedType(classType), optional], mapType => ["Types::Hash.meta(of: ", this.dryType(mapType.values), ")", optional], enumType => ["Types::", this.nameForNamedType(enumType), optional], unionType => {
128
- const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
129
- if (nullable !== null) {
130
- return [this.dryType(nullable), ".optional"];
131
- }
132
- return ["Types.Instance(", this.nameForNamedType(unionType), ")", optional];
133
- });
134
- }
135
- exampleUse(t, exp, depth = 6, optional = false) {
136
- if (depth-- <= 0) {
137
- return exp;
138
- }
139
- const safeNav = optional ? "&" : "";
140
- return (0, TypeUtils_1.matchType)(t, _anyType => exp, _nullType => [exp, ".nil?"], _boolType => exp, _integerType => [exp, ".even?"], _doubleType => exp, _stringType => exp, arrayType => this.exampleUse(arrayType.items, [exp, safeNav, ".first"], depth), classType => {
141
- let info;
142
- this.forEachClassProperty(classType, "none", (name, _json, prop) => {
143
- if (["class", "map", "array"].indexOf(prop.type.kind) >= 0) {
144
- info = { name, prop };
145
- }
146
- else if (info === undefined) {
147
- info = { name, prop };
148
- }
149
- });
150
- if (info !== undefined) {
151
- return this.exampleUse(info.prop.type, [exp, safeNav, ".", info.name], depth, info.prop.isOptional);
152
- }
153
- return exp;
154
- }, mapType => this.exampleUse(mapType.values, [exp, safeNav, `["…"]`], depth), enumType => {
155
- let name;
156
- // FIXME: This is a terrible way to get the first enum case name.
157
- this.forEachEnumCase(enumType, "none", theName => {
158
- if (name === undefined) {
159
- name = theName;
160
- }
161
- });
162
- if (name !== undefined) {
163
- return [exp, " == ", this.nameForNamedType(enumType), "::", name];
164
- }
165
- return exp;
166
- }, unionType => {
167
- const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
168
- if (nullable !== null) {
169
- if (["class", "map", "array"].indexOf(nullable.kind) >= 0) {
170
- return this.exampleUse(nullable, exp, depth, true);
171
- }
172
- return [exp, ".nil?"];
173
- }
174
- return exp;
175
- });
176
- }
177
- jsonSample(t) {
178
- function inner() {
179
- if (t instanceof Type_1.ArrayType) {
180
- return "[…]";
181
- }
182
- else if (t instanceof Type_1.MapType) {
183
- return "{…}";
184
- }
185
- else if (t instanceof Type_1.ClassType) {
186
- return "{…}";
187
- }
188
- else {
189
- return "…";
190
- }
191
- }
192
- return `"${inner()}"`;
193
- }
194
- fromDynamic(t, e, optional = false, castPrimitives = false) {
195
- const primitiveCast = [this.dryType(t, optional), "[", e, "]"];
196
- const primitive = castPrimitives ? primitiveCast : e;
197
- const safeAccess = optional ? "&" : "";
198
- return (0, TypeUtils_1.matchType)(t, _anyType => primitive, _nullType => primitive, _boolType => primitive, _integerType => primitive, _doubleType => primitive, _stringType => primitive, arrayType => [e, safeAccess, ".map { |x| ", this.fromDynamic(arrayType.items, "x", false, true), " }"], classType => {
199
- const expression = [this.nameForNamedType(classType), ".from_dynamic!(", e, ")"];
200
- return optional ? [e, " ? ", expression, " : nil"] : expression;
201
- }, mapType => [
202
- ["Types::Hash", optional ? ".optional" : "", "[", e, "]"],
203
- safeAccess,
204
- ".map { |k, v| [k, ",
205
- this.fromDynamic(mapType.values, "v", false, true),
206
- "] }",
207
- safeAccess,
208
- ".to_h"
209
- ], enumType => {
210
- const expression = ["Types::", this.nameForNamedType(enumType), "[", e, "]"];
211
- return optional ? [e, ".nil? ? nil : ", expression] : expression;
212
- }, unionType => {
213
- const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
214
- if (nullable !== null) {
215
- return this.fromDynamic(nullable, e, true);
216
- }
217
- const expression = [this.nameForNamedType(unionType), ".from_dynamic!(", e, ")"];
218
- return optional ? [e, " ? ", expression, " : nil"] : expression;
219
- });
220
- }
221
- toDynamic(t, e, optional = false) {
222
- if (this.marshalsImplicitlyToDynamic(t)) {
223
- return e;
224
- }
225
- return (0, TypeUtils_1.matchType)(t, _anyType => e, _nullType => e, _boolType => e, _integerType => e, _doubleType => e, _stringType => e, arrayType => [e, optional ? "&" : "", ".map { |x| ", this.toDynamic(arrayType.items, "x"), " }"], _classType => [e, optional ? "&" : "", ".to_dynamic"], mapType => [e, optional ? "&" : "", ".map { |k, v| [k, ", this.toDynamic(mapType.values, "v"), "] }.to_h"], _enumType => e, unionType => {
226
- const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
227
- if (nullable !== null) {
228
- return this.toDynamic(nullable, e, true);
229
- }
230
- if (this.marshalsImplicitlyToDynamic(unionType)) {
231
- return e;
232
- }
233
- return [e, optional ? "&" : "", ".to_dynamic"];
234
- });
235
- }
236
- marshalsImplicitlyToDynamic(t) {
237
- return (0, TypeUtils_1.matchType)(t, _anyType => true, _nullType => true, _boolType => true, _integerType => true, _doubleType => true, _stringType => true, arrayType => this.marshalsImplicitlyToDynamic(arrayType.items), _classType => false, mapType => this.marshalsImplicitlyToDynamic(mapType.values), _enumType => true, unionType => {
238
- const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
239
- if (nullable !== null) {
240
- return this.marshalsImplicitlyToDynamic(nullable);
241
- }
242
- return false;
243
- });
244
- }
245
- // This is only to be used to allow class properties to possibly
246
- // marshal implicitly. They are allowed to do this because they will
247
- // be checked in Dry::Struct.new
248
- propertyTypeMarshalsImplicitlyFromDynamic(t) {
249
- return (0, TypeUtils_1.matchType)(t, _anyType => true, _nullType => true, _boolType => true, _integerType => true, _doubleType => true, _stringType => true, arrayType => this.propertyTypeMarshalsImplicitlyFromDynamic(arrayType.items), _classType => false,
250
- // Map properties must be checked because Dry:Types doesn't have a generic Map
251
- _mapType => false, _enumType => true, unionType => {
252
- const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
253
- if (nullable !== null) {
254
- return this.propertyTypeMarshalsImplicitlyFromDynamic(nullable);
255
- }
256
- return false;
257
- });
258
- }
259
- emitBlock(source, emit) {
260
- this.emitLine(source);
261
- this.indent(emit);
262
- this.emitLine("end");
263
- }
264
- emitModule(emit) {
265
- const emitModuleInner = (moduleName) => {
266
- const [firstModule, ...subModules] = moduleName.split("::");
267
- if (subModules.length > 0) {
268
- this.emitBlock(["module ", firstModule], () => {
269
- emitModuleInner(subModules.join("::"));
270
- });
271
- }
272
- else {
273
- this.emitBlock(["module ", moduleName], emit);
274
- }
275
- };
276
- if (this._options.namespace !== undefined && this._options.namespace !== "") {
277
- emitModuleInner(this._options.namespace);
278
- }
279
- else {
280
- emit();
281
- }
282
- }
283
- emitClass(c, className) {
284
- this.emitDescription(this.descriptionForType(c));
285
- this.emitBlock(["class ", className, " < Dry::Struct"], () => {
286
- let table = [];
287
- let count = c.getProperties().size;
288
- this.forEachClassProperty(c, "none", (name, jsonName, p) => {
289
- const last = --count === 0;
290
- const description = this.descriptionForClassProperty(c, jsonName);
291
- const attribute = [
292
- ["attribute :", name, ","],
293
- [" ", this.dryType(p.type), p.isOptional ? ".optional" : ""]
294
- ];
295
- if (description !== undefined) {
296
- if (table.length > 0) {
297
- this.emitTable(table);
298
- table = [];
299
- }
300
- this.ensureBlankLine();
301
- this.emitDescriptionBlock(description);
302
- this.emitLine(attribute);
303
- if (!last) {
304
- this.ensureBlankLine();
305
- }
306
- }
307
- else {
308
- table.push(attribute);
309
- }
310
- });
311
- if (table.length > 0) {
312
- this.emitTable(table);
313
- }
314
- if (this._options.justTypes) {
315
- return;
316
- }
317
- this.ensureBlankLine();
318
- this.emitBlock(["def self.from_dynamic!(d)"], () => {
319
- this.emitLine("d = Types::Hash[d]");
320
- this.emitLine("new(");
321
- this.indent(() => {
322
- const inits = [];
323
- this.forEachClassProperty(c, "none", (name, jsonName, p) => {
324
- const dynamic = p.isOptional
325
- ? // If key is not found in hash, this will be nil
326
- `d["${stringEscape(jsonName)}"]`
327
- : // This will raise a runtime error if the key is not found in the hash
328
- `d.fetch("${stringEscape(jsonName)}")`;
329
- if (this.propertyTypeMarshalsImplicitlyFromDynamic(p.type)) {
330
- inits.push([
331
- [name, ": "],
332
- [dynamic, ","]
333
- ]);
334
- }
335
- else {
336
- const expression = this.fromDynamic(p.type, dynamic, p.isOptional);
337
- inits.push([
338
- [name, ": "],
339
- [expression, ","]
340
- ]);
341
- }
342
- });
343
- this.emitTable(inits);
344
- });
345
- this.emitLine(")");
346
- });
347
- this.ensureBlankLine();
348
- this.emitBlock("def self.from_json!(json)", () => {
349
- this.emitLine("from_dynamic!(JSON.parse(json))");
350
- });
351
- this.ensureBlankLine();
352
- this.emitBlock(["def to_dynamic"], () => {
353
- this.emitLine("{");
354
- this.indent(() => {
355
- const inits = [];
356
- this.forEachClassProperty(c, "none", (name, jsonName, p) => {
357
- const expression = this.toDynamic(p.type, name, p.isOptional);
358
- inits.push([[`"${stringEscape(jsonName)}"`], [" => ", expression, ","]]);
359
- });
360
- this.emitTable(inits);
361
- });
362
- this.emitLine("}");
363
- });
364
- this.ensureBlankLine();
365
- this.emitBlock("def to_json(options = nil)", () => {
366
- this.emitLine("JSON.generate(to_dynamic, options)");
367
- });
368
- });
369
- }
370
- emitEnum(e, enumName) {
371
- this.emitDescription(this.descriptionForType(e));
372
- this.emitBlock(["module ", enumName], () => {
373
- const table = [];
374
- this.forEachEnumCase(e, "none", (name, json) => {
375
- table.push([[name], [` = "${stringEscape(json)}"`]]);
376
- });
377
- this.emitTable(table);
378
- });
379
- }
380
- emitUnion(u, unionName) {
381
- this.emitDescription(this.descriptionForType(u));
382
- this.emitBlock(["class ", unionName, " < Dry::Struct"], () => {
383
- const table = [];
384
- this.forEachUnionMember(u, u.getChildren(), "none", null, (name, t) => {
385
- table.push([["attribute :", name, ", "], [this.dryType(t, true)]]);
386
- });
387
- this.emitTable(table);
388
- if (this._options.justTypes) {
389
- return;
390
- }
391
- this.ensureBlankLine();
392
- const [maybeNull, nonNulls] = (0, TypeUtils_1.removeNullFromUnion)(u, false);
393
- this.emitBlock("def self.from_dynamic!(d)", () => {
394
- const memberNames = Array.from(u.getChildren()).map(member => this.nameForUnionMember(u, member));
395
- this.forEachUnionMember(u, u.getChildren(), "none", null, (name, t) => {
396
- const nilMembers = memberNames
397
- .filter(n => n !== name)
398
- .map(memberName => [", ", memberName, ": nil"]);
399
- if (this.propertyTypeMarshalsImplicitlyFromDynamic(t)) {
400
- this.emitBlock(["if schema[:", name, "].right.valid? d"], () => {
401
- this.emitLine("return new(", name, ": d", nilMembers, ")");
402
- });
403
- }
404
- else {
405
- this.emitLine("begin");
406
- this.indent(() => {
407
- this.emitLine("value = ", this.fromDynamic(t, "d"));
408
- this.emitBlock(["if schema[:", name, "].right.valid? value"], () => {
409
- this.emitLine("return new(", name, ": value", nilMembers, ")");
410
- });
411
- });
412
- this.emitLine("rescue");
413
- this.emitLine("end");
414
- }
415
- });
416
- this.emitLine(`raise "Invalid union"`);
417
- });
418
- this.ensureBlankLine();
419
- this.emitBlock("def self.from_json!(json)", () => {
420
- this.emitLine("from_dynamic!(JSON.parse(json))");
421
- });
422
- this.ensureBlankLine();
423
- this.emitBlock("def to_dynamic", () => {
424
- let first = true;
425
- this.forEachUnionMember(u, nonNulls, "none", null, (name, t) => {
426
- this.emitLine(first ? "if" : "elsif", " ", name, " != nil");
427
- this.indent(() => {
428
- this.emitLine(this.toDynamic(t, name));
429
- });
430
- first = false;
431
- });
432
- if (maybeNull !== null) {
433
- this.emitLine("else");
434
- this.indent(() => {
435
- this.emitLine("nil");
436
- });
437
- }
438
- this.emitLine("end");
439
- });
440
- this.ensureBlankLine();
441
- this.emitBlock("def to_json(options = nil)", () => {
442
- this.emitLine("JSON.generate(to_dynamic, options)");
443
- });
444
- });
445
- }
446
- emitTypesModule() {
447
- this.emitBlock(["module Types"], () => {
448
- this.emitLine("include Dry.Types(default: :nominal)");
449
- const declarations = [];
450
- if (this._options.strictness !== Strictness.None) {
451
- let has = { int: false, nil: false, bool: false, hash: false, string: false, double: false };
452
- this.forEachType(t => {
453
- has = {
454
- int: has.int || t.kind === "integer",
455
- nil: has.nil || t.kind === "null",
456
- bool: has.bool || t.kind === "bool",
457
- hash: has.hash || t.kind === "map" || t.kind === "class",
458
- string: has.string || t.kind === "string" || t.kind === "enum",
459
- double: has.double || t.kind === "double"
460
- };
461
- });
462
- if (has.int)
463
- declarations.push([["Integer"], [` = ${this._options.strictness}Integer`]]);
464
- if (this._options.strictness === Strictness.Strict) {
465
- if (has.nil)
466
- declarations.push([["Nil"], [` = ${this._options.strictness}Nil`]]);
467
- }
468
- if (has.bool)
469
- declarations.push([["Bool"], [` = ${this._options.strictness}Bool`]]);
470
- if (has.hash)
471
- declarations.push([["Hash"], [` = ${this._options.strictness}Hash`]]);
472
- if (has.string)
473
- declarations.push([["String"], [` = ${this._options.strictness}String`]]);
474
- if (has.double)
475
- declarations.push([
476
- ["Double"],
477
- [` = ${this._options.strictness}Float | ${this._options.strictness}Integer`]
478
- ]);
479
- }
480
- this.forEachEnum("none", (enumType, enumName) => {
481
- const cases = [];
482
- this.forEachEnumCase(enumType, "none", (_name, json) => {
483
- cases.push([cases.length === 0 ? "" : ", ", `"${stringEscape(json)}"`]);
484
- });
485
- declarations.push([[enumName], [" = ", this._options.strictness, "String.enum(", ...cases, ")"]]);
486
- });
487
- if (declarations.length > 0) {
488
- this.ensureBlankLine();
489
- this.emitTable(declarations);
490
- }
491
- });
492
- }
493
- emitSourceStructure() {
494
- if (this.leadingComments !== undefined) {
495
- this.emitCommentLines(this.leadingComments);
496
- }
497
- else if (!this._options.justTypes) {
498
- this.emitLine("# This code may look unusually verbose for Ruby (and it is), but");
499
- this.emitLine("# it performs some subtle and complex validation of JSON data.");
500
- this.emitLine("#");
501
- this.emitLine("# To parse this JSON, add 'dry-struct' and 'dry-types' gems, then do:");
502
- this.emitLine("#");
503
- this.forEachTopLevel("none", (topLevel, name) => {
504
- const variable = (0, Source_1.modifySource)(Strings_1.snakeCase, name);
505
- this.emitLine("# ", variable, " = ", name, ".from_json! ", this.jsonSample(topLevel));
506
- this.emitLine("# puts ", this.exampleUse(topLevel, variable));
507
- this.emitLine("#");
508
- });
509
- this.emitLine("# If from_json! succeeds, the value returned matches the schema.");
510
- }
511
- this.ensureBlankLine();
512
- this.emitLine("require 'json'");
513
- this.emitLine("require 'dry-types'");
514
- this.emitLine("require 'dry-struct'");
515
- this.ensureBlankLine();
516
- this.emitModule(() => {
517
- this.emitTypesModule();
518
- this.forEachDeclaration("leading-and-interposing", decl => {
519
- if (decl.kind === "forward") {
520
- this.emitCommentLines(["(forward declaration)"]);
521
- this.emitModule(() => {
522
- this.emitLine("class ", this.nameForNamedType(decl.type), " < Dry::Struct; end");
523
- });
524
- }
525
- });
526
- this.forEachNamedType("leading-and-interposing", (c, n) => this.emitClass(c, n), (e, n) => this.emitEnum(e, n), (u, n) => this.emitUnion(u, n));
527
- if (!this._options.justTypes) {
528
- this.forEachTopLevel("leading-and-interposing", (topLevel, name) => {
529
- const self = (0, Source_1.modifySource)(Strings_1.snakeCase, name);
530
- // The json gem defines to_json on maps and primitives, so we only need to supply
531
- // it for arrays.
532
- const needsToJsonDefined = "array" === topLevel.kind;
533
- const classDeclaration = () => {
534
- this.emitBlock(["class ", name], () => {
535
- this.emitBlock(["def self.from_json!(json)"], () => {
536
- if (needsToJsonDefined) {
537
- this.emitLine(self, " = ", this.fromDynamic(topLevel, "JSON.parse(json, quirks_mode: true)"));
538
- this.emitBlock([self, ".define_singleton_method(:to_json) do"], () => {
539
- this.emitLine("JSON.generate(", this.toDynamic(topLevel, "self"), ")");
540
- });
541
- this.emitLine(self);
542
- }
543
- else {
544
- this.emitLine(this.fromDynamic(topLevel, "JSON.parse(json, quirks_mode: true)"));
545
- }
546
- });
547
- });
548
- };
549
- this.emitModule(() => {
550
- classDeclaration();
551
- });
552
- }, t => this.namedTypeToNameForTopLevel(t) === undefined);
553
- }
554
- });
555
- }
556
- }
557
- exports.RubyRenderer = RubyRenderer;
@@ -1,3 +0,0 @@
1
- export declare const keywords: string[];
2
- export declare const globals: string[];
3
- export declare const reservedProperties: string[];