1.4.1 • Published 2 years ago

gen-go-type v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

gen-go-type

Build Status npm version Node.js Version

Generate a formatted Go type string in JavaScript.

Installation

npm i gen-go-type

Usage

import { genGoType } from 'gen-go-type';

const goCode = genGoType('struct', 'T', [
  { name: 'A', type: 'T', tag: '"_____"' },
  { name: 'A_______B', type: 'T2' },
  { name: 'C', type: 'T______3', tag: '""' },
]);

/*
type T struct {
    A         T        "_____"
    A_______B T2
    C         T______3 ""
}
 */

Use Options.ctorFunc to generate a constructor:

import { genGoType } from 'gen-go-type';

const goCode = genGoType(
  'struct',
  'T',
  [
    { name: 'A', type: 'T', tag: '"_____"' },
    { name: 'A_______B', type: 'T2' },
    { name: 'C', type: 'T______3', tag: '""' },
  ],
  { ctorFunc: true },
);

/*
type T struct {
    A         T        "_____"
    A_______B T2
    C         T______3 ""
}

func NewT(a T, a_______A T2, b T______T) *T {
    return &T{
        A: a,
        A_______A: a_______A,
        B: b,
    }
}
 */

To generate a constructor returning a value type instead of a pointer, set Options.returnValueInCtor to true:

import { genGoType } from 'gen-go-type';

const goCode = genGoType(
  'struct',
  'T',
  [
    { name: 'A', type: 'T', tag: '"_____"' },
    { name: 'A_______B', type: 'T2' },
    { name: 'C', type: 'T______3', tag: '""' },
  ],
  { ctorFunc: true, returnValueInCtor: true },
);

/*
type T struct {
    A         T        "_____"
    A_______B T2
    C         T______3 ""
}

func NewT(a T, a_______A T2, b T______T) T {
    return T{
        A: a,
        A_______A: a_______A,
        B: b,
    }
}
 */

Constructor param names can be customized via Member.paramName, for example:

import { genGoType } from 'gen-go-type';

const goCode = genGoType(
  'struct',
  'T',
  [
    { name: 'ID', type: 'string', paramName: 'id' },
    { name: 'Type', type: 'string' },
  ],
  { ctorFunc: true },
);

/*
type T struct {
    ID   string
    Type string
}

func NewT(id string, type string) *T {
    return &T{
        ID: id,
        Type: type,
    }
}
 */
1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago