Skip to content

Commit f8f2fe7

Browse files
authored
Minor nits to ObjectSerializer (#490)
* Minor nits to ObjectSerializer Make some cleanups to serialize/deserialize. * Fix indent
1 parent f37e776 commit f8f2fe7

File tree

3 files changed

+157
-121
lines changed

3 files changed

+157
-121
lines changed

.generator/templates/model/ObjectSerializer.mustache

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import { {{classname}}{{#hasEnums}}{{#vars}}{{#isEnum}}, {{classname}}{{enumName
55
{{/models}}
66
import { logger } from "../../../index";
77

8-
/* tslint:disable:no-unused-variable */
9-
let primitives = [
10-
"string",
11-
"boolean",
12-
"double",
13-
"integer",
14-
"long",
15-
"float",
16-
"number",
17-
"any"
18-
];
8+
const primitives = [
9+
"string",
10+
"boolean",
11+
"double",
12+
"integer",
13+
"long",
14+
"float",
15+
"number",
16+
"any"
17+
];
18+
19+
const ARRAY_PREFIX = "Array<";
20+
const MAP_PREFIX = "{ [key: string]: ";
1921
2022
const supportedMediaTypes: { [mediaType: string]: number } = {
2123
"application/json": Infinity,
@@ -40,7 +42,7 @@ const enumsMap: {[key: string]: any[]} = {
4042
{{/models}}
4143
};
4244

43-
let typeMap: {[index: string]: any} = {
45+
const typeMap: {[index: string]: any} = {
4446
{{#models}}
4547
{{#model}}
4648
{{^isEnum}}
@@ -52,7 +54,7 @@ let typeMap: {[index: string]: any} = {
5254
{{/models}}
5355
}
5456

55-
let oneOfMap: {[index: string]: string[]} = {
57+
const oneOfMap: {[index: string]: string[]} = {
5658
{{#models}}{{#model}}{{#oneOf}}{{#-first}}"{{classname}}": [{{/-first}}"{{{.}}}"{{^-last}}, {{/-last}}{{#-last}}],
5759
{{/-last}}{{/oneOf}}{{/model}}{{/models}}
5860
};
@@ -61,32 +63,30 @@ export class ObjectSerializer {
6163
public static serialize(data: any, type: string, format: string): any {
6264
if (data == undefined) {
6365
return data;
64-
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
66+
} else if (primitives.includes(type.toLowerCase())) {
6567
return data;
66-
} else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6
67-
let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
68-
subType = subType.substring(0, subType.length - 1); // Type> => Type
69-
let transformedData: any[] = [];
70-
for (let index in data) {
71-
let date = data[index];
72-
transformedData.push(ObjectSerializer.serialize(date, subType, format));
68+
} else if (type.startsWith(ARRAY_PREFIX)) {
69+
// Array<Type> => Type
70+
const subType: string = type.substring(ARRAY_PREFIX.length, type.length - 1);
71+
const transformedData: any[] = [];
72+
for (const element of data) {
73+
transformedData.push(ObjectSerializer.serialize(element, subType, format));
7374
}
7475
return transformedData;
75-
} else if (type.lastIndexOf("{ [key: string]: ", 0) === 0) { // string.startsWith pre es6
76-
let subType: string = type.replace("{ [key: string]: ", ""); // { [key: string]: Type; } => Type; }
77-
subType = subType.substring(0, subType.length - 3); // Type; } => Type
78-
let transformedData: { [key: string]: any } = {};
79-
for (let k in data) {
80-
let date = data[k]
81-
transformedData[k] = ObjectSerializer.serialize(date, subType, format);
76+
} else if (type.startsWith(MAP_PREFIX)) {
77+
// { [key: string]: Type; } => Type
78+
const subType: string = type.substring(MAP_PREFIX.length, type.length - 3);
79+
const transformedData: { [key: string]: any } = {};
80+
for (const key in data) {
81+
transformedData[key] = ObjectSerializer.serialize(data[key], subType, format);
8282
}
8383
return transformedData;
8484
} else if (type === "Date") {
8585
if ("string" == typeof data) {
8686
return data;
8787
}
8888
if (format == "date") {
89-
let month = data.getMonth()+1
89+
let month = data.getMonth() + 1
9090
month = month < 10 ? "0" + month.toString() : month.toString()
9191
let day = data.getDate();
9292
day = day < 10 ? "0" + day.toString() : day.toString();
@@ -103,8 +103,8 @@ export class ObjectSerializer {
103103
throw new TypeError(`unknown enum value '${data}'`)
104104
}
105105
if (oneOfMap[type]) {
106-
let oneOfs: any[] = [];
107-
for(let oneOf of oneOfMap[type]) {
106+
const oneOfs: any[] = [];
107+
for (const oneOf of oneOfMap[type]) {
108108
try {
109109
oneOfs.push(ObjectSerializer.serialize(data, oneOf, format));
110110
} catch (e) {
@@ -120,12 +120,16 @@ export class ObjectSerializer {
120120
return oneOfs[0];
121121
}
122122

123+
if (!typeMap[type]) { // dont know the type
124+
throw new TypeError(`unknown type '${type}'`);
125+
}
126+
123127
// get the map for the correct type.
124-
let attributesMap = typeMap[type].getAttributeTypeMap();
125-
let instance: {[index: string]: any} = {};
128+
const attributesMap = typeMap[type].getAttributeTypeMap();
129+
const instance: {[index: string]: any} = {};
126130

127-
for (let attributeName in attributesMap) {
128-
let attributeObj = attributesMap[attributeName];
131+
for (const attributeName in attributesMap) {
132+
const attributeObj = attributesMap[attributeName];
129133
instance[attributeObj.baseName] = ObjectSerializer.serialize(data[attributeName], attributeObj.type, attributeObj.format);
130134
131135
// check for required properties
@@ -144,24 +148,22 @@ export class ObjectSerializer {
144148
public static deserialize(data: any, type: string, format: string = ""): any {
145149
if (data == undefined) {
146150
return data;
147-
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
151+
} else if (primitives.includes(type.toLowerCase())) {
148152
return data;
149-
} else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6
150-
let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
151-
subType = subType.substring(0, subType.length - 1); // Type> => Type
152-
let transformedData: any[] = [];
153-
for (let index in data) {
154-
let date = data[index];
155-
transformedData.push(ObjectSerializer.deserialize(date, subType, format));
153+
} else if (type.startsWith(ARRAY_PREFIX)) {
154+
// Array<Type> => Type
155+
const subType: string = type.substring(ARRAY_PREFIX.length, type.length - 1);
156+
const transformedData: any[] = [];
157+
for (const element of data) {
158+
transformedData.push(ObjectSerializer.deserialize(element, subType, format));
156159
}
157160
return transformedData;
158-
} else if (type.lastIndexOf("{ [key: string]: ", 0) === 0) { // string.startsWith pre es6
159-
let subType: string = type.replace("{ [key: string]: ", ""); // { [key: string]: Type; } => Type; }
160-
subType = subType.substring(0, subType.length - 3); // Type; } => Type
161-
let transformedData: { [key: string]: any } = {};
162-
for (let k in data) {
163-
let date = data[k]
164-
transformedData[k] = ObjectSerializer.deserialize(date, subType, format);
161+
} else if (type.startsWith(MAP_PREFIX)) {
162+
// { [key: string]: Type; } => Type
163+
const subType: string = type.substring(MAP_PREFIX.length, type.length - 3);
164+
const transformedData: { [key: string]: any } = {};
165+
for (const key in data) {
166+
transformedData[key] = ObjectSerializer.deserialize(data[key], subType, format);
165167
}
166168
return transformedData;
167169
} else if (type === "Date") {
@@ -172,10 +174,10 @@ export class ObjectSerializer {
172174
}
173175

174176
if (oneOfMap[type]) {
175-
let oneOfs: any[] = [];
176-
for(let oneOf of oneOfMap[type]) {
177+
const oneOfs: any[] = [];
178+
for (const oneOf of oneOfMap[type]) {
177179
try {
178-
let d = ObjectSerializer.deserialize(data, oneOf, format);
180+
const d = ObjectSerializer.deserialize(data, oneOf, format);
179181
if (d?.unparsedObject === undefined) {
180182
oneOfs.push(d);
181183
}
@@ -194,11 +196,11 @@ export class ObjectSerializer {
194196
throw new TypeError(`unknown type '${type}'`);
195197
}
196198

197-
let instance = new typeMap[type]();
198-
let attributesMap = typeMap[type].getAttributeTypeMap();
199+
const instance = new typeMap[type]();
200+
const attributesMap = typeMap[type].getAttributeTypeMap();
199201

200-
for (let attributeName in attributesMap) {
201-
let attributeObj = attributesMap[attributeName];
202+
for (const attributeName in attributesMap) {
203+
const attributeObj = attributesMap[attributeName];
202204
instance[attributeName] = ObjectSerializer.deserialize(data[attributeObj.baseName], attributeObj.type, attributeObj.format);
203205
204206
// check for required properties

packages/datadog-api-client-v1/models/ObjectSerializer.ts

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ import { WidgetStyle } from "./WidgetStyle";
489489
import { WidgetTime } from "./WidgetTime";
490490
import { logger } from "../../../index";
491491

492-
/* tslint:disable:no-unused-variable */
493492
const primitives = [
494493
"string",
495494
"boolean",
@@ -501,6 +500,9 @@ const primitives = [
501500
"any",
502501
];
503502

503+
const ARRAY_PREFIX = "Array<";
504+
const MAP_PREFIX = "{ [key: string]: ";
505+
504506
const supportedMediaTypes: { [mediaType: string]: number } = {
505507
"application/json": Infinity,
506508
"text/json": 100,
@@ -1701,26 +1703,34 @@ export class ObjectSerializer {
17011703
public static serialize(data: any, type: string, format: string): any {
17021704
if (data == undefined) {
17031705
return data;
1704-
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
1706+
} else if (primitives.includes(type.toLowerCase())) {
17051707
return data;
1706-
} else if (type.lastIndexOf("Array<", 0) === 0) {
1707-
// string.startsWith pre es6
1708-
let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
1709-
subType = subType.substring(0, subType.length - 1); // Type> => Type
1708+
} else if (type.startsWith(ARRAY_PREFIX)) {
1709+
// Array<Type> => Type
1710+
const subType: string = type.substring(
1711+
ARRAY_PREFIX.length,
1712+
type.length - 1
1713+
);
17101714
const transformedData: any[] = [];
1711-
for (const index in data) {
1712-
const date = data[index];
1713-
transformedData.push(ObjectSerializer.serialize(date, subType, format));
1715+
for (const element of data) {
1716+
transformedData.push(
1717+
ObjectSerializer.serialize(element, subType, format)
1718+
);
17141719
}
17151720
return transformedData;
1716-
} else if (type.lastIndexOf("{ [key: string]: ", 0) === 0) {
1717-
// string.startsWith pre es6
1718-
let subType: string = type.replace("{ [key: string]: ", ""); // { [key: string]: Type; } => Type; }
1719-
subType = subType.substring(0, subType.length - 3); // Type; } => Type
1721+
} else if (type.startsWith(MAP_PREFIX)) {
1722+
// { [key: string]: Type; } => Type
1723+
const subType: string = type.substring(
1724+
MAP_PREFIX.length,
1725+
type.length - 3
1726+
);
17201727
const transformedData: { [key: string]: any } = {};
1721-
for (const k in data) {
1722-
const date = data[k];
1723-
transformedData[k] = ObjectSerializer.serialize(date, subType, format);
1728+
for (const key in data) {
1729+
transformedData[key] = ObjectSerializer.serialize(
1730+
data[key],
1731+
subType,
1732+
format
1733+
);
17241734
}
17251735
return transformedData;
17261736
} else if (type === "Date") {
@@ -1766,6 +1776,11 @@ export class ObjectSerializer {
17661776
return oneOfs[0];
17671777
}
17681778

1779+
if (!typeMap[type]) {
1780+
// dont know the type
1781+
throw new TypeError(`unknown type '${type}'`);
1782+
}
1783+
17691784
// get the map for the correct type.
17701785
const attributesMap = typeMap[type].getAttributeTypeMap();
17711786
const instance: { [index: string]: any } = {};
@@ -1802,29 +1817,31 @@ export class ObjectSerializer {
18021817
public static deserialize(data: any, type: string, format = ""): any {
18031818
if (data == undefined) {
18041819
return data;
1805-
} else if (primitives.indexOf(type.toLowerCase()) !== -1) {
1820+
} else if (primitives.includes(type.toLowerCase())) {
18061821
return data;
1807-
} else if (type.lastIndexOf("Array<", 0) === 0) {
1808-
// string.startsWith pre es6
1809-
let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
1810-
subType = subType.substring(0, subType.length - 1); // Type> => Type
1822+
} else if (type.startsWith(ARRAY_PREFIX)) {
1823+
// Array<Type> => Type
1824+
const subType: string = type.substring(
1825+
ARRAY_PREFIX.length,
1826+
type.length - 1
1827+
);
18111828
const transformedData: any[] = [];
1812-
for (const index in data) {
1813-
const date = data[index];
1829+
for (const element of data) {
18141830
transformedData.push(
1815-
ObjectSerializer.deserialize(date, subType, format)
1831+
ObjectSerializer.deserialize(element, subType, format)
18161832
);
18171833
}
18181834
return transformedData;
1819-
} else if (type.lastIndexOf("{ [key: string]: ", 0) === 0) {
1820-
// string.startsWith pre es6
1821-
let subType: string = type.replace("{ [key: string]: ", ""); // { [key: string]: Type; } => Type; }
1822-
subType = subType.substring(0, subType.length - 3); // Type; } => Type
1835+
} else if (type.startsWith(MAP_PREFIX)) {
1836+
// { [key: string]: Type; } => Type
1837+
const subType: string = type.substring(
1838+
MAP_PREFIX.length,
1839+
type.length - 3
1840+
);
18231841
const transformedData: { [key: string]: any } = {};
1824-
for (const k in data) {
1825-
const date = data[k];
1826-
transformedData[k] = ObjectSerializer.deserialize(
1827-
date,
1842+
for (const key in data) {
1843+
transformedData[key] = ObjectSerializer.deserialize(
1844+
data[key],
18281845
subType,
18291846
format
18301847
);

0 commit comments

Comments
 (0)