@colyseus/schema 2.0.4 → 2.0.6
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 +0 -4
- package/build/cjs/index.js +48 -48
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +130 -104
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +50 -50
- package/lib/Reflection.js +87 -119
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +195 -257
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +6 -6
- package/lib/annotations.js +64 -92
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +1 -1
- package/lib/changes/ChangeTree.js +63 -70
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/changes/ReferenceTracker.js +24 -27
- package/lib/changes/ReferenceTracker.js.map +1 -1
- package/lib/codegen/api.js +9 -9
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.d.ts +1 -1
- package/lib/codegen/argv.js +11 -11
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +21 -10
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +126 -77
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +121 -62
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +34 -26
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +39 -27
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +48 -32
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +35 -24
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +63 -68
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.d.ts +9 -1
- package/lib/codegen/parser.js +88 -46
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.d.ts +8 -0
- package/lib/codegen/types.js +64 -54
- package/lib/codegen/types.js.map +1 -1
- package/lib/encoding/decode.js +15 -15
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +14 -14
- package/lib/encoding/encode.js.map +1 -1
- package/lib/events/EventEmitter.d.ts +1 -1
- package/lib/events/EventEmitter.js +16 -47
- package/lib/events/EventEmitter.js.map +1 -1
- package/lib/filters/index.js +7 -8
- package/lib/filters/index.js.map +1 -1
- package/lib/index.js +11 -11
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +161 -219
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +1 -1
- package/lib/types/CollectionSchema.js +63 -71
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +9 -9
- package/lib/types/MapSchema.d.ts +16 -16
- package/lib/types/MapSchema.js +68 -78
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.js +62 -71
- package/lib/types/SetSchema.js.map +1 -1
- package/lib/types/index.js +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/typeRegistry.js +1 -1
- package/lib/types/typeRegistry.js.map +1 -1
- package/lib/types/utils.js +9 -10
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +18 -15
- package/src/Reflection.ts +159 -0
- package/src/Schema.ts +1024 -0
- package/src/annotations.ts +400 -0
- package/src/changes/ChangeTree.ts +295 -0
- package/src/changes/ReferenceTracker.ts +81 -0
- package/src/codegen/api.ts +46 -0
- package/src/codegen/argv.ts +40 -0
- package/src/codegen/cli.ts +65 -0
- package/src/codegen/languages/cpp.ts +297 -0
- package/src/codegen/languages/csharp.ts +208 -0
- package/src/codegen/languages/haxe.ts +110 -0
- package/src/codegen/languages/java.ts +115 -0
- package/src/codegen/languages/js.ts +115 -0
- package/src/codegen/languages/lua.ts +125 -0
- package/src/codegen/languages/ts.ts +129 -0
- package/src/codegen/parser.ts +299 -0
- package/src/codegen/types.ts +177 -0
- package/src/encoding/decode.ts +278 -0
- package/src/encoding/encode.ts +283 -0
- package/src/filters/index.ts +23 -0
- package/src/index.ts +59 -0
- package/src/spec.ts +49 -0
- package/src/types/ArraySchema.ts +612 -0
- package/src/types/CollectionSchema.ts +199 -0
- package/src/types/HelperTypes.ts +34 -0
- package/src/types/MapSchema.ts +268 -0
- package/src/types/SetSchema.ts +208 -0
- package/src/types/typeRegistry.ts +19 -0
- package/src/types/utils.ts +62 -0
- package/src/utils.ts +28 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generate = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const typeMaps = {
|
|
6
6
|
"string": "string",
|
|
7
7
|
"number": "number",
|
|
8
8
|
"boolean": "boolean",
|
|
@@ -17,73 +17,89 @@ var typeMaps = {
|
|
|
17
17
|
"float32": "number",
|
|
18
18
|
"float64": "number",
|
|
19
19
|
};
|
|
20
|
-
|
|
20
|
+
const distinct = (value, index, self) => self.indexOf(value) === index;
|
|
21
21
|
function generate(context, options) {
|
|
22
|
-
return context.classes.map(
|
|
22
|
+
return context.classes.map(klass => ({
|
|
23
23
|
name: klass.name + ".js",
|
|
24
24
|
content: generateClass(klass, options.namespace, context.classes)
|
|
25
|
-
})
|
|
25
|
+
}));
|
|
26
26
|
}
|
|
27
27
|
exports.generate = generate;
|
|
28
28
|
function generateClass(klass, namespace, allClasses) {
|
|
29
|
-
|
|
30
|
-
klass.properties.forEach(
|
|
31
|
-
|
|
29
|
+
const allRefs = [];
|
|
30
|
+
klass.properties.forEach(property => {
|
|
31
|
+
let type = property.type;
|
|
32
32
|
// keep all refs list
|
|
33
33
|
if ((type === "ref" || type === "array" || type === "map")) {
|
|
34
34
|
allRefs.push(property);
|
|
35
35
|
}
|
|
36
36
|
});
|
|
37
|
-
return types_1.getCommentHeader
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
return `${(0, types_1.getCommentHeader)()}
|
|
38
|
+
|
|
39
|
+
const schema = require("@colyseus/schema");
|
|
40
|
+
const Schema = schema.Schema;
|
|
41
|
+
const type = schema.type;
|
|
42
|
+
${allRefs.
|
|
43
|
+
filter(ref => ref.childType && typeMaps[ref.childType] === undefined).
|
|
44
|
+
map(ref => ref.childType).
|
|
45
|
+
concat((0, types_1.getInheritanceTree)(klass, allClasses, false).map(klass => klass.name)).
|
|
41
46
|
filter(distinct).
|
|
42
|
-
map(
|
|
43
|
-
join("\n")
|
|
44
|
-
|
|
45
|
-
|
|
47
|
+
map(childType => `const ${childType} = require("./${childType}");`).
|
|
48
|
+
join("\n")}
|
|
49
|
+
|
|
50
|
+
class ${klass.name} extends ${klass.extends} {
|
|
51
|
+
constructor () {
|
|
52
|
+
super();
|
|
53
|
+
${klass.properties.
|
|
54
|
+
filter(prop => prop.childType !== undefined).
|
|
55
|
+
map(prop => " " + generatePropertyInitializer(prop)).join("\n")}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
${klass.properties.map(prop => generatePropertyDeclaration(klass.name, prop)).join("\n")}
|
|
59
|
+
|
|
60
|
+
export default ${klass.name};
|
|
61
|
+
`;
|
|
46
62
|
}
|
|
47
63
|
function generatePropertyDeclaration(className, prop) {
|
|
48
|
-
|
|
64
|
+
let typeArgs;
|
|
49
65
|
if (prop.childType) {
|
|
50
|
-
|
|
66
|
+
const isUpcaseFirst = prop.childType.match(/^[A-Z]/);
|
|
51
67
|
if (isUpcaseFirst) {
|
|
52
|
-
typeArgs +=
|
|
68
|
+
typeArgs += `, ${prop.childType}`;
|
|
53
69
|
}
|
|
54
70
|
else {
|
|
55
|
-
typeArgs +=
|
|
71
|
+
typeArgs += `, "${prop.childType}"`;
|
|
56
72
|
}
|
|
57
73
|
if (prop.type === "ref") {
|
|
58
|
-
typeArgs =
|
|
74
|
+
typeArgs = `${prop.childType}`;
|
|
59
75
|
}
|
|
60
76
|
else if (prop.type === "array") {
|
|
61
77
|
typeArgs = (isUpcaseFirst)
|
|
62
|
-
?
|
|
63
|
-
:
|
|
78
|
+
? `[ ${prop.childType} ]`
|
|
79
|
+
: `[ "${prop.childType}" ]`;
|
|
64
80
|
}
|
|
65
81
|
else if (prop.type === "map") {
|
|
66
82
|
typeArgs = (isUpcaseFirst)
|
|
67
|
-
?
|
|
68
|
-
:
|
|
83
|
+
? `{ map: ${prop.childType} }`
|
|
84
|
+
: `{ map: "${prop.childType}" }`;
|
|
69
85
|
}
|
|
70
86
|
}
|
|
71
87
|
else {
|
|
72
|
-
typeArgs = "
|
|
88
|
+
typeArgs = `"${prop.type}"`;
|
|
73
89
|
}
|
|
74
|
-
return
|
|
90
|
+
return `type(${typeArgs})(${className}.prototype, "${prop.name}");`;
|
|
75
91
|
}
|
|
76
92
|
function generatePropertyInitializer(prop) {
|
|
77
|
-
|
|
93
|
+
let initializer = "";
|
|
78
94
|
if (prop.type === "ref") {
|
|
79
|
-
initializer =
|
|
95
|
+
initializer = `new ${prop.childType}()`;
|
|
80
96
|
}
|
|
81
97
|
else if (prop.type === "array") {
|
|
82
|
-
initializer =
|
|
98
|
+
initializer = `new schema.ArraySchema()`;
|
|
83
99
|
}
|
|
84
100
|
else if (prop.type === "map") {
|
|
85
|
-
initializer =
|
|
101
|
+
initializer = `new schema.MapSchema()`;
|
|
86
102
|
}
|
|
87
|
-
return
|
|
103
|
+
return `this.${prop.name} = ${initializer}`;
|
|
88
104
|
}
|
|
89
105
|
//# sourceMappingURL=js.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js.js","sourceRoot":"","sources":["../../../src/codegen/languages/js.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"js.js","sourceRoot":"","sources":["../../../src/codegen/languages/js.ts"],"names":[],"mappings":";;;AAAA,oCAAgG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, Context } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\nconst typeMaps = {\n \"string\": \"string\",\n \"number\": \"number\",\n \"boolean\": \"boolean\",\n \"int8\": \"number\",\n \"uint8\": \"number\",\n \"int16\": \"number\",\n \"uint16\": \"number\",\n \"int32\": \"number\",\n \"uint32\": \"number\",\n \"int64\": \"number\",\n \"uint64\": \"number\",\n \"float32\": \"number\",\n \"float64\": \"number\",\n}\n\nconst distinct = (value, index, self) => self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n return context.classes.map(klass => ({\n name: klass.name + \".js\",\n content: generateClass(klass, options.namespace, context.classes)\n }));\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n const allRefs: Property[] = [];\n klass.properties.forEach(property => {\n let type = property.type;\n\n // keep all refs list\n if ((type === \"ref\" || type === \"array\" || type === \"map\")) {\n allRefs.push(property);\n }\n });\n\n return `${getCommentHeader()}\n\nconst schema = require(\"@colyseus/schema\");\nconst Schema = schema.Schema;\nconst type = schema.type;\n${allRefs.\n filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n map(ref => ref.childType).\n concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n filter(distinct).\n map(childType => `const ${childType} = require(\"./${childType}\");`).\n join(\"\\n\")}\n\nclass ${klass.name} extends ${klass.extends} {\n constructor () {\n super();\n${klass.properties.\n filter(prop => prop.childType !== undefined).\n map(prop => \" \" + generatePropertyInitializer(prop)).join(\"\\n\")}\n }\n}\n${klass.properties.map(prop => generatePropertyDeclaration(klass.name, prop)).join(\"\\n\")}\n\nexport default ${klass.name};\n`;\n}\n\nfunction generatePropertyDeclaration(className: string, prop: Property) {\n let typeArgs: string;\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n if (isUpcaseFirst) {\n typeArgs += `, ${prop.childType}`;\n\n } else {\n typeArgs += `, \"${prop.childType}\"`;\n }\n\n if(prop.type === \"ref\") {\n typeArgs = `${prop.childType}`;\n\n } else if(prop.type === \"array\") {\n typeArgs = (isUpcaseFirst)\n ? `[ ${prop.childType} ]`\n : `[ \"${prop.childType}\" ]`;\n\n } else if(prop.type === \"map\") {\n typeArgs = (isUpcaseFirst)\n ? `{ map: ${prop.childType} }`\n : `{ map: \"${prop.childType}\" }`;\n }\n\n } else {\n typeArgs = `\"${prop.type}\"`;\n }\n\n return `type(${typeArgs})(${className}.prototype, \"${prop.name}\");`;\n}\n\nfunction generatePropertyInitializer(prop: Property) {\n let initializer = \"\";\n\n if(prop.type === \"ref\") {\n initializer = `new ${prop.childType}()`;\n\n } else if(prop.type === \"array\") {\n initializer = `new schema.ArraySchema()`;\n\n } else if(prop.type === \"map\") {\n initializer = `new schema.MapSchema()`;\n }\n\n return `this.${prop.name} = ${initializer}`;\n}\n"]}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generate = void 0;
|
|
4
|
-
|
|
4
|
+
const types_1 = require("../types");
|
|
5
5
|
/**
|
|
6
6
|
TODO:
|
|
7
7
|
- Support inheritance
|
|
8
8
|
- Support importing Schema dependencies
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
const typeMaps = {
|
|
11
11
|
"string": "string",
|
|
12
12
|
"number": "number",
|
|
13
13
|
"boolean": "boolean",
|
|
@@ -22,31 +22,42 @@ var typeMaps = {
|
|
|
22
22
|
"float32": "number",
|
|
23
23
|
"float64": "number",
|
|
24
24
|
};
|
|
25
|
-
|
|
25
|
+
const distinct = (value, index, self) => self.indexOf(value) === index;
|
|
26
26
|
function generate(context, options) {
|
|
27
|
-
return context.classes.map(
|
|
27
|
+
return context.classes.map(klass => ({
|
|
28
28
|
name: klass.name + ".lua",
|
|
29
29
|
content: generateClass(klass, options.namespace, context.classes)
|
|
30
|
-
})
|
|
30
|
+
}));
|
|
31
31
|
}
|
|
32
32
|
exports.generate = generate;
|
|
33
33
|
function generateClass(klass, namespace, allClasses) {
|
|
34
|
-
|
|
35
|
-
klass.properties.forEach(
|
|
36
|
-
|
|
34
|
+
const allRefs = [];
|
|
35
|
+
klass.properties.forEach(property => {
|
|
36
|
+
let type = property.type;
|
|
37
37
|
// keep all refs list
|
|
38
38
|
if ((type === "ref" || type === "array" || type === "map")) {
|
|
39
39
|
allRefs.push(property);
|
|
40
40
|
}
|
|
41
41
|
});
|
|
42
42
|
// TOOD: inheritance
|
|
43
|
-
return types_1.getCommentHeader().replace(/\/\//mg, "--")
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
return `${(0, types_1.getCommentHeader)().replace(/\/\//mg, "--")}
|
|
44
|
+
|
|
45
|
+
local schema = require 'colyseus.serialization.schema.schema'
|
|
46
|
+
${allRefs.
|
|
47
|
+
filter(ref => ref.childType && typeMaps[ref.childType] === undefined).
|
|
48
|
+
map(ref => ref.childType).
|
|
49
|
+
concat((0, types_1.getInheritanceTree)(klass, allClasses, false).map(klass => klass.name)).
|
|
47
50
|
filter(distinct).
|
|
48
|
-
map(
|
|
49
|
-
join("\n")
|
|
51
|
+
map(childType => `local ${childType} = require '${(namespace ? `${namespace}.` : '')}${childType}'`).
|
|
52
|
+
join("\n")}
|
|
53
|
+
|
|
54
|
+
local ${klass.name} = schema.define({
|
|
55
|
+
${klass.properties.map(prop => generatePropertyDeclaration(prop)).join(",\n")},
|
|
56
|
+
["_fields_by_index"] = { ${klass.properties.map(prop => `"${prop.name}"`).join(", ")} },
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
return ${klass.name}
|
|
60
|
+
`;
|
|
50
61
|
// ["on_change"] = function(changes)
|
|
51
62
|
// -- on change logic here
|
|
52
63
|
// end,
|
|
@@ -58,30 +69,30 @@ function generateClass(klass, namespace, allClasses) {
|
|
|
58
69
|
// end,
|
|
59
70
|
}
|
|
60
71
|
function generatePropertyDeclaration(prop) {
|
|
61
|
-
|
|
72
|
+
let typeArgs;
|
|
62
73
|
if (prop.childType) {
|
|
63
|
-
|
|
74
|
+
const isUpcaseFirst = prop.childType.match(/^[A-Z]/);
|
|
64
75
|
if (isUpcaseFirst) {
|
|
65
|
-
typeArgs +=
|
|
76
|
+
typeArgs += `${prop.childType}`;
|
|
66
77
|
}
|
|
67
78
|
else {
|
|
68
|
-
typeArgs += "
|
|
79
|
+
typeArgs += `"${prop.childType}"`;
|
|
69
80
|
}
|
|
70
81
|
if (prop.type === "ref") {
|
|
71
82
|
typeArgs = (isUpcaseFirst)
|
|
72
|
-
?
|
|
73
|
-
: "
|
|
83
|
+
? `${prop.childType}`
|
|
84
|
+
: `"${prop.childType}"`;
|
|
74
85
|
}
|
|
75
86
|
else {
|
|
76
87
|
typeArgs = (isUpcaseFirst)
|
|
77
|
-
?
|
|
78
|
-
:
|
|
88
|
+
? `{ ${prop.type} = ${prop.childType} }`
|
|
89
|
+
: `{ ${prop.type} = "${prop.childType}" }`;
|
|
79
90
|
}
|
|
80
91
|
}
|
|
81
92
|
else {
|
|
82
|
-
typeArgs = "
|
|
93
|
+
typeArgs = `"${prop.type}"`;
|
|
83
94
|
}
|
|
84
|
-
return
|
|
95
|
+
return ` ["${prop.name}"] = ${typeArgs}`;
|
|
85
96
|
}
|
|
86
97
|
// function generatePropertyInitializer(prop: Property) {
|
|
87
98
|
// let initializer = "";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lua.js","sourceRoot":"","sources":["../../../src/codegen/languages/lua.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"lua.js","sourceRoot":"","sources":["../../../src/codegen/languages/lua.ts"],"names":[],"mappings":";;;AAAA,oCAAgG;AAGhG;;;;EAIE;AAEF,MAAM,QAAQ,GAAG;IACb,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,QAAQ;IAClB,SAAS,EAAE,SAAS;IACpB,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,QAAQ;IACjB,OAAO,EAAE,QAAQ;IACj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AC,SAAS,GAAG,CAAC;SACrC;QAED,IAAG,IAAI,CAAC,IAAI,KAAK,KAAK,EAAE;YACpB,QAAQ,GAAG,CAAC,aAAa,CAAC;gBACtB,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE;gBACrB,CAAC,CAAC,IAAI,IAAI,CAAC,SAAS,GAAG,CAAC;SAE/B;aAAM;YACH,QAAQ,GAAG,CAAC,aAAa,CAAC;gBACtB,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,SAAS,IAAI;gBACxC,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,SAAS,KAAK,CAAC;SAClD;KAEJ;SAAM;QACH,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC;KAC/B;IAED,OAAO,SAAS,IAAI,CAAC,IAAI,QAAQ,QAAQ,EAAE,CAAC;AAChD,CAAC;AAED,yDAAyD;AACzD,4BAA4B;AAE5B,gCAAgC;AAChC,mDAAmD;AAEnD,yCAAyC;AACzC,oDAAoD;AAEpD,uCAAuC;AACvC,kDAAkD;AAClD,QAAQ;AAER,mDAAmD;AACnD,IAAI","sourcesContent":["import { Class, Property, File, getCommentHeader, getInheritanceTree, Context } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\n/**\n TODO:\n - Support inheritance\n - Support importing Schema dependencies\n*/\n\nconst typeMaps = {\n \"string\": \"string\",\n \"number\": \"number\",\n \"boolean\": \"boolean\",\n \"int8\": \"number\",\n \"uint8\": \"number\",\n \"int16\": \"number\",\n \"uint16\": \"number\",\n \"int32\": \"number\",\n \"uint32\": \"number\",\n \"int64\": \"number\",\n \"uint64\": \"number\",\n \"float32\": \"number\",\n \"float64\": \"number\",\n}\n\nconst distinct = (value, index, self) => self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n return context.classes.map(klass => ({\n name: klass.name + \".lua\",\n content: generateClass(klass, options.namespace, context.classes)\n }));\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n const allRefs: Property[] = [];\n klass.properties.forEach(property => {\n let type = property.type;\n\n // keep all refs list\n if ((type === \"ref\" || type === \"array\" || type === \"map\")) {\n allRefs.push(property);\n }\n });\n\n// TOOD: inheritance\n\n return `${getCommentHeader().replace(/\\/\\//mg, \"--\")}\n\nlocal schema = require 'colyseus.serialization.schema.schema'\n${allRefs.\n filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n map(ref => ref.childType).\n concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n filter(distinct).\n map(childType => `local ${childType} = require '${(namespace ? `${namespace}.` : '')}${childType}'`).\n join(\"\\n\")}\n\nlocal ${klass.name} = schema.define({\n${klass.properties.map(prop => generatePropertyDeclaration(prop)).join(\",\\n\")},\n [\"_fields_by_index\"] = { ${klass.properties.map(prop => `\"${prop.name}\"`).join(\", \")} },\n})\n\nreturn ${klass.name}\n`;\n\n // [\"on_change\"] = function(changes)\n // -- on change logic here\n // end,\n\n // [\"on_add\"] = function()\n // -- on add logic here\n // end,\n\n // [\"on_remove\"] = function()\n // -- on remove logic here\n // end,\n}\n\nfunction generatePropertyDeclaration(prop: Property) {\n let typeArgs: string;\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n if (isUpcaseFirst) {\n typeArgs += `${prop.childType}`;\n\n } else {\n typeArgs += `\"${prop.childType}\"`;\n }\n\n if(prop.type === \"ref\") {\n typeArgs = (isUpcaseFirst)\n ? `${prop.childType}`\n : `\"${prop.childType}\"`;\n\n } else {\n typeArgs = (isUpcaseFirst)\n ? `{ ${prop.type} = ${prop.childType} }`\n : `{ ${prop.type} = \"${prop.childType}\" }`;\n }\n\n } else {\n typeArgs = `\"${prop.type}\"`;\n }\n\n return ` [\"${prop.name}\"] = ${typeArgs}`;\n}\n\n// function generatePropertyInitializer(prop: Property) {\n// let initializer = \"\";\n\n// if(prop.type === \"ref\") {\n// initializer = `new ${prop.childType}()`;\n\n// } else if(prop.type === \"array\") {\n// initializer = `new schema.ArraySchema()`;\n\n// } else if(prop.type === \"map\") {\n// initializer = `new schema.MapSchema()`;\n// }\n\n// return `this.${prop.name} = ${initializer}`;\n// }\n"]}
|
|
@@ -1,29 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
19
|
-
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
20
|
-
to[j] = from[i];
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
3
|
exports.generate = void 0;
|
|
25
|
-
|
|
26
|
-
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const typeMaps = {
|
|
27
6
|
"string": "string",
|
|
28
7
|
"number": "number",
|
|
29
8
|
"boolean": "boolean",
|
|
@@ -38,88 +17,104 @@ var typeMaps = {
|
|
|
38
17
|
"float32": "number",
|
|
39
18
|
"float64": "number",
|
|
40
19
|
};
|
|
41
|
-
|
|
20
|
+
const distinct = (value, index, self) => self.indexOf(value) === index;
|
|
42
21
|
function generate(context, options) {
|
|
43
|
-
return
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
22
|
+
return [
|
|
23
|
+
...context.classes.map(structure => ({
|
|
24
|
+
name: structure.name + ".ts",
|
|
25
|
+
content: generateClass(structure, options.namespace, context.classes)
|
|
26
|
+
})),
|
|
27
|
+
...context.interfaces.map(structure => ({
|
|
28
|
+
name: structure.name + ".ts",
|
|
29
|
+
content: generateInterface(structure, options.namespace, context.classes),
|
|
30
|
+
}))
|
|
31
|
+
];
|
|
50
32
|
}
|
|
51
33
|
exports.generate = generate;
|
|
52
34
|
function generateClass(klass, namespace, allClasses) {
|
|
53
|
-
|
|
54
|
-
klass.properties.forEach(
|
|
55
|
-
|
|
35
|
+
const allRefs = [];
|
|
36
|
+
klass.properties.forEach(property => {
|
|
37
|
+
let type = property.type;
|
|
56
38
|
// keep all refs list
|
|
57
39
|
if ((type === "ref" || type === "array" || type === "map" || type === "set")) {
|
|
58
40
|
allRefs.push(property);
|
|
59
41
|
}
|
|
60
42
|
});
|
|
61
|
-
return types_1.getCommentHeader()
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
43
|
+
return `${(0, types_1.getCommentHeader)()}
|
|
44
|
+
|
|
45
|
+
import { Schema, type, ArraySchema, MapSchema, SetSchema, DataChange } from '@colyseus/schema';
|
|
46
|
+
${allRefs.
|
|
47
|
+
filter(ref => ref.childType && typeMaps[ref.childType] === undefined).
|
|
48
|
+
map(ref => ref.childType).
|
|
49
|
+
concat((0, types_1.getInheritanceTree)(klass, allClasses, false).map(klass => klass.name)).
|
|
65
50
|
filter(distinct).
|
|
66
|
-
map(
|
|
67
|
-
join("\n")
|
|
51
|
+
map(childType => `import { ${childType} } from './${childType}'`).
|
|
52
|
+
join("\n")}
|
|
53
|
+
|
|
54
|
+
export class ${klass.name} extends ${klass.extends} {
|
|
55
|
+
${klass.properties.map(prop => ` ${generateProperty(prop)}`).join("\n")}
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
68
58
|
}
|
|
69
59
|
function generateProperty(prop) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
60
|
+
let langType;
|
|
61
|
+
let initializer = "";
|
|
62
|
+
let typeArgs;
|
|
73
63
|
if (prop.childType) {
|
|
74
|
-
|
|
64
|
+
const isUpcaseFirst = prop.childType.match(/^[A-Z]/);
|
|
75
65
|
if (isUpcaseFirst) {
|
|
76
|
-
typeArgs +=
|
|
66
|
+
typeArgs += `, ${prop.childType}`;
|
|
77
67
|
}
|
|
78
68
|
else {
|
|
79
|
-
typeArgs +=
|
|
69
|
+
typeArgs += `, "${prop.childType}"`;
|
|
80
70
|
}
|
|
81
71
|
if (prop.type === "ref") {
|
|
82
|
-
langType =
|
|
83
|
-
initializer =
|
|
84
|
-
typeArgs =
|
|
72
|
+
langType = `${prop.childType}`;
|
|
73
|
+
initializer = `new ${prop.childType}()`;
|
|
74
|
+
typeArgs = `${prop.childType}`;
|
|
85
75
|
}
|
|
86
76
|
else if (prop.type === "array") {
|
|
87
77
|
langType = (isUpcaseFirst)
|
|
88
|
-
?
|
|
89
|
-
:
|
|
90
|
-
initializer =
|
|
78
|
+
? `ArraySchema<${prop.childType}>`
|
|
79
|
+
: `ArraySchema<${typeMaps[prop.childType]}>`;
|
|
80
|
+
initializer = `new ${langType}()`;
|
|
91
81
|
typeArgs = (isUpcaseFirst)
|
|
92
|
-
?
|
|
93
|
-
:
|
|
82
|
+
? `[ ${prop.childType} ]`
|
|
83
|
+
: `[ "${prop.childType}" ]`;
|
|
94
84
|
}
|
|
95
85
|
else if (prop.type === "map") {
|
|
96
86
|
langType = (isUpcaseFirst)
|
|
97
|
-
?
|
|
98
|
-
:
|
|
99
|
-
initializer =
|
|
87
|
+
? `MapSchema<${prop.childType}>`
|
|
88
|
+
: `MapSchema<${typeMaps[prop.childType]}>`;
|
|
89
|
+
initializer = `new ${langType}()`;
|
|
100
90
|
typeArgs = (isUpcaseFirst)
|
|
101
|
-
?
|
|
102
|
-
:
|
|
91
|
+
? `{ map: ${prop.childType} }`
|
|
92
|
+
: `{ map: "${prop.childType}" }`;
|
|
103
93
|
}
|
|
104
94
|
else if (prop.type === "set") {
|
|
105
95
|
langType = (isUpcaseFirst)
|
|
106
|
-
?
|
|
107
|
-
:
|
|
108
|
-
initializer =
|
|
96
|
+
? `SetSchema<${prop.childType}>`
|
|
97
|
+
: `SetSchema<${typeMaps[prop.childType]}>`;
|
|
98
|
+
initializer = `new ${langType}()`;
|
|
109
99
|
typeArgs = (isUpcaseFirst)
|
|
110
|
-
?
|
|
111
|
-
:
|
|
100
|
+
? `{ set: ${prop.childType} }`
|
|
101
|
+
: `{ set: "${prop.childType}" }`;
|
|
112
102
|
}
|
|
113
103
|
}
|
|
114
104
|
else {
|
|
115
105
|
langType = typeMaps[prop.type];
|
|
116
|
-
typeArgs = "
|
|
106
|
+
typeArgs = `"${prop.type}"`;
|
|
117
107
|
}
|
|
118
108
|
// TS1263: "Declarations with initializers cannot also have definite assignment assertions"
|
|
119
|
-
|
|
120
|
-
return
|
|
109
|
+
const definiteAssertion = initializer ? "" : "!";
|
|
110
|
+
return `@type(${typeArgs}) public ${prop.name}${definiteAssertion}: ${langType}${(initializer) ? ` = ${initializer}` : ""};`;
|
|
121
111
|
}
|
|
122
112
|
function generateInterface(structure, namespace, allClasses) {
|
|
123
|
-
return
|
|
113
|
+
return `${(0, types_1.getCommentHeader)()}
|
|
114
|
+
|
|
115
|
+
export interface ${structure.name} {
|
|
116
|
+
${structure.properties.map(prop => ` ${prop.name}: ${prop.type};`).join("\n")}
|
|
117
|
+
}
|
|
118
|
+
`;
|
|
124
119
|
}
|
|
125
120
|
//# sourceMappingURL=ts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ts.js","sourceRoot":"","sources":["../../../src/codegen/languages/ts.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ts.js","sourceRoot":"","sources":["../../../src/codegen/languages/ts.ts"],"names":[],"mappings":";;;AAAA,oCAA2G;AAG3G,MAAM,QAAQ,GAAG;IACb,QAAQ,EAAE,QAAQ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AAC,aAAa,IAAI,CAAC,SAAS,GAAG;gBAChC,CAAC,CAAC,aAAa,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC;YAC/C,WAAW,GAAG,OAAO,QAAQ,IAAI,CAAC;YAClC,QAAQ,GAAG,CAAC,aAAa,CAAC;gBACtB,CAAC,CAAC,UAAU,IAAI,CAAC,SAAS,IAAI;gBAC9B,CAAC,CAAC,WAAW,IAAI,CAAC,SAAS,KAAK,CAAC;SACxC;KAEJ;SAAM;QACH,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,QAAQ,GAAG,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC;KAC/B;IAED,2FAA2F;IAC3F,MAAM,iBAAiB,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;IAEjD,OAAO,SAAS,QAAQ,YAAY,IAAI,CAAC,IAAI,GAAG,iBAAiB,KAAK,QAAQ,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,MAAM,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAA;AAChI,CAAC;AAGD,SAAS,iBAAiB,CAAC,SAAoB,EAAE,SAAiB,EAAE,UAAmB;IACnF,OAAO,GAAG,IAAA,wBAAgB,GAAE;;mBAEb,SAAS,CAAC,IAAI;EAC/B,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;;CAE/E,CAAC;AACF,CAAC","sourcesContent":["import { Class, Property, File, getCommentHeader, getInheritanceTree, Context, Interface } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\nconst typeMaps = {\n \"string\": \"string\",\n \"number\": \"number\",\n \"boolean\": \"boolean\",\n \"int8\": \"number\",\n \"uint8\": \"number\",\n \"int16\": \"number\",\n \"uint16\": \"number\",\n \"int32\": \"number\",\n \"uint32\": \"number\",\n \"int64\": \"number\",\n \"uint64\": \"number\",\n \"float32\": \"number\",\n \"float64\": \"number\",\n}\n\nconst distinct = (value, index, self) => self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n return [\n ...context.classes.map(structure => ({\n name: structure.name + \".ts\",\n content: generateClass(structure, options.namespace, context.classes)\n })),\n ...context.interfaces.map(structure => ({\n name: structure.name + \".ts\",\n content: generateInterface(structure, options.namespace, context.classes),\n }))\n ];\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n const allRefs: Property[] = [];\n klass.properties.forEach(property => {\n let type = property.type;\n\n // keep all refs list\n if ((type === \"ref\" || type === \"array\" || type === \"map\" || type === \"set\")) {\n allRefs.push(property);\n }\n });\n\n return `${getCommentHeader()}\n\nimport { Schema, type, ArraySchema, MapSchema, SetSchema, DataChange } from '@colyseus/schema';\n${allRefs.\n filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n map(ref => ref.childType).\n concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n filter(distinct).\n map(childType => `import { ${childType} } from './${childType}'`).\n join(\"\\n\")}\n\nexport class ${klass.name} extends ${klass.extends} {\n${klass.properties.map(prop => ` ${generateProperty(prop)}`).join(\"\\n\")}\n}\n`;\n}\n\nfunction generateProperty(prop: Property) {\n let langType: string;\n let initializer = \"\";\n let typeArgs: string;\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n if (isUpcaseFirst) {\n typeArgs += `, ${prop.childType}`;\n\n } else {\n typeArgs += `, \"${prop.childType}\"`;\n }\n\n if(prop.type === \"ref\") {\n langType = `${prop.childType}`;\n initializer = `new ${prop.childType}()`;\n typeArgs = `${prop.childType}`;\n\n } else if(prop.type === \"array\") {\n langType = (isUpcaseFirst)\n ? `ArraySchema<${prop.childType}>`\n : `ArraySchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `[ ${prop.childType} ]`\n : `[ \"${prop.childType}\" ]`;\n\n } else if(prop.type === \"map\") {\n langType = (isUpcaseFirst)\n ? `MapSchema<${prop.childType}>`\n : `MapSchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `{ map: ${prop.childType} }`\n : `{ map: \"${prop.childType}\" }`;\n } else if (prop.type === \"set\") {\n langType = (isUpcaseFirst)\n ? `SetSchema<${prop.childType}>`\n : `SetSchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `{ set: ${prop.childType} }`\n : `{ set: \"${prop.childType}\" }`;\n }\n\n } else {\n langType = typeMaps[prop.type];\n typeArgs = `\"${prop.type}\"`;\n }\n\n // TS1263: \"Declarations with initializers cannot also have definite assignment assertions\"\n const definiteAssertion = initializer ? \"\" : \"!\";\n\n return `@type(${typeArgs}) public ${prop.name}${definiteAssertion}: ${langType}${(initializer) ? ` = ${initializer}` : \"\"};`\n}\n\n\nfunction generateInterface(structure: Interface, namespace: string, allClasses: Class[]) {\n return `${getCommentHeader()}\n\nexport interface ${structure.name} {\n${structure.properties.map(prop => ` ${prop.name}: ${prop.type};`).join(\"\\n\")}\n}\n`;\n}\n"]}
|
package/lib/codegen/parser.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import { Class, Interface, Context, Enum } from "./types";
|
|
2
3
|
export declare function parseFiles(fileNames: string[], decoratorName?: string, context?: Context): {
|
|
3
4
|
classes: Class[];
|
|
4
5
|
interfaces: Interface[];
|
|
6
|
+
enums: Enum[];
|
|
5
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* TypeScript 4.8+ has introduced a change on how to access decorators.
|
|
10
|
+
* - https://github.com/microsoft/TypeScript/pull/49089
|
|
11
|
+
* - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
|
|
12
|
+
*/
|
|
13
|
+
export declare function getDecorators(node: ts.Node | null | undefined): undefined | ts.Decorator[];
|