mirror of
https://github.com/NaN-tic/ngx-tryton-json.git
synced 2023-12-14 02:13:00 +01:00
initial commit
This commit is contained in:
commit
8d8f39908f
5 changed files with 206 additions and 0 deletions
41
encode-json-read.spec.ts
Normal file
41
encode-json-read.spec.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import {} from 'jasmine';
|
||||
|
||||
import { EncodeJSONRead } from './encode-json-read'
|
||||
|
||||
let json_constructor = null;
|
||||
|
||||
describe('Encode JSON read unit test', () => {
|
||||
beforeEach(() => {
|
||||
json_constructor = new EncodeJSONRead();
|
||||
});
|
||||
|
||||
it('should create the inventory page', () => {
|
||||
let result = json_constructor.createDomain('id', '=', 2);
|
||||
expect(typeof result).toBe('string');
|
||||
expect(result).toEqual("('id', '=', 2)");
|
||||
});
|
||||
it('should add a new node', () => {
|
||||
let domain = "[" + json_constructor.createDomain('id', '=', 2) + "]";
|
||||
json_constructor.addNode('party.party', domain,
|
||||
["name", "address"]);
|
||||
|
||||
expect(json_constructor.root['party.party']).toEqual(
|
||||
['[(\'id\', \'=\', 2)]', ["name", "address"], 0, '']);
|
||||
|
||||
expect(typeof json_constructor.root['party.party']).toBe('object');
|
||||
|
||||
expect(json_constructor.root['party.party'].length).toEqual(4);
|
||||
|
||||
expect(json_constructor.root['party.party'][1]).toEqual(["name", "address"])
|
||||
|
||||
expect(json_constructor.root['party.party'][2]).toEqual(0);
|
||||
})
|
||||
it('should create a JSON', () => {
|
||||
let domain = "[" + json_constructor.createDomain('id', '=', 2) + "]";
|
||||
json_constructor.addNode('party.party', domain, ["name", "address"]);
|
||||
let result = json_constructor.createJson();
|
||||
|
||||
expect(typeof result).toBe('string');
|
||||
expect(result).toEqual("[{\"party.party\":[\"[(\'id\', \'=\', 2)]\",[\"name\",\"address\"],0,\"\"]}]")
|
||||
})
|
||||
});
|
58
encode-json-read.ts
Normal file
58
encode-json-read.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
/**
|
||||
* Encodes a JSON
|
||||
* EncodeJSONRead will encode a JSON to make a search request
|
||||
*
|
||||
* DO NOT MODIFIY THIS FILE, EXTEND THE CLASS INSTEAD
|
||||
*/
|
||||
|
||||
import { RootSend, Send } from './json-interface';
|
||||
|
||||
export class EncodeJSONRead {
|
||||
|
||||
root: Send;
|
||||
root_send: RootSend;
|
||||
|
||||
constructor(){
|
||||
console.log("Initialaizing JSON constructor");
|
||||
this.root_send = {
|
||||
root: []
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Adds a new node to the JSON object
|
||||
* @param {string} method Name of the target method
|
||||
* @param {string} domain Domain of the method
|
||||
* @param {Array<string>} fields Fields to get
|
||||
* @param {number} offset Sets where to start gathering data after getting it, the default value no offset
|
||||
* @param {any} limit Limits the number of data returned, default None
|
||||
*/
|
||||
addNode(method: string, domain: any, fields: Array<string>,
|
||||
offset = 0, limit:any = ''){
|
||||
console.log("Adding node for method, domain, fields",
|
||||
method, domain, fields)
|
||||
this.root = {}
|
||||
this.root[method] = [domain, fields, offset, limit];
|
||||
this.root_send.root.push(this.root);
|
||||
}
|
||||
/**
|
||||
* Creates a new domain
|
||||
* @param {string} field Name of the field
|
||||
* @param {string} operator Operator: =, >, <, in, like...
|
||||
* @param {any} value Value to compare
|
||||
* @param {string} related Used for reference fields search
|
||||
* @returns {string} String contaning the domain withour brackets
|
||||
*/
|
||||
createDomain(field: string, operator: string, value: any, related?: string){
|
||||
if (related)
|
||||
return [field, operator, value, related]
|
||||
return [field, operator, value]
|
||||
}
|
||||
/**
|
||||
* Creates the JSON object and returns it
|
||||
* @returns {JSON} JSON created with the nodes
|
||||
*/
|
||||
createJson(){
|
||||
console.log("Generating JSON")
|
||||
return JSON.stringify(this.root_send.root)
|
||||
}
|
||||
}
|
42
encode-json-write.ts
Normal file
42
encode-json-write.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Encodes a JSON
|
||||
* EncodeJSONWrite will encode a JSON to make a save or write request
|
||||
*
|
||||
* DO NOT MODIFIY THIS FILE, EXTEND THE CLASS INSTEAD
|
||||
*/
|
||||
|
||||
import { Write } from './json-interface';
|
||||
|
||||
export class EncodeJSONWrite {
|
||||
|
||||
write: Write;
|
||||
constructor(){
|
||||
console.log("Initialaizing JSON constructor");
|
||||
this.write = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a node to the JSON object
|
||||
* @param {string} method Name of the target method
|
||||
* @param {Object} values Values to create or update
|
||||
*/
|
||||
public addNode(method: string, values: any ){
|
||||
console.log("Adding node for method, id, values",
|
||||
method, values);
|
||||
|
||||
if (this.write[method] !== undefined)
|
||||
this.write[method].push(values);
|
||||
else
|
||||
this.write[method] = [values]
|
||||
console.log("Current state of the node", this.write)
|
||||
}
|
||||
/**
|
||||
* Creates the JSON object and returns it
|
||||
* @returns JSON object
|
||||
*/
|
||||
public createJSON(){
|
||||
console.log("Generating JSON");
|
||||
console.log(JSON.stringify(this.write))
|
||||
return JSON.stringify(this.write);
|
||||
}
|
||||
}
|
40
encode-json-wrtie.spec.ts
Normal file
40
encode-json-wrtie.spec.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import {} from 'jasmine';
|
||||
|
||||
import { EncodeJSONWrite } from './encode-json-write'
|
||||
|
||||
let json_constructor = null;
|
||||
let method = "party.party";
|
||||
|
||||
describe('Encode JSON read unit test', () => {
|
||||
beforeEach(() => {
|
||||
json_constructor = new EncodeJSONWrite();
|
||||
});
|
||||
|
||||
it('Should add a new node to write', () => {
|
||||
let id = -1;
|
||||
let val = {
|
||||
name: 'Test write'
|
||||
};
|
||||
let values = [id, val]
|
||||
json_constructor.addNode(method, values);
|
||||
expect(typeof json_constructor.write).toBe('object');
|
||||
})
|
||||
it('Should create a JSON', () => {
|
||||
let id = -1;
|
||||
let val = {
|
||||
name: 'Test write'
|
||||
};
|
||||
let values = [id, val]
|
||||
json_constructor.addNode(method, values);
|
||||
id = 1;
|
||||
val = {
|
||||
name: 'Test write w'
|
||||
};
|
||||
values = [id, val]
|
||||
json_constructor.addNode(method, values);
|
||||
|
||||
let result = json_constructor.createJSON();
|
||||
expect(typeof result).toBe('string')
|
||||
expect(result).toEqual('{"party.party":[[-1,{"name":"Test write"}],[1,{"name":"Test write w"}]]}')
|
||||
})
|
||||
});
|
25
json-interface.ts
Normal file
25
json-interface.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* Creates the interfaces to create the JSON objects
|
||||
*
|
||||
* DO NOT MODIFY THIS FILE, EXTEND THE INTERFACES INSTEAD
|
||||
*/
|
||||
|
||||
/*
|
||||
Storage of the JSON
|
||||
*/
|
||||
export interface RootSend {
|
||||
root: Array<Send>;
|
||||
}
|
||||
/*
|
||||
Temportary storage of a node
|
||||
*/
|
||||
export interface Send {
|
||||
[method: string]: any[];
|
||||
}
|
||||
|
||||
/*
|
||||
Storage for the write JSON
|
||||
*/
|
||||
export interface Write {
|
||||
[method:string]: any[];
|
||||
}
|
Loading…
Reference in a new issue