Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# ide
.idea

yarn.lock
# libraries
node_modules

Expand Down
15 changes: 0 additions & 15 deletions index.ts

This file was deleted.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"name": "xml-decorators",
"version": "1.0.6",
"description": "Decorators for xml serialization",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"pretest": "npm run build",
"test": "mocha 'test/specs/**/*.js'"
"build": "tsc --build tsconfig.json",
"test": "ts-mocha -p tsconfig.json 'test/specs/**/*.ts'"
},
"repository": {
"type": "git",
Expand All @@ -22,20 +21,22 @@
"reflect-metadata": "^0.1.8"
},
"dependencies": {
"@types/bluebird": "3.5.22",
"@types/lodash": "~4.14.42",
"bluebird": "3.5.1",
"es6-shim": "0.35.3",
"js2xmlparser": "3.0.0",
"lodash": "4.17.10"
"lodash.groupby": "4.6.0",
"lodash.merge": "4.6.2",
"object.entries": "1.1.3"
},
"devDependencies": {
"@types/bluebird": "3.5.33",
"@types/chai": "4.1.4",
"@types/mocha": "5.2.5",
"chai": "4.1.2",
"mocha": "5.2.0",
"reflect-metadata": "^0.1.8",
"ts-mocha": "8.0.0",
"tslint": "5.11.0",
"typescript": "2.9.2"
"typescript": "^3.9.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import 'reflect-metadata';
import {XMLAttribute as XMLAttributeModel} from "../models/XMLAttribute";
import {IXMLAttributeOptions} from "../interfaces/IXMLAttributeOptions";
import { XMLAttribute as XMLAttributeModel } from '../models/XMLAttribute';
import { IXMLAttributeOptions } from '../interfaces/IXMLAttributeOptions';

export function XMLAttribute(target: any, key: string, descriptor?: TypedPropertyDescriptor<any>): void;
export function XMLAttribute(options: IXMLAttributeOptions): Function;
export function XMLAttribute(...args: any[]): void|Function {

export function XMLAttribute(...args: any[]): void | Function {
if (args.length === 1) {

return (target: any, key: string, descriptor?: TypedPropertyDescriptor<any>) => {

return XMLAttributeModel.annotate(target, key, args[0], descriptor);
};
} else if (args.length === 4) {
return XMLAttributeModel.annotate(args[0], args[1], args[2], args[3]);
}
return XMLAttributeModel.annotate(args[0], args[1], void 0, args[2]);
}
9 changes: 3 additions & 6 deletions lib/annotations/XMLChild.ts → src/annotations/XMLChild.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import 'reflect-metadata';
import {XMLChild as XMLChildModel} from "../models/XMLChild";
import {IXMLChildOptions} from "../interfaces/IXMLChildOptions";
import { XMLChild as XMLChildModel } from '../models/XMLChild';
import { IXMLChildOptions } from '../interfaces/IXMLChildOptions';

export function XMLChild(target: any, key: string, descriptor?: TypedPropertyDescriptor<any>): void;
export function XMLChild(options: IXMLChildOptions): Function;
export function XMLChild(...args: any[]): void|Function {

export function XMLChild(...args: any[]): void | Function {
if (args.length === 1) {

return (target: any, key: string, descriptor?: TypedPropertyDescriptor<any>) => {

return XMLChildModel.annotate(target, key, args[0], descriptor);
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'reflect-metadata';
import {XMLElement as XMLElementModel} from "../models/XMLElement";
import {IXMLElementOptions} from "../interfaces/IXMLElementOptions";
import { XMLElement as XMLElementModel } from '../models/XMLElement';
import { IXMLElementOptions } from '../interfaces/IXMLElementOptions';

export function XMLElement(options: IXMLElementOptions): Function {

return (target: any) => {

return XMLElementModel.annotate(target.prototype, options);
};
}
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export {DEFAULT_ATTRIBUTE_PROPERTY, ns} from './utils';

export {XMLAttribute} from './annotations/XMLAttribute';
export {XMLChild} from './annotations/XMLChild';
export {XMLElement} from './annotations/XMLElement';

export {XMLElement as xml} from './models/XMLElement';
export {XMLAttribute as xmlAttribute} from './models/XMLAttribute';
export {XMLChild as xmlChild} from './models/XMLChild';

export {ISchemaOptions} from './interfaces/ISchemaOptions';
export {IXMLAttributeOptions} from './interfaces/IXMLAttributeOptions';
export {IXMLChildOptions} from './interfaces/IXMLChildOptions';
export {IXMLElementOptions} from './interfaces/IXMLElementOptions';

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IXMLAttributeOptions} from "./IXMLAttributeOptions";
import { IXMLAttributeOptions } from './IXMLAttributeOptions';

export interface ICustomXMLAttributeOptions extends IXMLAttributeOptions {

name: string;
getter?: (entity?: any) => any;
value?: any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IXMLChildOptions} from "./IXMLChildOptions";
import { IXMLChildOptions } from './IXMLChildOptions';

export interface ICustomXMLChildOptions extends IXMLChildOptions {

name: string;
getter?: (entity?: any) => any;
value?: any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IXMLAttributeOptions} from "./IXMLAttributeOptions";
import { IXMLAttributeOptions } from './IXMLAttributeOptions';

export interface IFullXMLAttributeOptions extends IXMLAttributeOptions {

name: string;
getter: (entity: any) => any;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IXMLChildOptions} from "./IXMLChildOptions";
import { IXMLChildOptions } from './IXMLChildOptions';

export interface IFullXMLChildOptions extends IXMLChildOptions {

name: string;
getter: (entity: any) => any;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

export interface ISchemaOptions {

attrContainerName?: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

export interface IXMLAttributeOptions {

name?: string;
required?: boolean;
namespace?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

export interface IXMLChildOptions {

name?: string;
stripPluralS?: boolean;
namespace?: string;
nestedNamespace?: string;
implicitStructure?: string;
virtual?: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

export interface IXMLElementOptions {

root?: string;
}
65 changes: 35 additions & 30 deletions lib/models/XMLAttribute.ts → src/models/XMLAttribute.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import {XMLElement} from "./XMLElement";
import {IXMLAttributeOptions} from "../interfaces/IXMLAttributeOptions";
import {IFullXMLAttributeOptions} from "../interfaces/IFullXMLAttributeOptions";
import {ICustomXMLAttributeOptions} from "../interfaces/ICustomXMLAttributeOptions";
import {createCustomGetter} from "../utils";
import { XMLElement } from './XMLElement';
import { IXMLAttributeOptions } from '../interfaces/IXMLAttributeOptions';
import { IFullXMLAttributeOptions } from '../interfaces/IFullXMLAttributeOptions';
import { ICustomXMLAttributeOptions } from '../interfaces/ICustomXMLAttributeOptions';
import { createCustomGetter } from '../utils';

export class XMLAttribute {

private name: string;
getName() {
return this.name;
}

static annotate(target: any,
key: string,
options: IXMLAttributeOptions = {},
descriptor?: TypedPropertyDescriptor<any>): void {

getOptions(){
return this.options
}
static annotate(
target: any,
key: string,
options: IXMLAttributeOptions = {},
descriptor?: TypedPropertyDescriptor<any>,
): void {
const element = XMLElement.getOrCreateIfNotExists(target);
const fullOptions = Object.assign({
getter(entity: any): any {
if (descriptor && descriptor.get) {
return descriptor.get.call(entity);
}
return entity[key];
}
}, options);
const fullOptions = Object.assign(
{
getter(entity: any): any {
if (descriptor && descriptor.get) {
return descriptor.get.call(entity);
}
return entity[key];
},
},
options,
);

fullOptions.name = options.name || key;

element.addAttribute(new XMLAttribute(fullOptions as IFullXMLAttributeOptions));
}

Expand All @@ -33,35 +41,32 @@ export class XMLAttribute {
const hasValue = options.value !== void 0;

if ((hasGetter && hasValue) || (!hasGetter && !hasValue)) {

throw new Error(`Either a getter or a value has to be defined for attribute "${options.name}".`);
}

const fullOptions = Object.assign({
getter: createCustomGetter(options),
}, options);

const fullOptions = Object.assign(
{
getter: createCustomGetter(options),
},
options,
);

return new XMLAttribute(fullOptions);
}

setSchema(target: any, entity: any): void {

const value = this.options.getter.call(null, entity);

if (value !== void 0) {
target[this.name] = value;
} else if (this.options.required) {

throw new Error(`Attribute ${this.name} is required, but empty.`);
}
}

private constructor(private options: IFullXMLAttributeOptions) {

this.name = options.name;

if (options.namespace) {

this.name = options.namespace + ':' + this.name;
}
}
Expand Down
Loading