Skip to content

Commit

Permalink
version 0.9.17
Browse files Browse the repository at this point in the history
fix declaration order and xmlns import
  • Loading branch information
spreeuwers committed May 8, 2021
1 parent 4035a04 commit fa5c6d5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
19 changes: 12 additions & 7 deletions classGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ function choiceBody(m, names) {
}
function addNewImport(fileDef, ns) {
if (fileDef.imports.filter(function (i) { return i.starImportName === ns; }).length === 0) {
fileDef.addImport({ moduleSpecifier: ns2modMap[ns], starImportName: ns });
if (ns !== XMLNS) {
xml_utils_1.log('addNewImport: ', ns, ns2modMap[ns]);
fileDef.addImport({ moduleSpecifier: ns2modMap[ns], starImportName: ns });
}
}
}
function addClassForASTNode(fileDef, astNode, indent) {
Expand Down Expand Up @@ -288,12 +291,12 @@ var ClassGenerator = /** @class */ (function () {
.filter(function (t) { return t.nodeType === 'Enumeration'; })
.forEach(function (t) {
var enumDef = fileDef.addEnum({ name: xml_utils_1.capFirst(t.name) });
t.attr.values.forEach(function (m) { enumDef.addMember({ name: m.attr.value, value: "\"" + m.attr.value + "\"" }); });
t.attr.values.forEach(function (m) { enumDef.addMember({ name: m.attr.value.replace('+', '_'), value: "\"" + m.attr.value + "\"" }); });
if (t.attr.element) {
schemaClass.addProperty({ name: lowfirst(t.name), type: capfirst(t.name) });
}
});
var tmp = this.makeSortedFileDefinition(fileDef.classes);
var tmp = this.makeSortedFileDefinition(fileDef.classes, fileDef);
Object.keys(typeAliases).forEach(function (k) {
fileDef.addTypeAlias({ name: k, type: typeAliases[k], isExported: true });
});
Expand Down Expand Up @@ -343,13 +346,13 @@ var ClassGenerator = /** @class */ (function () {
console.log.apply(console, [message].concat(optionalParams));
}
};
ClassGenerator.prototype.makeSortedFileDefinition = function (sortedClasses) {
ClassGenerator.prototype.makeSortedFileDefinition = function (sortedClasses, fileDef) {
var _this = this;
// console.log('makeSortedFileDefinition ');
var outFile = ts_code_generator_1.createFile({ classes: [] });
//outFile.addImport({moduleSpecifier: "mod", starImportName: "nspce"});
for (var ns in this.importMap) {
xml_utils_1.log('ns ', ns, this.importMap[ns]);
xml_utils_1.log('addImport: ', ns, this.importMap[ns]);
outFile.addImport({ moduleSpecifier: this.importMap[ns], starImportName: ns });
}
var depth = 0;
Expand All @@ -359,11 +362,11 @@ var ClassGenerator = /** @class */ (function () {
while (depth <= max_depth) {
// console.log('depth ');
sortedClasses.forEach(function (c) {
var hDepth = _this.findHierachyDepth(c, _this.fileDef);
var hDepth = _this.findHierachyDepth(c, fileDef);
if (hDepth > max_depth) {
max_depth = hDepth;
}
_this.log(c.name + '\t' + hDepth);
_this.log('--DEPTH:', c.name + '\t' + hDepth);
if (hDepth === depth) {
if (c.name.indexOf(GROUP_PREFIX) === 0) {
// return;
Expand Down Expand Up @@ -466,9 +469,11 @@ var ClassGenerator = /** @class */ (function () {
var result = 0;
var superClassName = (c.extendsTypes[0]) ? c.extendsTypes[0].text : '';
while (superClassName) {
//console.log('superClassName1:', superClassName , result);
result++;
c = f.getClass(superClassName);
superClassName = (_a = c === null || c === void 0 ? void 0 : c.extendsTypes[0]) === null || _a === void 0 ? void 0 : _a.text;
//console.log('superClassName2:', superClassName , c, result);
}
return result;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xsd2ts",
"version": "0.9.16",
"version": "0.9.17",
"description": "generates template classes for an xsd",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 15 additions & 10 deletions src/classGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ASTNode, getFieldType, NEWLINE} from "./parsing";
import {capFirst, log} from "./xml-utils";
import {regexpPattern2typeAlias, A2Z} from './regexp2aliasType';
import {XsdGrammar} from "./xsd-grammar";
import isUndefined = require("lodash/fp/isUndefined");


let XMLNS = 'xmlns';
let definedTypes: string[];
Expand Down Expand Up @@ -47,7 +47,10 @@ function choiceBody(m: any, names: string[]): string {

function addNewImport(fileDef: FileDefinition, ns: string) {
if (fileDef.imports.filter(i => i.starImportName === ns).length === 0) {
fileDef.addImport({moduleSpecifier: ns2modMap[ns], starImportName: ns});
if (ns !== XMLNS) {
log('addNewImport: ', ns, ns2modMap[ns]);
fileDef.addImport({moduleSpecifier: ns2modMap[ns], starImportName: ns});
}
}
}
function addClassForASTNode(fileDef: FileDefinition, astNode: ASTNode, indent = ''): ClassDefinition {
Expand Down Expand Up @@ -182,7 +185,7 @@ function addClassForASTNode(fileDef: FileDefinition, astNode: ASTNode, indent =
export class ClassGenerator {
public types: string[] = [];
public schemaName = "schema";
public xmlnsName= "xmlns";
public xmlnsName = "xmlns";
private fileDef = createFile({classes: []});
private verbose = false;
private pluralPostFix = 's';
Expand Down Expand Up @@ -340,14 +343,14 @@ export class ClassGenerator {
.forEach((t) => {
const enumDef = fileDef.addEnum({name: capFirst(t.name)});
t.attr.values.forEach (
(m) => { enumDef.addMember( {name: m.attr.value , value: `"${m.attr.value}"` as any } ); },
(m) => { enumDef.addMember( {name: m.attr.value.replace('+', '_') , value: `"${m.attr.value}"` as any } ); },
);
if (t.attr.element) {
schemaClass.addProperty({name: lowfirst(t.name), type: capfirst(t.name)});
}
});

const tmp = this.makeSortedFileDefinition(fileDef.classes);
const tmp = this.makeSortedFileDefinition(fileDef.classes, fileDef);
Object.keys(typeAliases).forEach( k => {
fileDef.addTypeAlias({name: k, type: typeAliases[k], isExported: true});
});
Expand Down Expand Up @@ -405,31 +408,31 @@ export class ClassGenerator {
}


private makeSortedFileDefinition(sortedClasses: ClassDefinition[]): FileDefinition {
private makeSortedFileDefinition(sortedClasses: ClassDefinition[], fileDef : FileDefinition): FileDefinition {
// console.log('makeSortedFileDefinition ');
const outFile = createFile({classes: []});

//outFile.addImport({moduleSpecifier: "mod", starImportName: "nspce"});
for ( const ns in this.importMap) {
log('ns ', ns, this.importMap[ns]);
log('addImport: ', ns, this.importMap[ns]);
outFile.addImport({moduleSpecifier: this.importMap[ns], starImportName: ns});
}

let depth = 0;
let max_depth = 1;
log('makeSortedFileDefinition, max_depth ',max_depth);
log('makeSortedFileDefinition, max_depth ', max_depth);
let redundantArrayClasses:string[] = [];
while (depth <= max_depth) {
// console.log('depth ');
sortedClasses.forEach(
(c) => {

const hDepth = this.findHierachyDepth(c, this.fileDef);
const hDepth = this.findHierachyDepth(c, fileDef);

if (hDepth > max_depth) {
max_depth = hDepth;
}
this.log( c.name + '\t' + hDepth);
this.log('--DEPTH:', c.name + '\t' + hDepth);
if (hDepth === depth) {

if (c.name.indexOf(GROUP_PREFIX) === 0) {
Expand Down Expand Up @@ -546,9 +549,11 @@ export class ClassGenerator {
let result = 0;
let superClassName = (c.extendsTypes[0]) ? c.extendsTypes[0].text : '';
while (superClassName) {
//console.log('superClassName1:', superClassName , result);
result++;
c = f.getClass(superClassName);
superClassName = c?.extendsTypes[0]?.text;
//console.log('superClassName2:', superClassName , c, result);
}
return result;
}
Expand Down
1 change: 1 addition & 0 deletions src/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function getFieldType(type: string, defNs: string): string {
double: "number",
int: "number",
integer: "number",
long: "number",
positiveinteger: "number",
nonnegativeinteger: "number",
decimal: "number",
Expand Down

0 comments on commit fa5c6d5

Please sign in to comment.