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.
- package/README.md +1 -1
- package/dist/{cli/CompressedJSONFromStream.d.ts → CompressedJSONFromStream.d.ts} +1 -1
- package/dist/{cli/CompressedJSONFromStream.js → CompressedJSONFromStream.js} +2 -2
- package/dist/{cli/GraphQLIntrospection.d.ts → GraphQLIntrospection.d.ts} +0 -0
- package/dist/{cli/GraphQLIntrospection.js → GraphQLIntrospection.js} +1 -1
- package/dist/{cli/TypeSource.d.ts → TypeSource.d.ts} +2 -2
- package/dist/{cli/TypeSource.js → TypeSource.js} +0 -0
- package/dist/{cli/URLGrammar.d.ts → URLGrammar.d.ts} +0 -0
- package/dist/{cli/URLGrammar.js → URLGrammar.js} +1 -1
- package/dist/{cli/index.d.ts → index.d.ts} +1 -1
- package/dist/{cli/index.js → index.js} +4 -5
- package/package.json +61 -77
- package/dist/quicktype-core/Annotation.d.ts +0 -8
- package/dist/quicktype-core/Annotation.js +0 -16
- package/dist/quicktype-core/ConvenienceRenderer.d.ts +0 -138
- package/dist/quicktype-core/ConvenienceRenderer.js +0 -668
- package/dist/quicktype-core/DateTime.d.ts +0 -10
- package/dist/quicktype-core/DateTime.js +0 -57
- package/dist/quicktype-core/DeclarationIR.d.ts +0 -14
- package/dist/quicktype-core/DeclarationIR.js +0 -167
- package/dist/quicktype-core/EncodedMarkovChain.d.ts +0 -1
- package/dist/quicktype-core/EncodedMarkovChain.js +0 -4
- package/dist/quicktype-core/GatherNames.d.ts +0 -2
- package/dist/quicktype-core/GatherNames.js +0 -333
- package/dist/quicktype-core/Graph.d.ts +0 -12
- package/dist/quicktype-core/Graph.js +0 -220
- package/dist/quicktype-core/GraphRewriting.d.ts +0 -87
- package/dist/quicktype-core/GraphRewriting.js +0 -421
- package/dist/quicktype-core/MakeTransformations.d.ts +0 -4
- package/dist/quicktype-core/MakeTransformations.js +0 -193
- package/dist/quicktype-core/MarkovChain.d.ts +0 -15
- package/dist/quicktype-core/MarkovChain.js +0 -154
- package/dist/quicktype-core/Messages.d.ts +0 -294
- package/dist/quicktype-core/Messages.js +0 -105
- package/dist/quicktype-core/Naming.d.ts +0 -64
- package/dist/quicktype-core/Naming.js +0 -356
- package/dist/quicktype-core/Renderer.d.ts +0 -50
- package/dist/quicktype-core/Renderer.js +0 -274
- package/dist/quicktype-core/RendererOptions.d.ts +0 -71
- package/dist/quicktype-core/RendererOptions.js +0 -141
- package/dist/quicktype-core/Run.d.ts +0 -174
- package/dist/quicktype-core/Run.js +0 -393
- package/dist/quicktype-core/Source.d.ts +0 -65
- package/dist/quicktype-core/Source.js +0 -204
- package/dist/quicktype-core/TargetLanguage.d.ts +0 -34
- package/dist/quicktype-core/TargetLanguage.js +0 -66
- package/dist/quicktype-core/Transformers.d.ts +0 -209
- package/dist/quicktype-core/Transformers.js +0 -836
- package/dist/quicktype-core/Type.d.ts +0 -206
- package/dist/quicktype-core/Type.js +0 -716
- package/dist/quicktype-core/TypeBuilder.d.ts +0 -61
- package/dist/quicktype-core/TypeBuilder.js +0 -324
- package/dist/quicktype-core/TypeGraph.d.ts +0 -75
- package/dist/quicktype-core/TypeGraph.js +0 -420
- package/dist/quicktype-core/TypeUtils.d.ts +0 -33
- package/dist/quicktype-core/TypeUtils.js +0 -230
- package/dist/quicktype-core/UnifyClasses.d.ts +0 -16
- package/dist/quicktype-core/UnifyClasses.js +0 -159
- package/dist/quicktype-core/UnionBuilder.d.ts +0 -49
- package/dist/quicktype-core/UnionBuilder.js +0 -282
- package/dist/quicktype-core/attributes/AccessorNames.d.ts +0 -18
- package/dist/quicktype-core/attributes/AccessorNames.js +0 -182
- package/dist/quicktype-core/attributes/Constraints.d.ts +0 -38
- package/dist/quicktype-core/attributes/Constraints.js +0 -172
- package/dist/quicktype-core/attributes/Description.d.ts +0 -9
- package/dist/quicktype-core/attributes/Description.js +0 -97
- package/dist/quicktype-core/attributes/EnumValues.d.ts +0 -8
- package/dist/quicktype-core/attributes/EnumValues.js +0 -31
- package/dist/quicktype-core/attributes/StringTypes.d.ts +0 -28
- package/dist/quicktype-core/attributes/StringTypes.js +0 -189
- package/dist/quicktype-core/attributes/TypeAttributes.d.ts +0 -34
- package/dist/quicktype-core/attributes/TypeAttributes.js +0 -119
- package/dist/quicktype-core/attributes/TypeNames.d.ts +0 -46
- package/dist/quicktype-core/attributes/TypeNames.js +0 -274
- package/dist/quicktype-core/attributes/URIAttributes.d.ts +0 -8
- package/dist/quicktype-core/attributes/URIAttributes.js +0 -85
- package/dist/quicktype-core/index.d.ts +0 -48
- package/dist/quicktype-core/index.js +0 -180
- package/dist/quicktype-core/input/CompressedJSON.d.ts +0 -69
- package/dist/quicktype-core/input/CompressedJSON.js +0 -276
- package/dist/quicktype-core/input/FetchingJSONSchemaStore.d.ts +0 -6
- package/dist/quicktype-core/input/FetchingJSONSchemaStore.js +0 -18
- package/dist/quicktype-core/input/Inference.d.ts +0 -22
- package/dist/quicktype-core/input/Inference.js +0 -240
- package/dist/quicktype-core/input/Inputs.d.ts +0 -48
- package/dist/quicktype-core/input/Inputs.js +0 -145
- package/dist/quicktype-core/input/JSONSchemaInput.d.ts +0 -91
- package/dist/quicktype-core/input/JSONSchemaInput.js +0 -1041
- package/dist/quicktype-core/input/JSONSchemaStore.d.ts +0 -8
- package/dist/quicktype-core/input/JSONSchemaStore.js +0 -36
- package/dist/quicktype-core/input/PostmanCollection.d.ts +0 -5
- package/dist/quicktype-core/input/PostmanCollection.js +0 -60
- package/dist/quicktype-core/input/io/NodeIO.d.ts +0 -3
- package/dist/quicktype-core/input/io/NodeIO.js +0 -87
- package/dist/quicktype-core/input/io/get-stream/buffer-stream.d.ts +0 -2
- package/dist/quicktype-core/input/io/get-stream/buffer-stream.js +0 -45
- package/dist/quicktype-core/input/io/get-stream/index.d.ts +0 -9
- package/dist/quicktype-core/input/io/get-stream/index.js +0 -52
- package/dist/quicktype-core/language/All.d.ts +0 -3
- package/dist/quicktype-core/language/All.js +0 -57
- package/dist/quicktype-core/language/CPlusPlus.d.ts +0 -241
- package/dist/quicktype-core/language/CPlusPlus.js +0 -1847
- package/dist/quicktype-core/language/CSharp.d.ts +0 -201
- package/dist/quicktype-core/language/CSharp.js +0 -1850
- package/dist/quicktype-core/language/Crystal.d.ts +0 -37
- package/dist/quicktype-core/language/Crystal.js +0 -337
- package/dist/quicktype-core/language/Dart.d.ts +0 -66
- package/dist/quicktype-core/language/Dart.js +0 -576
- package/dist/quicktype-core/language/Elm.d.ts +0 -58
- package/dist/quicktype-core/language/Elm.js +0 -522
- package/dist/quicktype-core/language/Golang.d.ts +0 -51
- package/dist/quicktype-core/language/Golang.js +0 -460
- package/dist/quicktype-core/language/Haskell.d.ts +0 -56
- package/dist/quicktype-core/language/Haskell.js +0 -411
- package/dist/quicktype-core/language/JSONSchema.d.ts +0 -31
- package/dist/quicktype-core/language/JSONSchema.js +0 -169
- package/dist/quicktype-core/language/Java.d.ts +0 -125
- package/dist/quicktype-core/language/Java.js +0 -1081
- package/dist/quicktype-core/language/JavaScript.d.ts +0 -68
- package/dist/quicktype-core/language/JavaScript.js +0 -435
- package/dist/quicktype-core/language/JavaScriptPropTypes.d.ts +0 -40
- package/dist/quicktype-core/language/JavaScriptPropTypes.js +0 -213
- package/dist/quicktype-core/language/JavaScriptUnicodeMaps.d.ts +0 -2
- package/dist/quicktype-core/language/JavaScriptUnicodeMaps.js +0 -134
- package/dist/quicktype-core/language/Kotlin.d.ts +0 -109
- package/dist/quicktype-core/language/Kotlin.js +0 -832
- package/dist/quicktype-core/language/Objective-C.d.ts +0 -79
- package/dist/quicktype-core/language/Objective-C.js +0 -777
- package/dist/quicktype-core/language/Php.d.ts +0 -80
- package/dist/quicktype-core/language/Php.js +0 -702
- package/dist/quicktype-core/language/Pike.d.ts +0 -37
- package/dist/quicktype-core/language/Pike.js +0 -276
- package/dist/quicktype-core/language/Python.d.ts +0 -107
- package/dist/quicktype-core/language/Python.js +0 -969
- package/dist/quicktype-core/language/Rust.d.ts +0 -56
- package/dist/quicktype-core/language/Rust.js +0 -309
- package/dist/quicktype-core/language/Swift.d.ts +0 -96
- package/dist/quicktype-core/language/Swift.js +0 -1211
- package/dist/quicktype-core/language/TypeScriptFlow.d.ts +0 -77
- package/dist/quicktype-core/language/TypeScriptFlow.js +0 -286
- package/dist/quicktype-core/language/ruby/index.d.ts +0 -52
- package/dist/quicktype-core/language/ruby/index.js +0 -557
- package/dist/quicktype-core/language/ruby/keywords.d.ts +0 -3
- package/dist/quicktype-core/language/ruby/keywords.js +0 -358
- package/dist/quicktype-core/rewrites/CombineClasses.d.ts +0 -3
- package/dist/quicktype-core/rewrites/CombineClasses.js +0 -125
- package/dist/quicktype-core/rewrites/ExpandStrings.d.ts +0 -4
- package/dist/quicktype-core/rewrites/ExpandStrings.js +0 -143
- package/dist/quicktype-core/rewrites/FlattenStrings.d.ts +0 -3
- package/dist/quicktype-core/rewrites/FlattenStrings.js +0 -45
- package/dist/quicktype-core/rewrites/FlattenUnions.d.ts +0 -3
- package/dist/quicktype-core/rewrites/FlattenUnions.js +0 -47
- package/dist/quicktype-core/rewrites/InferMaps.d.ts +0 -3
- package/dist/quicktype-core/rewrites/InferMaps.js +0 -117
- package/dist/quicktype-core/rewrites/ReplaceObjectType.d.ts +0 -3
- package/dist/quicktype-core/rewrites/ReplaceObjectType.js +0 -60
- package/dist/quicktype-core/rewrites/ResolveIntersections.d.ts +0 -3
- package/dist/quicktype-core/rewrites/ResolveIntersections.js +0 -244
- package/dist/quicktype-core/support/Acronyms.d.ts +0 -10
- package/dist/quicktype-core/support/Acronyms.js +0 -1128
- package/dist/quicktype-core/support/Chance.d.ts +0 -38
- package/dist/quicktype-core/support/Chance.js +0 -2146
- package/dist/quicktype-core/support/Converters.d.ts +0 -6
- package/dist/quicktype-core/support/Converters.js +0 -16
- package/dist/quicktype-core/support/Strings.d.ts +0 -44
- package/dist/quicktype-core/support/Strings.js +0 -525
- package/dist/quicktype-core/support/Support.d.ts +0 -29
- package/dist/quicktype-core/support/Support.js +0 -162
- package/dist/quicktype-graphql-input/GraphQLSchema.d.ts +0 -645
- package/dist/quicktype-graphql-input/GraphQLSchema.js +0 -39
- package/dist/quicktype-graphql-input/index.d.ts +0 -17
- package/dist/quicktype-graphql-input/index.js +0 -407
- package/dist/quicktype-typescript-input/index.d.ts +0 -2
- package/dist/quicktype-typescript-input/index.js +0 -98
|
@@ -1,832 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KotlinXRenderer = exports.KotlinJacksonRenderer = exports.KotlinKlaxonRenderer = exports.KotlinRenderer = exports.KotlinTargetLanguage = exports.kotlinOptions = exports.Framework = void 0;
|
|
4
|
-
const collection_utils_1 = require("collection-utils");
|
|
5
|
-
const Annotation_1 = require("../Annotation");
|
|
6
|
-
const ConvenienceRenderer_1 = require("../ConvenienceRenderer");
|
|
7
|
-
const Naming_1 = require("../Naming");
|
|
8
|
-
const RendererOptions_1 = require("../RendererOptions");
|
|
9
|
-
const Source_1 = require("../Source");
|
|
10
|
-
const Strings_1 = require("../support/Strings");
|
|
11
|
-
const Support_1 = require("../support/Support");
|
|
12
|
-
const TargetLanguage_1 = require("../TargetLanguage");
|
|
13
|
-
const Type_1 = require("../Type");
|
|
14
|
-
const TypeUtils_1 = require("../TypeUtils");
|
|
15
|
-
const Acronyms_1 = require("../support/Acronyms");
|
|
16
|
-
var Framework;
|
|
17
|
-
(function (Framework) {
|
|
18
|
-
Framework[Framework["None"] = 0] = "None";
|
|
19
|
-
Framework[Framework["Jackson"] = 1] = "Jackson";
|
|
20
|
-
Framework[Framework["Klaxon"] = 2] = "Klaxon";
|
|
21
|
-
Framework[Framework["KotlinX"] = 3] = "KotlinX";
|
|
22
|
-
})(Framework = exports.Framework || (exports.Framework = {}));
|
|
23
|
-
exports.kotlinOptions = {
|
|
24
|
-
framework: new RendererOptions_1.EnumOption("framework", "Serialization framework", [
|
|
25
|
-
["just-types", Framework.None],
|
|
26
|
-
["jackson", Framework.Jackson],
|
|
27
|
-
["klaxon", Framework.Klaxon],
|
|
28
|
-
["kotlinx", Framework.KotlinX]
|
|
29
|
-
], "klaxon"),
|
|
30
|
-
acronymStyle: (0, Acronyms_1.acronymOption)(Acronyms_1.AcronymStyleOptions.Pascal),
|
|
31
|
-
packageName: new RendererOptions_1.StringOption("package", "Package", "PACKAGE", "quicktype")
|
|
32
|
-
};
|
|
33
|
-
class KotlinTargetLanguage extends TargetLanguage_1.TargetLanguage {
|
|
34
|
-
constructor() {
|
|
35
|
-
super("Kotlin", ["kotlin"], "kt");
|
|
36
|
-
}
|
|
37
|
-
getOptions() {
|
|
38
|
-
return [exports.kotlinOptions.framework, exports.kotlinOptions.acronymStyle, exports.kotlinOptions.packageName];
|
|
39
|
-
}
|
|
40
|
-
get supportsOptionalClassProperties() {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
get supportsUnionsWithBothNumberTypes() {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
makeRenderer(renderContext, untypedOptionValues) {
|
|
47
|
-
const options = (0, RendererOptions_1.getOptionValues)(exports.kotlinOptions, untypedOptionValues);
|
|
48
|
-
switch (options.framework) {
|
|
49
|
-
case Framework.None:
|
|
50
|
-
return new KotlinRenderer(this, renderContext, options);
|
|
51
|
-
case Framework.Jackson:
|
|
52
|
-
return new KotlinJacksonRenderer(this, renderContext, options);
|
|
53
|
-
case Framework.Klaxon:
|
|
54
|
-
return new KotlinKlaxonRenderer(this, renderContext, options);
|
|
55
|
-
case Framework.KotlinX:
|
|
56
|
-
return new KotlinXRenderer(this, renderContext, options);
|
|
57
|
-
default:
|
|
58
|
-
return (0, Support_1.assertNever)(options.framework);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.KotlinTargetLanguage = KotlinTargetLanguage;
|
|
63
|
-
const keywords = [
|
|
64
|
-
"package",
|
|
65
|
-
"as",
|
|
66
|
-
"typealias",
|
|
67
|
-
"class",
|
|
68
|
-
"this",
|
|
69
|
-
"super",
|
|
70
|
-
"val",
|
|
71
|
-
"var",
|
|
72
|
-
"fun",
|
|
73
|
-
"for",
|
|
74
|
-
"null",
|
|
75
|
-
"true",
|
|
76
|
-
"false",
|
|
77
|
-
"is",
|
|
78
|
-
"in",
|
|
79
|
-
"throw",
|
|
80
|
-
"return",
|
|
81
|
-
"break",
|
|
82
|
-
"continue",
|
|
83
|
-
"object",
|
|
84
|
-
"if",
|
|
85
|
-
"try",
|
|
86
|
-
"else",
|
|
87
|
-
"while",
|
|
88
|
-
"do",
|
|
89
|
-
"when",
|
|
90
|
-
"interface",
|
|
91
|
-
"typeof",
|
|
92
|
-
"klaxon",
|
|
93
|
-
"toJson",
|
|
94
|
-
"Any",
|
|
95
|
-
"Boolean",
|
|
96
|
-
"Double",
|
|
97
|
-
"Float",
|
|
98
|
-
"Long",
|
|
99
|
-
"Int",
|
|
100
|
-
"Short",
|
|
101
|
-
"System",
|
|
102
|
-
"Byte",
|
|
103
|
-
"String",
|
|
104
|
-
"Array",
|
|
105
|
-
"List",
|
|
106
|
-
"Map",
|
|
107
|
-
"Enum",
|
|
108
|
-
"Class",
|
|
109
|
-
"JsonObject",
|
|
110
|
-
"JsonValue",
|
|
111
|
-
"Converter",
|
|
112
|
-
"Klaxon"
|
|
113
|
-
];
|
|
114
|
-
function isPartCharacter(codePoint) {
|
|
115
|
-
return (0, Strings_1.isLetterOrUnderscore)(codePoint) || (0, Strings_1.isNumeric)(codePoint);
|
|
116
|
-
}
|
|
117
|
-
function isStartCharacter(codePoint) {
|
|
118
|
-
return isPartCharacter(codePoint) && !(0, Strings_1.isDigit)(codePoint);
|
|
119
|
-
}
|
|
120
|
-
const legalizeName = (0, Strings_1.legalizeCharacters)(isPartCharacter);
|
|
121
|
-
function kotlinNameStyle(isUpper, original, acronymsStyle = Strings_1.allUpperWordStyle) {
|
|
122
|
-
const words = (0, Strings_1.splitIntoWords)(original);
|
|
123
|
-
return (0, Strings_1.combineWords)(words, legalizeName, isUpper ? Strings_1.firstUpperWordStyle : Strings_1.allLowerWordStyle, Strings_1.firstUpperWordStyle, isUpper ? Strings_1.allUpperWordStyle : Strings_1.allLowerWordStyle, acronymsStyle, "", isStartCharacter);
|
|
124
|
-
}
|
|
125
|
-
function unicodeEscape(codePoint) {
|
|
126
|
-
return "\\u" + (0, Strings_1.intToHex)(codePoint, 4);
|
|
127
|
-
}
|
|
128
|
-
const _stringEscape = (0, Strings_1.utf32ConcatMap)((0, Strings_1.escapeNonPrintableMapper)(Strings_1.isPrintable, unicodeEscape));
|
|
129
|
-
function stringEscape(s) {
|
|
130
|
-
// "$this" is a template string in Kotlin so we have to escape $
|
|
131
|
-
return _stringEscape(s).replace(/\$/g, "\\$");
|
|
132
|
-
}
|
|
133
|
-
class KotlinRenderer extends ConvenienceRenderer_1.ConvenienceRenderer {
|
|
134
|
-
_kotlinOptions;
|
|
135
|
-
constructor(targetLanguage, renderContext, _kotlinOptions) {
|
|
136
|
-
super(targetLanguage, renderContext);
|
|
137
|
-
this._kotlinOptions = _kotlinOptions;
|
|
138
|
-
}
|
|
139
|
-
forbiddenNamesForGlobalNamespace() {
|
|
140
|
-
return keywords;
|
|
141
|
-
}
|
|
142
|
-
forbiddenForObjectProperties(_o, _classNamed) {
|
|
143
|
-
return { names: [], includeGlobalForbidden: true };
|
|
144
|
-
}
|
|
145
|
-
forbiddenForEnumCases(_e, _enumName) {
|
|
146
|
-
return { names: [], includeGlobalForbidden: true };
|
|
147
|
-
}
|
|
148
|
-
forbiddenForUnionMembers(_u, _unionName) {
|
|
149
|
-
return { names: [], includeGlobalForbidden: false };
|
|
150
|
-
}
|
|
151
|
-
topLevelNameStyle(rawName) {
|
|
152
|
-
return kotlinNameStyle(true, rawName);
|
|
153
|
-
}
|
|
154
|
-
makeNamedTypeNamer() {
|
|
155
|
-
return (0, Naming_1.funPrefixNamer)("upper", s => kotlinNameStyle(true, s, (0, Acronyms_1.acronymStyle)(this._kotlinOptions.acronymStyle)));
|
|
156
|
-
}
|
|
157
|
-
namerForObjectProperty() {
|
|
158
|
-
return (0, Naming_1.funPrefixNamer)("lower", s => kotlinNameStyle(false, s, (0, Acronyms_1.acronymStyle)(this._kotlinOptions.acronymStyle)));
|
|
159
|
-
}
|
|
160
|
-
makeUnionMemberNamer() {
|
|
161
|
-
return (0, Naming_1.funPrefixNamer)("upper", s => kotlinNameStyle(true, s) + "Value");
|
|
162
|
-
}
|
|
163
|
-
makeEnumCaseNamer() {
|
|
164
|
-
return (0, Naming_1.funPrefixNamer)("upper", s => kotlinNameStyle(true, s, (0, Acronyms_1.acronymStyle)(this._kotlinOptions.acronymStyle)));
|
|
165
|
-
}
|
|
166
|
-
emitDescriptionBlock(lines) {
|
|
167
|
-
this.emitCommentLines(lines, " * ", "/**", " */");
|
|
168
|
-
}
|
|
169
|
-
emitBlock(line, f, delimiter = "curly") {
|
|
170
|
-
const [open, close] = delimiter === "curly" ? ["{", "}"] : delimiter === "paren" ? ["(", ")"] : ["{", "})"];
|
|
171
|
-
this.emitLine(line, " ", open);
|
|
172
|
-
this.indent(f);
|
|
173
|
-
this.emitLine(close);
|
|
174
|
-
}
|
|
175
|
-
anySourceType(optional) {
|
|
176
|
-
return ["Any", optional];
|
|
177
|
-
}
|
|
178
|
-
// (asarazan): I've broken out the following two functions
|
|
179
|
-
// because some renderers, such as kotlinx, can cope with `any`, while some get mad.
|
|
180
|
-
arrayType(arrayType, withIssues = false, _noOptional = false) {
|
|
181
|
-
return ["List<", this.kotlinType(arrayType.items, withIssues), ">"];
|
|
182
|
-
}
|
|
183
|
-
mapType(mapType, withIssues = false, _noOptional = false) {
|
|
184
|
-
return ["Map<String, ", this.kotlinType(mapType.values, withIssues), ">"];
|
|
185
|
-
}
|
|
186
|
-
kotlinType(t, withIssues = false, noOptional = false) {
|
|
187
|
-
const optional = noOptional ? "" : "?";
|
|
188
|
-
return (0, TypeUtils_1.matchType)(t, _anyType => {
|
|
189
|
-
return (0, Source_1.maybeAnnotated)(withIssues, Annotation_1.anyTypeIssueAnnotation, this.anySourceType(optional));
|
|
190
|
-
}, _nullType => {
|
|
191
|
-
return (0, Source_1.maybeAnnotated)(withIssues, Annotation_1.nullTypeIssueAnnotation, this.anySourceType(optional));
|
|
192
|
-
}, _boolType => "Boolean", _integerType => "Long", _doubleType => "Double", _stringType => "String", arrayType => this.arrayType(arrayType, withIssues), classType => this.nameForNamedType(classType), mapType => this.mapType(mapType, withIssues), enumType => this.nameForNamedType(enumType), unionType => {
|
|
193
|
-
const nullable = (0, TypeUtils_1.nullableFromUnion)(unionType);
|
|
194
|
-
if (nullable !== null)
|
|
195
|
-
return [this.kotlinType(nullable, withIssues), optional];
|
|
196
|
-
return this.nameForNamedType(unionType);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
emitUsageHeader() {
|
|
200
|
-
// To be overridden
|
|
201
|
-
}
|
|
202
|
-
emitHeader() {
|
|
203
|
-
if (this.leadingComments !== undefined) {
|
|
204
|
-
this.emitCommentLines(this.leadingComments);
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
this.emitUsageHeader();
|
|
208
|
-
}
|
|
209
|
-
this.ensureBlankLine();
|
|
210
|
-
this.emitLine("package ", this._kotlinOptions.packageName);
|
|
211
|
-
this.ensureBlankLine();
|
|
212
|
-
}
|
|
213
|
-
emitTopLevelArray(t, name) {
|
|
214
|
-
const elementType = this.kotlinType(t.items);
|
|
215
|
-
this.emitLine(["typealias ", name, " = ArrayList<", elementType, ">"]);
|
|
216
|
-
}
|
|
217
|
-
emitTopLevelMap(t, name) {
|
|
218
|
-
const elementType = this.kotlinType(t.values);
|
|
219
|
-
this.emitLine(["typealias ", name, " = HashMap<String, ", elementType, ">"]);
|
|
220
|
-
}
|
|
221
|
-
emitEmptyClassDefinition(c, className) {
|
|
222
|
-
this.emitDescription(this.descriptionForType(c));
|
|
223
|
-
this.emitClassAnnotations(c, className);
|
|
224
|
-
this.emitLine("class ", className, "()");
|
|
225
|
-
}
|
|
226
|
-
emitClassDefinition(c, className) {
|
|
227
|
-
if (c.getProperties().size === 0) {
|
|
228
|
-
this.emitEmptyClassDefinition(c, className);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
const kotlinType = (p) => {
|
|
232
|
-
if (p.isOptional) {
|
|
233
|
-
return [this.kotlinType(p.type, true, true), "?"];
|
|
234
|
-
}
|
|
235
|
-
else {
|
|
236
|
-
return this.kotlinType(p.type, true);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
this.emitDescription(this.descriptionForType(c));
|
|
240
|
-
this.emitClassAnnotations(c, className);
|
|
241
|
-
this.emitLine("data class ", className, " (");
|
|
242
|
-
this.indent(() => {
|
|
243
|
-
let count = c.getProperties().size;
|
|
244
|
-
let first = true;
|
|
245
|
-
this.forEachClassProperty(c, "none", (name, jsonName, p) => {
|
|
246
|
-
const nullable = p.type.kind === "union" && (0, TypeUtils_1.nullableFromUnion)(p.type) !== null;
|
|
247
|
-
const nullableOrOptional = p.isOptional || p.type.kind === "null" || nullable;
|
|
248
|
-
const last = --count === 0;
|
|
249
|
-
let meta = [];
|
|
250
|
-
const description = this.descriptionForClassProperty(c, jsonName);
|
|
251
|
-
if (description !== undefined) {
|
|
252
|
-
meta.push(() => this.emitDescription(description));
|
|
253
|
-
}
|
|
254
|
-
this.renameAttribute(name, jsonName, !nullableOrOptional, meta);
|
|
255
|
-
if (meta.length > 0 && !first) {
|
|
256
|
-
this.ensureBlankLine();
|
|
257
|
-
}
|
|
258
|
-
for (const emit of meta) {
|
|
259
|
-
emit();
|
|
260
|
-
}
|
|
261
|
-
this.emitLine("val ", name, ": ", kotlinType(p), nullableOrOptional ? " = null" : "", last ? "" : ",");
|
|
262
|
-
if (meta.length > 0 && !last) {
|
|
263
|
-
this.ensureBlankLine();
|
|
264
|
-
}
|
|
265
|
-
first = false;
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
this.emitClassDefinitionMethods(c, className);
|
|
269
|
-
}
|
|
270
|
-
emitClassDefinitionMethods(_c, _className) {
|
|
271
|
-
this.emitLine(")");
|
|
272
|
-
}
|
|
273
|
-
emitClassAnnotations(_c, _className) {
|
|
274
|
-
// to be overridden
|
|
275
|
-
}
|
|
276
|
-
renameAttribute(_name, _jsonName, _required, _meta) {
|
|
277
|
-
// to be overridden
|
|
278
|
-
}
|
|
279
|
-
emitEnumDefinition(e, enumName) {
|
|
280
|
-
this.emitDescription(this.descriptionForType(e));
|
|
281
|
-
this.emitBlock(["enum class ", enumName], () => {
|
|
282
|
-
let count = e.cases.size;
|
|
283
|
-
this.forEachEnumCase(e, "none", name => {
|
|
284
|
-
this.emitLine(name, --count === 0 ? "" : ",");
|
|
285
|
-
});
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
emitUnionDefinition(u, unionName) {
|
|
289
|
-
function sortBy(t) {
|
|
290
|
-
const kind = t.kind;
|
|
291
|
-
if (kind === "class")
|
|
292
|
-
return kind;
|
|
293
|
-
return "_" + kind;
|
|
294
|
-
}
|
|
295
|
-
this.emitDescription(this.descriptionForType(u));
|
|
296
|
-
const [maybeNull, nonNulls] = (0, TypeUtils_1.removeNullFromUnion)(u, sortBy);
|
|
297
|
-
this.emitClassAnnotations(u, unionName);
|
|
298
|
-
this.emitBlock(["sealed class ", unionName], () => {
|
|
299
|
-
{
|
|
300
|
-
let table = [];
|
|
301
|
-
this.forEachUnionMember(u, nonNulls, "none", null, (name, t) => {
|
|
302
|
-
table.push([
|
|
303
|
-
["class ", name, "(val value: ", this.kotlinType(t), ")"],
|
|
304
|
-
[" : ", unionName, "()"]
|
|
305
|
-
]);
|
|
306
|
-
});
|
|
307
|
-
if (maybeNull !== null) {
|
|
308
|
-
table.push([
|
|
309
|
-
["class ", this.nameForUnionMember(u, maybeNull), "()"],
|
|
310
|
-
[" : ", unionName, "()"]
|
|
311
|
-
]);
|
|
312
|
-
}
|
|
313
|
-
this.emitTable(table);
|
|
314
|
-
}
|
|
315
|
-
this.emitUnionDefinitionMethods(u, nonNulls, maybeNull, unionName);
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
emitUnionDefinitionMethods(_u, _nonNulls, _maybeNull, _unionName) {
|
|
319
|
-
// to be overridden
|
|
320
|
-
}
|
|
321
|
-
emitSourceStructure() {
|
|
322
|
-
this.emitHeader();
|
|
323
|
-
// Top-level arrays, maps
|
|
324
|
-
this.forEachTopLevel("leading", (t, name) => {
|
|
325
|
-
if (t instanceof Type_1.ArrayType) {
|
|
326
|
-
this.emitTopLevelArray(t, name);
|
|
327
|
-
}
|
|
328
|
-
else if (t instanceof Type_1.MapType) {
|
|
329
|
-
this.emitTopLevelMap(t, name);
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
this.forEachNamedType("leading-and-interposing", (c, n) => this.emitClassDefinition(c, n), (e, n) => this.emitEnumDefinition(e, n), (u, n) => this.emitUnionDefinition(u, n));
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
exports.KotlinRenderer = KotlinRenderer;
|
|
336
|
-
class KotlinKlaxonRenderer extends KotlinRenderer {
|
|
337
|
-
constructor(targetLanguage, renderContext, _kotlinOptions) {
|
|
338
|
-
super(targetLanguage, renderContext, _kotlinOptions);
|
|
339
|
-
}
|
|
340
|
-
unionMemberFromJsonValue(t, e) {
|
|
341
|
-
return (0, TypeUtils_1.matchType)(t, _anyType => [e, ".inside"], _nullType => "null", _boolType => [e, ".boolean"], _integerType => ["(", e, ".int?.toLong() ?: ", e, ".longValue)"], _doubleType => [e, ".double"], _stringType => [e, ".string"], arrayType => [e, ".array?.let { klaxon.parseFromJsonArray<", this.kotlinType(arrayType.items), ">(it) }"], _classType => [e, ".obj?.let { klaxon.parseFromJsonObject<", this.kotlinType(t), ">(it) }"], _mapType => [e, ".obj?.let { klaxon.parseFromJsonObject<", this.kotlinType(t), ">(it) }"], enumType => [e, ".string?.let { ", this.kotlinType(enumType), ".fromValue(it) }"], _unionType => (0, Support_1.mustNotHappen)());
|
|
342
|
-
}
|
|
343
|
-
unionMemberJsonValueGuard(t, _e) {
|
|
344
|
-
return (0, TypeUtils_1.matchType)(t, _anyType => "is Any", _nullType => "null", _boolType => "is Boolean", _integerType => "is Int, is Long", _doubleType => "is Double", _stringType => "is String", _arrayType => "is JsonArray<*>",
|
|
345
|
-
// These could be stricter, but for now we don't allow maps
|
|
346
|
-
// and objects in the same union
|
|
347
|
-
_classType => "is JsonObject", _mapType => "is JsonObject",
|
|
348
|
-
// This could be stricter, but for now we don't allow strings
|
|
349
|
-
// and enums in the same union
|
|
350
|
-
_enumType => "is String", _unionType => (0, Support_1.mustNotHappen)());
|
|
351
|
-
}
|
|
352
|
-
emitUsageHeader() {
|
|
353
|
-
this.emitLine("// To parse the JSON, install Klaxon and do:");
|
|
354
|
-
this.emitLine("//");
|
|
355
|
-
this.forEachTopLevel("none", (_, name) => {
|
|
356
|
-
this.emitLine("// val ", (0, Source_1.modifySource)(Strings_1.camelCase, name), " = ", name, ".fromJson(jsonString)");
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
emitHeader() {
|
|
360
|
-
super.emitHeader();
|
|
361
|
-
this.emitLine("import com.beust.klaxon.*");
|
|
362
|
-
const hasUnions = (0, collection_utils_1.iterableSome)(this.typeGraph.allNamedTypes(), t => t instanceof Type_1.UnionType && (0, TypeUtils_1.nullableFromUnion)(t) === null);
|
|
363
|
-
const hasEmptyObjects = (0, collection_utils_1.iterableSome)(this.typeGraph.allNamedTypes(), c => c instanceof Type_1.ClassType && c.getProperties().size === 0);
|
|
364
|
-
if (hasUnions || this.haveEnums || hasEmptyObjects) {
|
|
365
|
-
this.emitGenericConverter();
|
|
366
|
-
}
|
|
367
|
-
let converters = [];
|
|
368
|
-
if (hasEmptyObjects) {
|
|
369
|
-
converters.push([[".convert(JsonObject::class,"], [" { it.obj!! },"], [" { it.toJsonString() })"]]);
|
|
370
|
-
}
|
|
371
|
-
this.forEachEnum("none", (_, name) => {
|
|
372
|
-
converters.push([
|
|
373
|
-
[".convert(", name, "::class,"],
|
|
374
|
-
[" { ", name, ".fromValue(it.string!!) },"],
|
|
375
|
-
[' { "\\"${it.value}\\"" })']
|
|
376
|
-
]);
|
|
377
|
-
});
|
|
378
|
-
this.forEachUnion("none", (_, name) => {
|
|
379
|
-
converters.push([
|
|
380
|
-
[".convert(", name, "::class,"],
|
|
381
|
-
[" { ", name, ".fromJson(it) },"],
|
|
382
|
-
[" { it.toJson() }, true)"]
|
|
383
|
-
]);
|
|
384
|
-
});
|
|
385
|
-
this.ensureBlankLine();
|
|
386
|
-
this.emitLine("private val klaxon = Klaxon()");
|
|
387
|
-
if (converters.length > 0) {
|
|
388
|
-
this.indent(() => this.emitTable(converters));
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
emitTopLevelArray(t, name) {
|
|
392
|
-
const elementType = this.kotlinType(t.items);
|
|
393
|
-
this.emitBlock(["class ", name, "(elements: Collection<", elementType, ">) : ArrayList<", elementType, ">(elements)"], () => {
|
|
394
|
-
this.emitLine("public fun toJson() = klaxon.toJsonString(this)");
|
|
395
|
-
this.ensureBlankLine();
|
|
396
|
-
this.emitBlock("companion object", () => {
|
|
397
|
-
this.emitLine("public fun fromJson(json: String) = ", name, "(klaxon.parseArray<", elementType, ">(json)!!)");
|
|
398
|
-
});
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
emitTopLevelMap(t, name) {
|
|
402
|
-
const elementType = this.kotlinType(t.values);
|
|
403
|
-
this.emitBlock([
|
|
404
|
-
"class ",
|
|
405
|
-
name,
|
|
406
|
-
"(elements: Map<String, ",
|
|
407
|
-
elementType,
|
|
408
|
-
">) : HashMap<String, ",
|
|
409
|
-
elementType,
|
|
410
|
-
">(elements)"
|
|
411
|
-
], () => {
|
|
412
|
-
this.emitLine("public fun toJson() = klaxon.toJsonString(this)");
|
|
413
|
-
this.ensureBlankLine();
|
|
414
|
-
this.emitBlock("companion object", () => {
|
|
415
|
-
this.emitBlock(["public fun fromJson(json: String) = ", name], () => {
|
|
416
|
-
this.emitLine("klaxon.parseJsonObject(java.io.StringReader(json)) as Map<String, ", elementType, ">");
|
|
417
|
-
}, "paren");
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
klaxonRenameAttribute(propName, jsonName, ignore = false) {
|
|
422
|
-
const escapedName = stringEscape(jsonName);
|
|
423
|
-
const namesDiffer = this.sourcelikeToString(propName) !== escapedName;
|
|
424
|
-
const properties = [];
|
|
425
|
-
if (namesDiffer) {
|
|
426
|
-
properties.push(['name = "', escapedName, '"']);
|
|
427
|
-
}
|
|
428
|
-
if (ignore) {
|
|
429
|
-
properties.push("ignored = true");
|
|
430
|
-
}
|
|
431
|
-
return properties.length === 0 ? undefined : ["@Json(", (0, collection_utils_1.arrayIntercalate)(", ", properties), ")"];
|
|
432
|
-
}
|
|
433
|
-
emitEmptyClassDefinition(c, className) {
|
|
434
|
-
this.emitDescription(this.descriptionForType(c));
|
|
435
|
-
this.emitLine("typealias ", className, " = JsonObject");
|
|
436
|
-
}
|
|
437
|
-
emitClassDefinitionMethods(c, className) {
|
|
438
|
-
const isTopLevel = (0, collection_utils_1.iterableSome)(this.topLevels, ([_, top]) => top === c);
|
|
439
|
-
if (isTopLevel) {
|
|
440
|
-
this.emitBlock(")", () => {
|
|
441
|
-
this.emitLine("public fun toJson() = klaxon.toJsonString(this)");
|
|
442
|
-
this.ensureBlankLine();
|
|
443
|
-
this.emitBlock("companion object", () => {
|
|
444
|
-
this.emitLine("public fun fromJson(json: String) = klaxon.parse<", className, ">(json)");
|
|
445
|
-
});
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
else {
|
|
449
|
-
this.emitLine(")");
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
renameAttribute(name, jsonName, _required, meta) {
|
|
453
|
-
const rename = this.klaxonRenameAttribute(name, jsonName);
|
|
454
|
-
if (rename !== undefined) {
|
|
455
|
-
meta.push(() => this.emitLine(rename));
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
emitEnumDefinition(e, enumName) {
|
|
459
|
-
this.emitDescription(this.descriptionForType(e));
|
|
460
|
-
this.emitBlock(["enum class ", enumName, "(val value: String)"], () => {
|
|
461
|
-
let count = e.cases.size;
|
|
462
|
-
this.forEachEnumCase(e, "none", (name, json) => {
|
|
463
|
-
this.emitLine(name, `("${stringEscape(json)}")`, --count === 0 ? ";" : ",");
|
|
464
|
-
});
|
|
465
|
-
this.ensureBlankLine();
|
|
466
|
-
this.emitBlock("companion object", () => {
|
|
467
|
-
this.emitBlock(["public fun fromValue(value: String): ", enumName, " = when (value)"], () => {
|
|
468
|
-
let table = [];
|
|
469
|
-
this.forEachEnumCase(e, "none", (name, json) => {
|
|
470
|
-
table.push([[`"${stringEscape(json)}"`], [" -> ", name]]);
|
|
471
|
-
});
|
|
472
|
-
table.push([["else"], [" -> throw IllegalArgumentException()"]]);
|
|
473
|
-
this.emitTable(table);
|
|
474
|
-
});
|
|
475
|
-
});
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
emitGenericConverter() {
|
|
479
|
-
this.ensureBlankLine();
|
|
480
|
-
this.emitLine("private fun <T> Klaxon.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonValue) -> T, toJson: (T) -> String, isUnion: Boolean = false) =");
|
|
481
|
-
this.indent(() => {
|
|
482
|
-
this.emitLine("this.converter(object: Converter {");
|
|
483
|
-
this.indent(() => {
|
|
484
|
-
this.emitLine(`@Suppress("UNCHECKED_CAST")`);
|
|
485
|
-
this.emitTable([
|
|
486
|
-
["override fun toJson(value: Any)", " = toJson(value as T)"],
|
|
487
|
-
["override fun fromJson(jv: JsonValue)", " = fromJson(jv) as Any"],
|
|
488
|
-
[
|
|
489
|
-
"override fun canConvert(cls: Class<*>)",
|
|
490
|
-
" = cls == k.java || (isUnion && cls.superclass == k.java)"
|
|
491
|
-
]
|
|
492
|
-
]);
|
|
493
|
-
});
|
|
494
|
-
this.emitLine("})");
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
emitUnionDefinitionMethods(u, nonNulls, maybeNull, unionName) {
|
|
498
|
-
this.ensureBlankLine();
|
|
499
|
-
this.emitLine("public fun toJson(): String = klaxon.toJsonString(when (this) {");
|
|
500
|
-
this.indent(() => {
|
|
501
|
-
let toJsonTable = [];
|
|
502
|
-
this.forEachUnionMember(u, nonNulls, "none", null, name => {
|
|
503
|
-
toJsonTable.push([["is ", name], [" -> this.value"]]);
|
|
504
|
-
});
|
|
505
|
-
if (maybeNull !== null) {
|
|
506
|
-
const name = this.nameForUnionMember(u, maybeNull);
|
|
507
|
-
toJsonTable.push([["is ", name], [' -> "null"']]);
|
|
508
|
-
}
|
|
509
|
-
this.emitTable(toJsonTable);
|
|
510
|
-
});
|
|
511
|
-
this.emitLine("})");
|
|
512
|
-
this.ensureBlankLine();
|
|
513
|
-
this.emitBlock("companion object", () => {
|
|
514
|
-
this.emitLine("public fun fromJson(jv: JsonValue): ", unionName, " = when (jv.inside) {");
|
|
515
|
-
this.indent(() => {
|
|
516
|
-
let table = [];
|
|
517
|
-
this.forEachUnionMember(u, nonNulls, "none", null, (name, t) => {
|
|
518
|
-
table.push([
|
|
519
|
-
[this.unionMemberJsonValueGuard(t, "jv.inside")],
|
|
520
|
-
[" -> ", name, "(", this.unionMemberFromJsonValue(t, "jv"), "!!)"]
|
|
521
|
-
]);
|
|
522
|
-
});
|
|
523
|
-
if (maybeNull !== null) {
|
|
524
|
-
const name = this.nameForUnionMember(u, maybeNull);
|
|
525
|
-
table.push([[this.unionMemberJsonValueGuard(maybeNull, "jv.inside")], [" -> ", name, "()"]]);
|
|
526
|
-
}
|
|
527
|
-
table.push([["else"], [" -> throw IllegalArgumentException()"]]);
|
|
528
|
-
this.emitTable(table);
|
|
529
|
-
});
|
|
530
|
-
this.emitLine("}");
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
exports.KotlinKlaxonRenderer = KotlinKlaxonRenderer;
|
|
535
|
-
class KotlinJacksonRenderer extends KotlinRenderer {
|
|
536
|
-
constructor(targetLanguage, renderContext, _kotlinOptions) {
|
|
537
|
-
super(targetLanguage, renderContext, _kotlinOptions);
|
|
538
|
-
}
|
|
539
|
-
unionMemberJsonValueGuard(t, _e) {
|
|
540
|
-
return (0, TypeUtils_1.matchType)(t, _anyType => "is Any", _nullType => "null", _boolType => "is BooleanNode", _integerType => "is IntNode, is LongNode", _doubleType => "is DoubleNode", _stringType => "is TextNode", _arrayType => "is ArrayNode",
|
|
541
|
-
// These could be stricter, but for now we don't allow maps
|
|
542
|
-
// and objects in the same union
|
|
543
|
-
_classType => "is ObjectNode", _mapType => "is ObjectNode",
|
|
544
|
-
// This could be stricter, but for now we don't allow strings
|
|
545
|
-
// and enums in the same union
|
|
546
|
-
_enumType => "is TextNode", _unionType => (0, Support_1.mustNotHappen)());
|
|
547
|
-
}
|
|
548
|
-
emitUsageHeader() {
|
|
549
|
-
this.emitLine("// To parse the JSON, install jackson-module-kotlin and do:");
|
|
550
|
-
this.emitLine("//");
|
|
551
|
-
this.forEachTopLevel("none", (_, name) => {
|
|
552
|
-
this.emitLine("// val ", (0, Source_1.modifySource)(Strings_1.camelCase, name), " = ", name, ".fromJson(jsonString)");
|
|
553
|
-
});
|
|
554
|
-
}
|
|
555
|
-
emitHeader() {
|
|
556
|
-
super.emitHeader();
|
|
557
|
-
this.emitMultiline(`import com.fasterxml.jackson.annotation.*
|
|
558
|
-
import com.fasterxml.jackson.core.*
|
|
559
|
-
import com.fasterxml.jackson.databind.*
|
|
560
|
-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
|
|
561
|
-
import com.fasterxml.jackson.databind.module.SimpleModule
|
|
562
|
-
import com.fasterxml.jackson.databind.node.*
|
|
563
|
-
import com.fasterxml.jackson.databind.ser.std.StdSerializer
|
|
564
|
-
import com.fasterxml.jackson.module.kotlin.*`);
|
|
565
|
-
const hasUnions = (0, collection_utils_1.iterableSome)(this.typeGraph.allNamedTypes(), t => t instanceof Type_1.UnionType && (0, TypeUtils_1.nullableFromUnion)(t) === null);
|
|
566
|
-
const hasEmptyObjects = (0, collection_utils_1.iterableSome)(this.typeGraph.allNamedTypes(), c => c instanceof Type_1.ClassType && c.getProperties().size === 0);
|
|
567
|
-
if (hasUnions || this.haveEnums || hasEmptyObjects) {
|
|
568
|
-
this.emitGenericConverter();
|
|
569
|
-
}
|
|
570
|
-
let converters = [];
|
|
571
|
-
// if (hasEmptyObjects) {
|
|
572
|
-
// converters.push([["convert(JsonNode::class,"], [" { it },"], [" { writeValueAsString(it) })"]]);
|
|
573
|
-
// }
|
|
574
|
-
this.forEachEnum("none", (_, name) => {
|
|
575
|
-
converters.push([
|
|
576
|
-
["convert(", name, "::class,"],
|
|
577
|
-
[" { ", name, ".fromValue(it.asText()) },"],
|
|
578
|
-
[' { "\\"${it.value}\\"" })']
|
|
579
|
-
]);
|
|
580
|
-
});
|
|
581
|
-
this.forEachUnion("none", (_, name) => {
|
|
582
|
-
converters.push([
|
|
583
|
-
["convert(", name, "::class,"],
|
|
584
|
-
[" { ", name, ".fromJson(it) },"],
|
|
585
|
-
[" { it.toJson() }, true)"]
|
|
586
|
-
]);
|
|
587
|
-
});
|
|
588
|
-
this.ensureBlankLine();
|
|
589
|
-
this.emitLine("val mapper = jacksonObjectMapper().apply {");
|
|
590
|
-
this.indent(() => {
|
|
591
|
-
this.emitLine("propertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE");
|
|
592
|
-
this.emitLine("setSerializationInclusion(JsonInclude.Include.NON_NULL)");
|
|
593
|
-
});
|
|
594
|
-
if (converters.length > 0) {
|
|
595
|
-
this.indent(() => this.emitTable(converters));
|
|
596
|
-
}
|
|
597
|
-
this.emitLine("}");
|
|
598
|
-
}
|
|
599
|
-
emitTopLevelArray(t, name) {
|
|
600
|
-
const elementType = this.kotlinType(t.items);
|
|
601
|
-
this.emitBlock(["class ", name, "(elements: Collection<", elementType, ">) : ArrayList<", elementType, ">(elements)"], () => {
|
|
602
|
-
this.emitLine("fun toJson() = mapper.writeValueAsString(this)");
|
|
603
|
-
this.ensureBlankLine();
|
|
604
|
-
this.emitBlock("companion object", () => {
|
|
605
|
-
this.emitLine("fun fromJson(json: String) = mapper.readValue<", name, ">(json)");
|
|
606
|
-
});
|
|
607
|
-
});
|
|
608
|
-
}
|
|
609
|
-
emitTopLevelMap(t, name) {
|
|
610
|
-
const elementType = this.kotlinType(t.values);
|
|
611
|
-
this.emitBlock([
|
|
612
|
-
"class ",
|
|
613
|
-
name,
|
|
614
|
-
"(elements: Map<String, ",
|
|
615
|
-
elementType,
|
|
616
|
-
">) : HashMap<String, ",
|
|
617
|
-
elementType,
|
|
618
|
-
">(elements)"
|
|
619
|
-
], () => {
|
|
620
|
-
this.emitLine("fun toJson() = mapper.writeValueAsString(this)");
|
|
621
|
-
this.ensureBlankLine();
|
|
622
|
-
this.emitBlock("companion object", () => {
|
|
623
|
-
this.emitLine("fun fromJson(json: String) = mapper.readValue<", name, ">(json)");
|
|
624
|
-
});
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
jacksonRenameAttribute(propName, jsonName, required, ignore = false) {
|
|
628
|
-
const escapedName = stringEscape(jsonName);
|
|
629
|
-
const namesDiffer = this.sourcelikeToString(propName) !== escapedName;
|
|
630
|
-
const properties = [];
|
|
631
|
-
const isPrefixBool = jsonName.startsWith("is"); // https://github.com/FasterXML/jackson-module-kotlin/issues/80
|
|
632
|
-
const propertyOpts = [];
|
|
633
|
-
if (namesDiffer || isPrefixBool) {
|
|
634
|
-
propertyOpts.push('"' + escapedName + '"');
|
|
635
|
-
}
|
|
636
|
-
if (required) {
|
|
637
|
-
propertyOpts.push("required=true");
|
|
638
|
-
}
|
|
639
|
-
if (propertyOpts.length > 0) {
|
|
640
|
-
properties.push(["@get:JsonProperty(", (0, collection_utils_1.arrayIntercalate)(", ", propertyOpts), ")"]);
|
|
641
|
-
properties.push(["@field:JsonProperty(", (0, collection_utils_1.arrayIntercalate)(", ", propertyOpts), ")"]);
|
|
642
|
-
}
|
|
643
|
-
if (ignore) {
|
|
644
|
-
properties.push("@get:JsonIgnore");
|
|
645
|
-
properties.push("@field:JsonIgnore");
|
|
646
|
-
}
|
|
647
|
-
return properties.length === 0 ? undefined : properties;
|
|
648
|
-
}
|
|
649
|
-
emitEmptyClassDefinition(c, className) {
|
|
650
|
-
this.emitDescription(this.descriptionForType(c));
|
|
651
|
-
this.emitLine("typealias ", className, " = JsonNode");
|
|
652
|
-
}
|
|
653
|
-
emitClassDefinitionMethods(c, className) {
|
|
654
|
-
const isTopLevel = (0, collection_utils_1.iterableSome)(this.topLevels, ([_, top]) => top === c);
|
|
655
|
-
if (isTopLevel) {
|
|
656
|
-
this.emitBlock(")", () => {
|
|
657
|
-
this.emitLine("fun toJson() = mapper.writeValueAsString(this)");
|
|
658
|
-
this.ensureBlankLine();
|
|
659
|
-
this.emitBlock("companion object", () => {
|
|
660
|
-
this.emitLine("fun fromJson(json: String) = mapper.readValue<", className, ">(json)");
|
|
661
|
-
});
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
else {
|
|
665
|
-
this.emitLine(")");
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
renameAttribute(name, jsonName, required, meta) {
|
|
669
|
-
const rename = this.jacksonRenameAttribute(name, jsonName, required);
|
|
670
|
-
if (rename !== undefined) {
|
|
671
|
-
meta.push(() => this.emitLine(rename));
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
emitEnumDefinition(e, enumName) {
|
|
675
|
-
this.emitDescription(this.descriptionForType(e));
|
|
676
|
-
this.emitBlock(["enum class ", enumName, "(val value: String)"], () => {
|
|
677
|
-
let count = e.cases.size;
|
|
678
|
-
this.forEachEnumCase(e, "none", (name, json) => {
|
|
679
|
-
this.emitLine(name, `("${stringEscape(json)}")`, --count === 0 ? ";" : ",");
|
|
680
|
-
});
|
|
681
|
-
this.ensureBlankLine();
|
|
682
|
-
this.emitBlock("companion object", () => {
|
|
683
|
-
this.emitBlock(["fun fromValue(value: String): ", enumName, " = when (value)"], () => {
|
|
684
|
-
let table = [];
|
|
685
|
-
this.forEachEnumCase(e, "none", (name, json) => {
|
|
686
|
-
table.push([[`"${stringEscape(json)}"`], [" -> ", name]]);
|
|
687
|
-
});
|
|
688
|
-
table.push([["else"], [" -> throw IllegalArgumentException()"]]);
|
|
689
|
-
this.emitTable(table);
|
|
690
|
-
});
|
|
691
|
-
});
|
|
692
|
-
});
|
|
693
|
-
}
|
|
694
|
-
emitGenericConverter() {
|
|
695
|
-
this.ensureBlankLine();
|
|
696
|
-
this.emitMultiline(`
|
|
697
|
-
@Suppress("UNCHECKED_CAST")
|
|
698
|
-
private fun <T> ObjectMapper.convert(k: kotlin.reflect.KClass<*>, fromJson: (JsonNode) -> T, toJson: (T) -> String, isUnion: Boolean = false) = registerModule(SimpleModule().apply {
|
|
699
|
-
addSerializer(k.java as Class<T>, object : StdSerializer<T>(k.java as Class<T>) {
|
|
700
|
-
override fun serialize(value: T, gen: JsonGenerator, provider: SerializerProvider) = gen.writeRawValue(toJson(value))
|
|
701
|
-
})
|
|
702
|
-
addDeserializer(k.java as Class<T>, object : StdDeserializer<T>(k.java as Class<T>) {
|
|
703
|
-
override fun deserialize(p: JsonParser, ctxt: DeserializationContext) = fromJson(p.readValueAsTree())
|
|
704
|
-
})
|
|
705
|
-
})`);
|
|
706
|
-
}
|
|
707
|
-
emitUnionDefinitionMethods(u, nonNulls, maybeNull, unionName) {
|
|
708
|
-
this.ensureBlankLine();
|
|
709
|
-
this.emitLine("fun toJson(): String = mapper.writeValueAsString(when (this) {");
|
|
710
|
-
this.indent(() => {
|
|
711
|
-
let toJsonTable = [];
|
|
712
|
-
this.forEachUnionMember(u, nonNulls, "none", null, name => {
|
|
713
|
-
toJsonTable.push([["is ", name], [" -> this.value"]]);
|
|
714
|
-
});
|
|
715
|
-
if (maybeNull !== null) {
|
|
716
|
-
const name = this.nameForUnionMember(u, maybeNull);
|
|
717
|
-
toJsonTable.push([["is ", name], [' -> "null"']]);
|
|
718
|
-
}
|
|
719
|
-
this.emitTable(toJsonTable);
|
|
720
|
-
});
|
|
721
|
-
this.emitLine("})");
|
|
722
|
-
this.ensureBlankLine();
|
|
723
|
-
this.emitBlock("companion object", () => {
|
|
724
|
-
this.emitLine("fun fromJson(jn: JsonNode): ", unionName, " = when (jn) {");
|
|
725
|
-
this.indent(() => {
|
|
726
|
-
let table = [];
|
|
727
|
-
this.forEachUnionMember(u, nonNulls, "none", null, (name, t) => {
|
|
728
|
-
table.push([[this.unionMemberJsonValueGuard(t, "jn")], [" -> ", name, "(mapper.treeToValue(jn))"]]);
|
|
729
|
-
});
|
|
730
|
-
if (maybeNull !== null) {
|
|
731
|
-
const name = this.nameForUnionMember(u, maybeNull);
|
|
732
|
-
table.push([[this.unionMemberJsonValueGuard(maybeNull, "jn")], [" -> ", name, "()"]]);
|
|
733
|
-
}
|
|
734
|
-
table.push([["else"], [" -> throw IllegalArgumentException()"]]);
|
|
735
|
-
this.emitTable(table);
|
|
736
|
-
});
|
|
737
|
-
this.emitLine("}");
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
exports.KotlinJacksonRenderer = KotlinJacksonRenderer;
|
|
742
|
-
/**
|
|
743
|
-
* Currently supports simple classes, enums, and TS string unions (which are also enums).
|
|
744
|
-
* TODO: Union, Any, Top Level Array, Top Level Map
|
|
745
|
-
*/
|
|
746
|
-
class KotlinXRenderer extends KotlinRenderer {
|
|
747
|
-
constructor(targetLanguage, renderContext, _kotlinOptions) {
|
|
748
|
-
super(targetLanguage, renderContext, _kotlinOptions);
|
|
749
|
-
}
|
|
750
|
-
anySourceType(optional) {
|
|
751
|
-
return ["JsonElement", optional];
|
|
752
|
-
}
|
|
753
|
-
arrayType(arrayType, withIssues = false, noOptional = false) {
|
|
754
|
-
const valType = this.kotlinType(arrayType.items, withIssues, true);
|
|
755
|
-
const name = this.sourcelikeToString(valType);
|
|
756
|
-
if (name === "JsonObject" || name === "JsonElement") {
|
|
757
|
-
return "JsonArray";
|
|
758
|
-
}
|
|
759
|
-
return super.arrayType(arrayType, withIssues, noOptional);
|
|
760
|
-
}
|
|
761
|
-
mapType(mapType, withIssues = false, noOptional = false) {
|
|
762
|
-
const valType = this.kotlinType(mapType.values, withIssues, true);
|
|
763
|
-
const name = this.sourcelikeToString(valType);
|
|
764
|
-
if (name === "JsonObject" || name === "JsonElement") {
|
|
765
|
-
return "JsonObject";
|
|
766
|
-
}
|
|
767
|
-
return super.mapType(mapType, withIssues, noOptional);
|
|
768
|
-
}
|
|
769
|
-
emitTopLevelMap(t, name) {
|
|
770
|
-
const elementType = this.kotlinType(t.values);
|
|
771
|
-
if (elementType === "JsonObject") {
|
|
772
|
-
this.emitLine(["typealias ", name, " = JsonObject"]);
|
|
773
|
-
}
|
|
774
|
-
else {
|
|
775
|
-
super.emitTopLevelMap(t, name);
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
emitTopLevelArray(t, name) {
|
|
779
|
-
const elementType = this.kotlinType(t.items);
|
|
780
|
-
this.emitLine(["typealias ", name, " = JsonArray<", elementType, ">"]);
|
|
781
|
-
}
|
|
782
|
-
emitUsageHeader() {
|
|
783
|
-
this.emitLine("// To parse the JSON, install kotlin's serialization plugin and do:");
|
|
784
|
-
this.emitLine("//");
|
|
785
|
-
const table = [];
|
|
786
|
-
table.push(["// val ", "json", " = Json { allowStructuredMapKeys = true }"]);
|
|
787
|
-
this.forEachTopLevel("none", (_, name) => {
|
|
788
|
-
table.push([
|
|
789
|
-
"// val ",
|
|
790
|
-
(0, Source_1.modifySource)(Strings_1.camelCase, name),
|
|
791
|
-
` = json.parse(${this.sourcelikeToString(name)}.serializer(), jsonString)`
|
|
792
|
-
]);
|
|
793
|
-
});
|
|
794
|
-
this.emitTable(table);
|
|
795
|
-
}
|
|
796
|
-
emitHeader() {
|
|
797
|
-
super.emitHeader();
|
|
798
|
-
this.emitLine("import kotlinx.serialization.*");
|
|
799
|
-
this.emitLine("import kotlinx.serialization.json.*");
|
|
800
|
-
this.emitLine("import kotlinx.serialization.descriptors.*");
|
|
801
|
-
this.emitLine("import kotlinx.serialization.encoding.*");
|
|
802
|
-
}
|
|
803
|
-
emitClassAnnotations(_c, _className) {
|
|
804
|
-
this.emitLine("@Serializable");
|
|
805
|
-
}
|
|
806
|
-
renameAttribute(name, jsonName, _required, meta) {
|
|
807
|
-
const rename = this._rename(name, jsonName);
|
|
808
|
-
if (rename !== undefined) {
|
|
809
|
-
meta.push(() => this.emitLine(rename));
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
_rename(propName, jsonName) {
|
|
813
|
-
const escapedName = stringEscape(jsonName);
|
|
814
|
-
const namesDiffer = this.sourcelikeToString(propName) !== escapedName;
|
|
815
|
-
if (namesDiffer) {
|
|
816
|
-
return ['@SerialName("', escapedName, '")'];
|
|
817
|
-
}
|
|
818
|
-
return undefined;
|
|
819
|
-
}
|
|
820
|
-
emitEnumDefinition(e, enumName) {
|
|
821
|
-
this.emitDescription(this.descriptionForType(e));
|
|
822
|
-
this.emitLine(["@Serializable"]);
|
|
823
|
-
this.emitBlock(["enum class ", enumName, "(val value: String)"], () => {
|
|
824
|
-
let count = e.cases.size;
|
|
825
|
-
this.forEachEnumCase(e, "none", (name, json) => {
|
|
826
|
-
const jsonEnum = stringEscape(json);
|
|
827
|
-
this.emitLine(`@SerialName("${jsonEnum}") `, name, `("${jsonEnum}")`, --count === 0 ? ";" : ",");
|
|
828
|
-
});
|
|
829
|
-
});
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
exports.KotlinXRenderer = KotlinXRenderer;
|