gen-go-type
v1.4.1
Published
Generate a formatted Go type string in JavaScript
Downloads
7
Readme
gen-go-type
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,
}
}
*/