struct2ts

Generate Typescript classes/interfaces out of Go structs

Github星跟踪图

struct2ts GoDoc

An extremely simple and powerful Go struct to Typescript Class generator.

Inspired by tkrajina/typescriptify-golang-structs.

Install

go get -u -v github.com/OneOfOne/struct2ts/...

Features

  • Fairly decent command line interface if you don't wanna write a generator yourself.
  • Automatically handles Go int64 timestamps <-> Javascript Date.
  • Automatically handles json tags.

Options

There's an extra struct tag to control the output, ts, valid options are

  • - omit this field.
  • date handle converting time.Time{}.Unix() <-> javascript Date.
  • ,no-null only valid for struct fields, forces creating a new class rather than using null in TS.
  • ,null allows any field type to be null.

Example

  • Input:
type OtherStruct struct {
	T time.Time `json:"t,omitempty"`
}

type ComplexStruct struct {
	S           string       `json:"s,omitempty"`
	I           int          `json:"i,omitempty"`
	F           float64      `json:"f,omitempty"`
	TS          *int64       `json:"ts,omitempty" ts:"date,null"`
	T           time.Time    `json:"t,omitempty"` // automatically handled
	NullOther   *OtherStruct `json:"o,omitempty"`
	NoNullOther *OtherStruct `json:"nno,omitempty" ts:",no-null"`
}
  • Output:
// ... helpers...
// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructOtherStruct
class ComplexStructOtherStruct {
	t: Date;

	constructor(data?: any) {
		const d: any = (data && typeof data === 'object') ? ToObject(data) : {};
		this.t = ('t' in d) ? ParseDate(d.t) : new Date();
	}

	toObject(): any {
		const cfg: any = {};
		cfg.t = 'string';
		return ToObject(this, cfg);
	}
}

// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStruct
class ComplexStruct {
	s: string;
	i: number;
	f: number;
	ts: Date, null;
	t: Date;
	o: ComplexStructOtherStruct, null;
	nno: ComplexStructOtherStruct;

	constructor(data?: any) {
		const d: any = (data && typeof data === 'object') ? ToObject(data) : {};
		this.s = ('s' in d) ? d.s as string : '';
		this.i = ('i' in d) ? d.i as number : 0;
		this.f = ('f' in d) ? d.f as number : 0;
		this.ts = ('ts' in d) ? ParseDate(d.ts) : null;
		this.t = ('t' in d) ? ParseDate(d.t) : new Date();
		this.o = ('o' in d) ? new ComplexStructOtherStruct(d.o) : null;
		this.nno = new ComplexStructOtherStruct(d.nno);
	}

	toObject(): any {
		const cfg: any = {};
		cfg.i = 'number';
		cfg.f = 'number';
		cfg.t = 'string';
		return ToObject(this, cfg);
	}
}
// ...exports...

Command Line Usage

➤ struct2ts -h
usage: struct2ts [<flags>] [<pkg.struct>...]

Flags:
	-h, --help                  Show context-sensitive help (also try --help-long
								and --help-man).
		--indent="\t"           Output indentation.
	-m, --mark-optional-fields  Add `?` to fields with omitempty.
	-6, --es6                   generate es6 code
	-C, --no-ctor               Don't generate a ctor.
	-T, --no-toObject           Don't generate a Class.toObject() method.
	-E, --no-exports            Don't automatically export the generated types.
	-D, --no-date               Don't automatically handle time.Unix () <-> JS
								Date().
	-H, --no-helpers            Don't output the helpers.
	-N, --no-default-values     Don't assign default/zero values in the ctor.
	-i, --interface             Only generate an interface (disables all the other
								options).
	-s, --src-only              Only output the Go code (helpful if you want to
								edit it yourself).
	-p, --package-name="main"   the package name to use if --src-only is set.
	-k, --keep-temp             Keep the generated Go file, ignored if --src-only
								is set.
	-o, --out="-"               Write the output to a file instead of stdout.
	-V, --version               Show application version.

Args:
	[<pkg.struct>]  List of structs to convert (github.com/you/auth/users.User,
					users.User or users.User:AliasUser).

TODO

  • Use xast to skip reflection.
  • Support annoymous structs.
  • Support ES6.

License

This project is released under the BSD 3-clause "New" or "Revised" License.

主要指标

概览
名称与所有者OneOfOne/struct2ts
主编程语言Go
编程语言Go (语言数: 3)
平台
许可证
所有者活动
创建于2018-05-12 04:07:48
推送于2021-08-07 21:44:38
最后一次提交2021-08-07 16:44:38
发布数7
最新版本名称v1.0.6 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数143
关注者数6
派生数15
提交数58
已启用问题?
问题数25
打开的问题数7
拉请求数7
打开的拉请求数0
关闭的拉请求数3
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?