airship-server
Version:
Airship is a framework for Node.JS & TypeScript that helps you to write big, scalable and maintainable API servers.
178 lines • 7.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const SourceCode_1 = require("../domain/SourceCode");
const Utils_1 = require("./Utils");
const VectorType_1 = require("../domain/types/VectorType");
const CustomType_1 = require("../domain/types/CustomType");
const IntBoolType_1 = require("../domain/types/IntBoolType");
const AnyType_1 = require("../domain/types/AnyType");
const NumberType_1 = require("../domain/types/NumberType");
const StringType_1 = require("../domain/types/StringType");
const BooleanType_1 = require("../domain/types/BooleanType");
class SwiftCodeGenerator {
generateClass(scheme) {
let code = new SourceCode_1.default();
let props = this.generateProps(scheme);
let constructor = this.generateClassConstructor(scheme);
let deserializeMethod = this.generateDeserializeMethod(scheme);
let serializeMethod = this.generateSerializeMethod(scheme);
code.add(`class ${scheme.name} {`);
code.append(props);
code.add('');
code.append(constructor, 1);
code.add('');
code.append(deserializeMethod, 1);
code.add('');
code.append(serializeMethod, 1);
code.add('}');
return code;
}
generateApiMethod(scheme) {
throw new Error('Not implemented');
}
generateApiMethodParamsInterface(scheme) {
throw new Error('Not implemented');
}
generateProps(scheme) {
let code = new SourceCode_1.default();
scheme.fields.forEach((field, index) => {
code.add(`public var ${Utils_1.toCamelCase(field.name)}: ${this.renderType(field.type)}`, 1);
});
return code;
}
generateClassConstructor(scheme) {
let code = new SourceCode_1.default();
code.add('init (');
scheme.fields.forEach((field, index) => {
let coma = this.genComa(scheme.fields, index);
code.add(`${Utils_1.toCamelCase(field.name)}: ${this.renderType(field.type)}${coma}`, 1);
});
code.add(') {');
scheme.fields.forEach((field, index) => {
code.add(`self.${Utils_1.toCamelCase(field.name)} = ${Utils_1.toCamelCase(field.name)}`, 1);
});
code.add('}');
return code;
}
generateDeserializeMethod(scheme) {
let code = new SourceCode_1.default();
code.add(`public static func deserialize(raw: [String: Any]?) -> ${scheme.name}? {`);
code.add(`guard let raw = raw else {`, 1);
code.add(`return nil`, 2);
code.add(`}`, 1);
code.add('');
code.add(`return ${scheme.name} (`, 1);
scheme.fields.forEach((field, index) => {
let coma = this.genComa(scheme.fields, index);
let fieldVar = `${Utils_1.toCamelCase(field.name)}: raw["${field.name}"] as? ${this.renderNonOptionalType(field.type)}`;
if (field.type instanceof VectorType_1.default)
code.add(this.renderVectorDeserialize(`${Utils_1.toCamelCase(field.name)}: (raw["${field.name}"] as? [Any])`, field.type) + coma, 2);
else if (field.type instanceof CustomType_1.default)
code.add(`${Utils_1.toCamelCase(field.name)}: ${field.type.name}.deserialize(raw: raw["${field.name}"] as? [String : Any])${coma}`, 2);
else if (field.type instanceof IntBoolType_1.default)
code.add(`!!${fieldVar}${coma}`, 2);
else
code.add(fieldVar + coma, 2);
});
code.add(`)`, 1);
code.add('}');
return code;
}
generateSerializeMethod(scheme) {
let code = new SourceCode_1.default();
code.add(`public func serialize() -> [String: Any] {`);
code.add(`return [`, 1);
scheme.fields.forEach((field, index) => {
let coma = this.genComa(scheme.fields, index);
let fieldVar = `"${field.name}": self.${Utils_1.toCamelCase(field.name)}`;
if (field.type instanceof VectorType_1.default)
code.add(`"${field.name}": ${this.renderVectorSerialize(`self.${Utils_1.toCamelCase(field.name)}?`, field.type) + coma}`, 2);
else if (field.type instanceof CustomType_1.default)
code.add(`${fieldVar}?.serialize()${coma}`, 2);
else
code.add(`${fieldVar}${coma}`, 2);
});
code.add(']', 1);
code.add('}');
return code;
}
renderType(type, withoutUndefined = false) {
if (type instanceof StringType_1.default)
return 'String?';
if (type instanceof NumberType_1.default)
return 'Int?';
// if (type instanceof FloatType)
// return 'Float?'
if (type instanceof AnyType_1.default)
return 'Any?';
if (type instanceof BooleanType_1.default)
return 'Bool?';
if (type instanceof IntBoolType_1.default)
return 'Bool?';
if (type instanceof CustomType_1.default)
return type.name + `${!withoutUndefined ? '?' : ''}`;
if (type instanceof VectorType_1.default) {
return '[' + this.renderNonOptionalType(type.item) + `]${!withoutUndefined ? '?' : ''}`;
}
throw new Error('UNSUPPORTED TYPE' + JSON.stringify(type));
}
renderNonOptionalType(type) {
if (type instanceof StringType_1.default)
return 'String';
if (type instanceof NumberType_1.default)
return 'Int';
// if (type instanceof FloatType)
// return 'Float'
if (type instanceof AnyType_1.default)
return 'Any';
if (type instanceof BooleanType_1.default)
return 'Bool';
if (type instanceof IntBoolType_1.default)
return 'Bool';
if (type instanceof CustomType_1.default)
return type.name;
if (type instanceof VectorType_1.default) {
return '[' + this.renderNonOptionalType(type.item) + ']';
}
throw new Error('UNSUPPORTED TYPE' + JSON.stringify(type));
}
genComa(list, index) {
return (index == list.length - 1) ? '' : ',';
}
renderVectorDeserialize(value, type) {
let code = '';
if (type instanceof VectorType_1.default)
code += `${value}?.flatMap({${this.renderVectorDeserialize('$0', type.item)}})`;
else if (type instanceof CustomType_1.default)
code += `${type.name}.deserialize(raw: ${value} as? [String: Any])`;
else
code += `${value} as? ${this.renderNonOptionalType(type)}`;
return code;
}
renderVectorSerialize(value, type) {
let code = '';
if (type instanceof VectorType_1.default)
code += `${value}.map({${this.renderVectorSerialize('$0', type.item)}})`;
else if (type instanceof CustomType_1.default)
code += `${value}.serialize()`;
else
code += value;
return code;
}
isCustomType(type) {
if (type instanceof CustomType_1.default)
return true;
if (type instanceof VectorType_1.default)
return this.isCustomType(type.item);
return false;
}
getCustomType(type) {
if (type instanceof VectorType_1.default)
return this.getCustomType(type.item);
if (type instanceof CustomType_1.default)
return type;
return null;
}
}
exports.default = SwiftCodeGenerator;
//# sourceMappingURL=SwiftCodeGenerator.js.map