Skip to content

Commit

Permalink
Make XML Schema better
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Jul 26, 2024
1 parent 267d8ef commit 79f215e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.15.1",
"version": "0.15.2",
"scripts": {
"dev": "npm run wasm && vite dev",
"wasm": "wasm-pack build ./selenite-commons-rs --target web",
Expand Down
44 changes: 23 additions & 21 deletions src/lib/utils/xsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export type ComplexType = {
doc?: string;
/** Attributes of the complex type. */
attrs: Attribute[];
children?: string[];
attributes: Map<string, Attribute>;
children: string[];
childTypes: string[];
};

/**
Expand All @@ -64,35 +66,27 @@ export type XMLTypeName = string;
*/
export class XmlSchema {
/** Simple types of the xml schema. */
simpleTypes: SimpleType[] = [];
simpleTypes: Map<string, SimpleType> = new Map();
/** Complex types of the xml schema. */
complexTypes: ComplexType[] = [];
complexTypes: Map<string, ComplexType> = new Map();

/** Map which associates the names of simple XML types to their definition. */
get simpleTypeMap(): Map<XMLTypeName, SimpleType> {
const res = new Map<XMLTypeName, SimpleType>();
for (const type of this.simpleTypes) {
res.set(type.name, type);
}
return res;
return this.simpleTypes;
}

/** Map which associates the names of complex XML types to their definitions. */
get typeMap(): Map<XMLTypeName, ComplexType> {
const res = new Map<string, ComplexType>();
for (const type of this.complexTypes) {
res.set(type.name, type);
}
return res;
return this.complexTypes;
}

/** Map which associates the names of complex XML types to their parents' names. */
get parentsMap(): Map<XMLTypeName, XMLTypeName[]> {
const res = new Map<string, string[]>();
for (const { name } of this.complexTypes) {
for (const { name } of this.complexTypes.values()) {
res.set(name, []);
}
for (const { name: parentName, children } of this.complexTypes) {
for (const { name: parentName, children } of this.complexTypes.values()) {
if (!children) continue;
for (const c of children) {
res.get(c)!.push(parentName);
Expand Down Expand Up @@ -178,7 +172,7 @@ export async function parseXsd(xsd: string): Promise<XmlSchema | undefined> {
const res: XmlSchema = new XmlSchema();

for (const { name, doc, options, pattern } of schema.simple_types) {
res.simpleTypes.push({
res.simpleTypes.set(name, {
name,
doc,
pattern,
Expand All @@ -187,9 +181,7 @@ export async function parseXsd(xsd: string): Promise<XmlSchema | undefined> {
}

for (const { name, children, fields, doc } of schema.complex_types) {
res.complexTypes.push({
name,
attrs: fields.map(({ name, doc, type_name: type, required, default: default_ }) => {
const attrs = fields.map(({ name, doc, type_name: type, required, default: default_ }) => {
if (default_) {
try {
default_ = JSON.parse(default_);
Expand All @@ -202,9 +194,19 @@ export async function parseXsd(xsd: string): Promise<XmlSchema | undefined> {
default: default_,
doc
};
}),
});
res.complexTypes.set(name, {
name,
get attrs() {
return Object.values(this.attributes) as Attribute[];
},
attributes: new Map(attrs.map((a) => [a.name, a])),
get childTypes() {
return this.children;
},
doc,
children
children: children ?? [],

});
}
schema.free();
Expand Down

0 comments on commit 79f215e

Please sign in to comment.