[TS] Add Obj API (#5788)

* added basic code

* backup work

* got class property to work

* backup progress

* implementented fmt for creating code

* added docs for genFieldUtils

* back up work

* added base helper js func

* added union js code

* added unpackTo and base for pack

* added pack code

* added null check for packing struct list

* passes compile test

* fixed some spacing of generated functions

* added annotations for constructors

* added obj api unpack test

* tested pack to work

* merge branch

* separated js and ts test

* fixed union signature to include string

* fixed generator to support string union

* hardcoded fb builder name

* refactored struct vector creation

* work around createLong

* handle default value in constructor

* update typescript docs

* added notes about import flag

* fixed formatting stuffs

* undo TypescriptTest change

* refactored fmt

* updated generated code

* remove ignoring union_vector for js

* revert changes for .project

* revert changes for package.json

* don't generate js in ts test

* fixed android project file

* removed unused js function

* removed package-lock.json

* adjust createObjList to new signature

* changed regex to callback style

* fixed package.json

* used existing func for generating annotation

* changed ternary to !!

* added return type for lambda

* removed callback style for obj api generator

* fixed js file indentation

* removed unused header

* added tests for string only union

* handle string only union and refactor union conv func

* updated generated ts files

* renamed union conv func

* made js test create files like other languages

* removed union string only handling

* don't allow null in createObjectOffsetList

* updated generated ts code

* changed the line that triggers Windows build errors

* hopefully fix CI error
This commit is contained in:
Khoi Dinh Trinh
2020-04-09 09:53:16 -07:00
committed by GitHub
parent 21cf300f4c
commit 003e164057
12 changed files with 2159 additions and 57 deletions

View File

@@ -13,6 +13,39 @@ export enum Character{
Unused= 6
};
export function unionToCharacter(
type: Character,
accessor: (obj:Attacker|BookReader|Rapunzel|string) => Attacker|BookReader|Rapunzel|string|null
): Attacker|BookReader|Rapunzel|string|null {
switch(Character[type]) {
case 'NONE': return null;
case 'MuLan': return accessor(new Attacker())! as Attacker;
case 'Rapunzel': return accessor(new Rapunzel())! as Rapunzel;
case 'Belle': return accessor(new BookReader())! as BookReader;
case 'BookFan': return accessor(new BookReader())! as BookReader;
case 'Other': return accessor('') as string;
case 'Unused': return accessor('') as string;
default: return null;
}
}
export function unionListToCharacter(
type: Character,
accessor: (index: number, obj:Attacker|BookReader|Rapunzel|string) => Attacker|BookReader|Rapunzel|string|null,
index: number
): Attacker|BookReader|Rapunzel|string|null {
switch(Character[type]) {
case 'NONE': return null;
case 'MuLan': return accessor(index, new Attacker())! as Attacker;
case 'Rapunzel': return accessor(index, new Rapunzel())! as Rapunzel;
case 'Belle': return accessor(index, new BookReader())! as BookReader;
case 'BookFan': return accessor(index, new BookReader())! as BookReader;
case 'Other': return accessor(index, '') as string;
case 'Unused': return accessor(index, '') as string;
default: return null;
}
}
/**
* @constructor
*/
@@ -102,6 +135,42 @@ static createAttacker(builder:flatbuffers.Builder, swordAttackDamage:number):fla
Attacker.addSwordAttackDamage(builder, swordAttackDamage);
return Attacker.endAttacker(builder);
}
/**
* @returns AttackerT
*/
unpack(): AttackerT {
return new AttackerT(
this.swordAttackDamage()
);
};
/**
* @param AttackerT _o
*/
unpackTo(_o: AttackerT): void {
_o.swordAttackDamage = this.swordAttackDamage();
};
}
export class AttackerT {
/**
* @constructor
* @param number swordAttackDamage
*/
constructor(
public swordAttackDamage: number = 0
){};
/**
* @param flatbuffers.Builder builder
* @returns flatbuffers.Offset
*/
pack(builder:flatbuffers.Builder): flatbuffers.Offset {
return Attacker.createAttacker(builder,
this.swordAttackDamage
);
};
}
/**
* @constructor
@@ -154,6 +223,42 @@ static createRapunzel(builder:flatbuffers.Builder, hair_length: number):flatbuff
return builder.offset();
};
/**
* @returns RapunzelT
*/
unpack(): RapunzelT {
return new RapunzelT(
this.hairLength()
);
};
/**
* @param RapunzelT _o
*/
unpackTo(_o: RapunzelT): void {
_o.hairLength = this.hairLength();
};
}
export class RapunzelT {
/**
* @constructor
* @param number hairLength
*/
constructor(
public hairLength: number = 0
){};
/**
* @param flatbuffers.Builder builder
* @returns flatbuffers.Offset
*/
pack(builder:flatbuffers.Builder): flatbuffers.Offset {
return Rapunzel.createRapunzel(builder,
this.hairLength
);
};
}
/**
* @constructor
@@ -206,6 +311,42 @@ static createBookReader(builder:flatbuffers.Builder, books_read: number):flatbuf
return builder.offset();
};
/**
* @returns BookReaderT
*/
unpack(): BookReaderT {
return new BookReaderT(
this.booksRead()
);
};
/**
* @param BookReaderT _o
*/
unpackTo(_o: BookReaderT): void {
_o.booksRead = this.booksRead();
};
}
export class BookReaderT {
/**
* @constructor
* @param number booksRead
*/
constructor(
public booksRead: number = 0
){};
/**
* @param flatbuffers.Builder builder
* @returns flatbuffers.Offset
*/
pack(builder:flatbuffers.Builder): flatbuffers.Offset {
return BookReader.createBookReader(builder,
this.booksRead
);
};
}
/**
* @constructor
@@ -264,9 +405,9 @@ mainCharacterType():Character {
* @param flatbuffers.Table obj
* @returns ?flatbuffers.Table
*/
mainCharacter<T extends flatbuffers.Table>(obj:T):T|null {
mainCharacter<T extends flatbuffers.Table>(obj:T|string):T|string|null {
var offset = this.bb!.__offset(this.bb_pos, 6);
return offset ? this.bb!.__union(obj, this.bb_pos + offset) : null;
return offset ? this.bb!.__union_with_string(obj, this.bb_pos + offset) : null;
};
/**
@@ -299,9 +440,9 @@ charactersTypeArray():Uint8Array|null {
* @param flatbuffers.Table= obj
* @returns ?flatbuffers.Table
*/
characters<T extends flatbuffers.Table>(index: number, obj:T):T|null {
characters<T extends flatbuffers.Table>(index: number, obj:T|string):T|string|null {
var offset = this.bb!.__offset(this.bb_pos, 10);
return offset ? this.bb!.__union(obj, this.bb!.__vector(this.bb_pos + offset) + index * 4) : null;
return offset ? this.bb!.__union_with_string(obj, this.bb!.__vector(this.bb_pos + offset) + index * 4) : null;
};
/**
@@ -426,4 +567,93 @@ static createMovie(builder:flatbuffers.Builder, mainCharacterType:Character, mai
Movie.addCharacters(builder, charactersOffset);
return Movie.endMovie(builder);
}
/**
* @returns MovieT
*/
unpack(): MovieT {
return new MovieT(
this.mainCharacterType(),
(() => {
let temp = unionToCharacter(this.mainCharacterType(), this.mainCharacter.bind(this));
if(temp === null) { return null; }
if(typeof temp === 'string') { return temp; }
return temp.unpack()
})(),
this.bb!.createScalarList(this.charactersType.bind(this), this.charactersTypeLength()),
(() => {
let ret = [];
for(let targetEnumIndex = 0; targetEnumIndex < this.charactersTypeLength(); ++targetEnumIndex) {
let targetEnum = this.charactersType(targetEnumIndex);
if(targetEnum === null || Character[targetEnum!] === 'NONE') { continue; }
let temp = unionListToCharacter(targetEnum, this.characters.bind(this), targetEnumIndex);
if(temp === null) { continue; }
if(typeof temp === 'string') { ret.push(temp); continue; }
ret.push(temp.unpack());
}
return ret;
})()
);
};
/**
* @param MovieT _o
*/
unpackTo(_o: MovieT): void {
_o.mainCharacterType = this.mainCharacterType();
_o.mainCharacter = (() => {
let temp = unionToCharacter(this.mainCharacterType(), this.mainCharacter.bind(this));
if(temp === null) { return null; }
if(typeof temp === 'string') { return temp; }
return temp.unpack()
})();
_o.charactersType = this.bb!.createScalarList(this.charactersType.bind(this), this.charactersTypeLength());
_o.characters = (() => {
let ret = [];
for(let targetEnumIndex = 0; targetEnumIndex < this.charactersTypeLength(); ++targetEnumIndex) {
let targetEnum = this.charactersType(targetEnumIndex);
if(targetEnum === null || Character[targetEnum!] === 'NONE') { continue; }
let temp = unionListToCharacter(targetEnum, this.characters.bind(this), targetEnumIndex);
if(temp === null) { continue; }
if(typeof temp === 'string') { ret.push(temp); continue; }
ret.push(temp.unpack());
}
return ret;
})();
};
}
export class MovieT {
/**
* @constructor
* @param Character mainCharacterType
* @param AttackerT|BookReaderT|RapunzelT|string|null mainCharacter
* @param (Character)[] charactersType
* @param (AttackerT|BookReaderT|RapunzelT|string)[] characters
*/
constructor(
public mainCharacterType: Character = Character.NONE,
public mainCharacter: AttackerT|BookReaderT|RapunzelT|string|null = null,
public charactersType: (Character)[] = [],
public characters: (AttackerT|BookReaderT|RapunzelT|string)[] = []
){};
/**
* @param flatbuffers.Builder builder
* @returns flatbuffers.Offset
*/
pack(builder:flatbuffers.Builder): flatbuffers.Offset {
const mainCharacter = builder.createObjectOffset(this.mainCharacter);
const charactersType = Movie.createCharactersTypeVector(builder, this.charactersType);
const characters = Movie.createCharactersVector(builder, builder.createObjectOffsetList(this.characters));
return Movie.createMovie(builder,
this.mainCharacterType,
mainCharacter,
charactersType,
characters
);
};
}