@colyseus/schema
Advanced tools
Comparing version 1.0.44 to 1.0.45
@@ -54,3 +54,7 @@ "use strict"; | ||
function generate(context, options) { | ||
return __spreadArray(__spreadArray([], __read(context.classes.map(function (structure) { return ({ | ||
// enrich typeMaps with enums | ||
context.enums.forEach(function (structure) { | ||
typeMaps[structure.name] = structure.name; | ||
}); | ||
return __spreadArray(__spreadArray(__spreadArray([], __read(context.classes.map(function (structure) { return ({ | ||
name: "".concat(structure.name, ".cs"), | ||
@@ -60,3 +64,6 @@ content: generateClass(structure, options.namespace) | ||
name: "".concat(structure.name, ".cs"), | ||
content: generateInterface(structure, options.namespace) | ||
content: generateInterface(structure, options.namespace), | ||
}); })), false), __read(context.enums.filter(function (structure) { return structure.name !== 'OPERATION'; }).map(function (structure) { return ({ | ||
name: "".concat(structure.name, ".cs"), | ||
content: generateEnum(structure, options.namespace), | ||
}); })), false); | ||
@@ -69,2 +76,25 @@ } | ||
} | ||
function generateEnum(_enum, namespace) { | ||
var indent = namespace ? "\t" : ""; | ||
return "".concat((0, types_1.getCommentHeader)(), "\n").concat(namespace ? "\nnamespace ".concat(namespace, " {") : "", "\n").concat(indent, "public struct ").concat(_enum.name, " {\n\n").concat(_enum.properties | ||
.map(function (prop) { | ||
var dataType = "int"; | ||
var value; | ||
if (prop.type) { | ||
if (isNaN(Number(prop.type))) { | ||
value = prop.type; | ||
dataType = "string"; | ||
} | ||
else { | ||
value = Number(prop.type); | ||
dataType = Number.isInteger(value) ? 'int' : 'float'; | ||
} | ||
} | ||
else { | ||
value = _enum.properties.indexOf(prop); | ||
} | ||
return "".concat(indent, "\tpublic const ").concat(dataType, " ").concat(prop.name, " = ").concat(value, ";"); | ||
}) | ||
.join("\n"), "\n").concat(indent, "}"); | ||
} | ||
function generateProperty(prop, indent) { | ||
@@ -71,0 +101,0 @@ if (indent === void 0) { indent = ""; } |
import * as ts from "typescript"; | ||
import { Class, Interface, Context } from "./types"; | ||
import { Class, Interface, Context, Enum } from "./types"; | ||
export declare function parseFiles(fileNames: string[], decoratorName?: string, context?: Context): { | ||
classes: Class[]; | ||
interfaces: Interface[]; | ||
enums: Enum[]; | ||
}; | ||
@@ -7,0 +8,0 @@ /** |
@@ -29,3 +29,3 @@ "use strict"; | ||
function inspectNode(node, context, decoratorName) { | ||
var _a, _b, _c, _d, _e; | ||
var _a, _b, _c, _d, _e, _f; | ||
switch (node.kind) { | ||
@@ -60,2 +60,8 @@ case ts.SyntaxKind.ImportClause: | ||
break; | ||
case ts.SyntaxKind.EnumDeclaration: | ||
var enumName = node.name.escapedText.toString(); | ||
currentStructure = new types_1.Enum(); | ||
currentStructure.name = enumName; | ||
context.addStructure(currentStructure); | ||
break; | ||
case ts.SyntaxKind.ExtendsKeyword: | ||
@@ -151,2 +157,15 @@ // console.log(node.getText()); | ||
break; | ||
case ts.SyntaxKind.EnumMember: | ||
if (currentStructure instanceof types_1.Enum) { | ||
var initializer = (_f = node.initializer) === null || _f === void 0 ? void 0 : _f.getText(); | ||
var name = node.getFirstToken().getText(); | ||
var property = currentProperty || new types_1.Property(); | ||
property.name = name; | ||
if (initializer !== undefined) { | ||
property.type = initializer; | ||
} | ||
currentStructure.addProperty(property); | ||
currentProperty = undefined; | ||
} | ||
break; | ||
} | ||
@@ -153,0 +172,0 @@ ts.forEachChild(node, function (n) { return inspectNode(n, context, decoratorName); }); |
@@ -5,5 +5,7 @@ export declare function getCommentHeader(singleLineComment?: string): string; | ||
interfaces: Interface[]; | ||
enums: Enum[]; | ||
getStructures(): { | ||
classes: Class[]; | ||
interfaces: Interface[]; | ||
enums: Enum[]; | ||
}; | ||
@@ -34,2 +36,8 @@ addStructure(structure: IStructure): void; | ||
} | ||
export declare class Enum implements IStructure { | ||
context: Context; | ||
name: string; | ||
properties: Property[]; | ||
addProperty(property: Property): void; | ||
} | ||
export declare class Property { | ||
@@ -36,0 +44,0 @@ index: number; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getInheritanceTree = exports.Property = exports.Class = exports.Interface = exports.Context = exports.getCommentHeader = void 0; | ||
exports.getInheritanceTree = exports.Property = exports.Enum = exports.Class = exports.Interface = exports.Context = exports.getCommentHeader = void 0; | ||
var fs = require("fs"); | ||
@@ -16,2 +16,3 @@ var VERSION = JSON.parse(fs.readFileSync(__dirname + "/../../package.json").toString()).version; | ||
this.interfaces = []; | ||
this.enums = []; | ||
} | ||
@@ -36,2 +37,3 @@ Context.prototype.getStructures = function () { | ||
interfaces: this.interfaces, | ||
enums: this.enums, | ||
}; | ||
@@ -47,2 +49,5 @@ }; | ||
} | ||
else if (structure instanceof Enum) { | ||
this.enums.push(structure); | ||
} | ||
}; | ||
@@ -118,2 +123,12 @@ Context.prototype.getParentClass = function (klass) { | ||
exports.Class = Class; | ||
var Enum = /** @class */ (function () { | ||
function Enum() { | ||
this.properties = []; | ||
} | ||
Enum.prototype.addProperty = function (property) { | ||
this.properties.push(property); | ||
}; | ||
return Enum; | ||
}()); | ||
exports.Enum = Enum; | ||
var Property = /** @class */ (function () { | ||
@@ -120,0 +135,0 @@ function Property() { |
{ | ||
"name": "@colyseus/schema", | ||
"version": "1.0.44", | ||
"version": "1.0.45", | ||
"description": "Binary state serializer with delta encoding for games", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -1,2 +0,9 @@ | ||
import { Class, Property, File, getCommentHeader, Interface } from "../types"; | ||
import { | ||
Class, | ||
Property, | ||
File, | ||
getCommentHeader, | ||
Interface, | ||
Enum, | ||
} from "../types"; | ||
import { GenerateOptions } from "../api"; | ||
@@ -29,3 +36,7 @@ import { Context } from "../types"; | ||
export function generate (context: Context, options: GenerateOptions): File[] { | ||
export function generate(context: Context, options: GenerateOptions): File[] { | ||
// enrich typeMaps with enums | ||
context.enums.forEach((structure) => { | ||
typeMaps[structure.name] = structure.name; | ||
}); | ||
return [ | ||
@@ -38,4 +49,8 @@ ...context.classes.map(structure => ({ | ||
name: `${structure.name}.cs`, | ||
content: generateInterface(structure, options.namespace) | ||
})) | ||
content: generateInterface(structure, options.namespace), | ||
})), | ||
...context.enums.filter(structure => structure.name !== 'OPERATION').map((structure) => ({ | ||
name: `${structure.name}.cs`, | ||
content: generateEnum(structure, options.namespace), | ||
})), | ||
]; | ||
@@ -51,3 +66,3 @@ } | ||
${indent}public partial class ${klass.name} : ${klass.extends} { | ||
${klass.properties.map(prop => generateProperty(prop, indent)).join("\n\n")} | ||
${klass.properties.map((prop) => generateProperty(prop, indent)).join("\n\n")} | ||
${indent}} | ||
@@ -58,2 +73,30 @@ ${namespace ? "}" : ""} | ||
function generateEnum(_enum: Enum, namespace: string) { | ||
const indent = namespace ? "\t" : ""; | ||
return `${getCommentHeader()} | ||
${namespace ? `\nnamespace ${namespace} {` : ""} | ||
${indent}public struct ${_enum.name} { | ||
${_enum.properties | ||
.map((prop) => { | ||
let dataType: string = "int"; | ||
let value: any; | ||
if(prop.type) { | ||
if(isNaN(Number(prop.type))) { | ||
value = prop.type; | ||
dataType = "string"; | ||
} else { | ||
value = Number(prop.type); | ||
dataType = Number.isInteger(value)? 'int': 'float'; | ||
} | ||
} else { | ||
value = _enum.properties.indexOf(prop); | ||
} | ||
return `${indent}\tpublic const ${dataType} ${prop.name} = ${value};`; | ||
}) | ||
.join("\n")} | ||
${indent}}` | ||
} | ||
function generateProperty(prop: Property, indent: string = "") { | ||
@@ -60,0 +103,0 @@ let typeArgs = `"${prop.type}"`; |
import * as ts from "typescript"; | ||
import * as path from "path"; | ||
import { readFileSync } from "fs"; | ||
import { IStructure, Class, Interface, Property, Context } from "./types"; | ||
import { IStructure, Class, Interface, Property, Context, Enum } from "./types"; | ||
@@ -65,2 +65,11 @@ let currentStructure: IStructure; | ||
case ts.SyntaxKind.EnumDeclaration: | ||
const enumName = ( | ||
node as ts.EnumDeclaration | ||
).name.escapedText.toString(); | ||
currentStructure = new Enum(); | ||
currentStructure.name = enumName; | ||
context.addStructure(currentStructure); | ||
break; | ||
case ts.SyntaxKind.ExtendsKeyword: | ||
@@ -186,2 +195,16 @@ // console.log(node.getText()); | ||
break; | ||
case ts.SyntaxKind.EnumMember: | ||
if (currentStructure instanceof Enum) { | ||
const initializer = (node as any).initializer?.getText(); | ||
const name = node.getFirstToken().getText(); | ||
const property = currentProperty || new Property(); | ||
property.name = name; | ||
if (initializer !== undefined) { | ||
property.type = initializer; | ||
} | ||
currentStructure.addProperty(property); | ||
currentProperty = undefined; | ||
} | ||
break; | ||
} | ||
@@ -188,0 +211,0 @@ |
@@ -18,2 +18,3 @@ import * as fs from "fs"; | ||
interfaces: Interface[] = []; | ||
enums: Enum[] = []; | ||
@@ -37,2 +38,3 @@ getStructures() { | ||
interfaces: this.interfaces, | ||
enums: this.enums, | ||
}; | ||
@@ -46,5 +48,6 @@ } | ||
this.classes.push(structure); | ||
} else if (structure instanceof Interface) { | ||
this.interfaces.push(structure); | ||
} else if (structure instanceof Enum) { | ||
this.enums.push(structure); | ||
} | ||
@@ -140,2 +143,12 @@ } | ||
export class Enum implements IStructure { | ||
context: Context; | ||
name: string; | ||
properties: Property[] = []; | ||
addProperty(property: Property) { | ||
this.properties.push(property); | ||
} | ||
} | ||
export class Property { | ||
@@ -142,0 +155,0 @@ index: number; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1615518
20139