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

Typescript generator #46

Closed
wants to merge 60 commits into from

Conversation

@rg911
Copy link
Member

commented Jul 31, 2019

Typescript Generator

Two typescript generators created in this PR:

  • Typescript: Strong typed typescript generator.
  • Typescript-js-base: Based off js generator with refactored ts codes. Loose typed.

Adding two generators is for comparing and reviewing purposes. We probably only need one for ts. The Typescript-js-base has got the same structure as js generator but is not strong typed. Please review and decide if we should keep both or one of them.

New to py anyway, code probably need a good review and refactoring. Feel free to contribute please.

some examples generated code below

Typescript Generator

import { GeneratorUtils } from './GeneratorUtils';
import { AccountLinkActionDto } from './AccountLinkActionDto';
import { EntityTypeDto } from './EntityTypeDto';
import { SignatureDto } from './SignatureDto';
import { AccountLinkTransactionBodyBuilder } from './AccountLinkTransactionBodyBuilder';
import { TimestampDto } from './TimestampDto';
import { KeyDto } from './KeyDto';
import { AmountDto } from './AmountDto';
import { TransactionBuilder } from './TransactionBuilder';

/** Binary layout for a non-embedded account link transaction. */
export class AccountLinkTransactionBuilder extends TransactionBuilder {
    /** Account link transaction body. */
    accountLinkTransactionBody: AccountLinkTransactionBodyBuilder;

    /**
     * Constructor.
     *
     * @param signature Entity signature.
     * @param signer Entity signer's public key.
     * @param version Entity version.
     * @param type Entity type.
     * @param fee Transaction fee.
     * @param deadline Transaction deadline.
     * @param remoteAccountKey Remote account key.
     * @param linkAction Account link action.
     */
    public constructor(signature: SignatureDto,  signer: KeyDto,  version: number,  type: EntityTypeDto,  fee: AmountDto,  deadline: TimestampDto,  remoteAccountKey: KeyDto,  linkAction: AccountLinkActionDto) {
        super(signature, signer, version, type, fee, deadline);
        this.accountLinkTransactionBody = AccountLinkTransactionBodyBuilder.create(remoteAccountKey, linkAction);
    }

    /**
     * Gets remote account key.
     *
     * @return Remote account key.
     */
    public getRemoteAccountKey(): KeyDto {
        return this.accountLinkTransactionBody.getRemoteAccountKey();
    }

    /**
     * Gets account link action.
     *
     * @return Account link action.
     */
    public getLinkAction(): AccountLinkActionDto {
        return this.accountLinkTransactionBody.getLinkAction();
    }

    /**
     * Gets the size of the object.
     *
     * @return Size in bytes.
     */
    public getSize(): number {
        let size: number = super.getSize();
        size += this.accountLinkTransactionBody.getSize();
        return size;
    }

    /**
     * Creates an instance of AccountLinkTransactionBuilder from binary payload.
     *
     * @param payload Byte payload to use to serialize the object.
     * @return Instance of AccountLinkTransactionBuilder.
     */
    public static loadFromBinary(payload: Uint8Array): AccountLinkTransactionBuilder {
        let byteArray = Array.from(payload);
        const superObject = TransactionBuilder.loadFromBinary(Uint8Array.from(byteArray));
        byteArray.splice(0, superObject.getSize());
        const accountLinkTransactionBody = AccountLinkTransactionBodyBuilder.loadFromBinary(Uint8Array.from(byteArray));
        byteArray.splice(0, accountLinkTransactionBody.getSize());
        return new AccountLinkTransactionBuilder(superObject.signature, superObject.signer, superObject.version, superObject.type, superObject.fee, superObject.deadline, accountLinkTransactionBody.remoteAccountKey, accountLinkTransactionBody.linkAction);
    }

    /**
     * Serializes an object to bytes.
     *
     * @return Serialized bytes.
     */
    public serialize(): Uint8Array {
        let newArray = Uint8Array.from([]);
        const superBytes = super.serialize();
        newArray = GeneratorUtils.concatTypedArrays(newArray, superBytes);
        const accountLinkTransactionBodyBytes = this.accountLinkTransactionBody.serialize();
        newArray = GeneratorUtils.concatTypedArrays(newArray, accountLinkTransactionBodyBytes);
        return newArray;
    }
}

Typescript-js-base Generator

/*** File automatically generated by Catbuffer ***/

const { concatTypedArrays, fitByteArray, Uint8ArrayConsumableBuffer, bufferToUint, uintToBuffer } = require('./JavaScriptUtils.js')

class AccountLinkTransactionBuffer {
    getSize() {
        return this.size
    }
    setSize(size) {
        this.size = size
    }
    getSignature() {
        return this.signature
    }
    setSignature(signature) {
        this.signature = signature
    }
    getSigner() {
        return this.signer
    }
    setSigner(signer) {
        this.signer = signer
    }
    getVersion() {
        return this.version
    }
    setVersion(version) {
        this.version = version
    }
    getType() {
        return this.type
    }
    setType(type) {
        this.type = type
    }
    getFee() {
        return this.fee
    }
    setFee(fee) {
        this.fee = fee
    }
    getDeadline() {
        return this.deadline
    }
    setDeadline(deadline) {
        this.deadline = deadline
    }
    getRemoteaccountkey() {
        return this.remoteAccountKey
    }
    setRemoteaccountkey(remoteAccountKey) {
        this.remoteAccountKey = remoteAccountKey
    }
    getLinkaction() {
        return this.linkAction
    }
    setLinkaction(linkAction) {
        this.linkAction = linkAction
    }
    static loadFromBinary(consumableBuffer) {
        var object = new AccountLinkTransactionBuffer()
        var size = consumableBuffer.getBytes(4)
        object.size = size
        var signature = consumableBuffer.getBytes(64)
        object.signature = signature
        var signer = consumableBuffer.getBytes(32)
        object.signer = signer
        var version = consumableBuffer.getBytes(2)
        object.version = version
        var type = consumableBuffer.getBytes(2)
        object.type = type
        var fee = consumableBuffer.getBytes(8)
        object.fee = fee
        var deadline = consumableBuffer.getBytes(8)
        object.deadline = deadline
        var remoteAccountKey = consumableBuffer.getBytes(32)
        object.remoteAccountKey = remoteAccountKey
        var linkAction = consumableBuffer.getBytes(1)
        object.linkAction = linkAction
        return object
    }
    serialize() {
        var newArray = new Uint8Array()
        var fitArraysize = fitByteArray(this.size, 4)
        newArray = concatTypedArrays(newArray, fitArraysize)
        var fitArraysignature = fitByteArray(this.signature, 64)
        newArray = concatTypedArrays(newArray, fitArraysignature)
        var fitArraysigner = fitByteArray(this.signer, 32)
        newArray = concatTypedArrays(newArray, fitArraysigner)
        var fitArrayversion = fitByteArray(this.version, 2)
        newArray = concatTypedArrays(newArray, fitArrayversion)
        var fitArraytype = fitByteArray(this.type, 2)
        newArray = concatTypedArrays(newArray, fitArraytype)
        var fitArrayfee = fitByteArray(this.fee, 8)
        newArray = concatTypedArrays(newArray, fitArrayfee)
        var fitArraydeadline = fitByteArray(this.deadline, 8)
        newArray = concatTypedArrays(newArray, fitArraydeadline)
        var fitArrayremoteAccountKey = fitByteArray(this.remoteAccountKey, 32)
        newArray = concatTypedArrays(newArray, fitArrayremoteAccountKey)
        var fitArraylinkAction = fitByteArray(this.linkAction, 1)
        newArray = concatTypedArrays(newArray, fitArraylinkAction)
        return newArray
    }
}

module.exports = {
    AccountLinkTransactionBuffer,
};

Jaguar0625 and others added some commits Jun 15, 2018

add basic union semantics assuming enum discriminator
e.g.
     foo = bar *if* baz *equals* something
minor housekeeping
1. update readme

2. change -d to -o

3. move hints file into generator and change to yaml
cow schema updates
* Add schemas for multiple transactions:
 + register namespace and aliases
 + mosaic definition, mosaic supply
 + modify multisig account
 + aggregate (won't work right now) + cosignatures
* handling in cpp builder
regenerate all builders (ex aggregate) from catbuffer using 'cpp_buil…
…der'

- add 'cpp_builder' generation to travisci
rename and reorganize cats files
- rename tx cats files to match tx names

- group cats files by plugin

- allow underscores in property names

- add schema root directory option
Updated README.md
Added requirements.txt file
add HEADER.inc to repo
- issues: #16

@rg911 rg911 requested review from Jaguar0625, Vektrat, evias and Wayonb Jul 31, 2019

rg911 added some commits Jul 31, 2019

@evias

This comment has been minimized.

Copy link

commented Aug 2, 2019

I would go with the typed TS generator - type hints in TS also serve as an input validation (e.g.: Deadline object-only vs. any and permit input of negative number, needs validation..).

@Jaguar0625 Jaguar0625 force-pushed the nemtech:master branch 2 times, most recently from 5d6feb7 to 2ff787f Aug 2, 2019

@Wayonb

This comment has been minimized.

Copy link
Member

commented Aug 2, 2019

@rg911 Can you rebase since your pr got moved to a new repro.

@rg911

This comment has been minimized.

Copy link
Member Author

commented Aug 5, 2019

PR will be recreated

@rg911 rg911 closed this Aug 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.