# struct2ts [![GoDoc](https://godoc.org/github.com/OneOfOne/struct2ts?status.svg)](https://godoc.org/github.com/OneOfOne/struct2ts)
An extremely simple and powerful Go struct to Typescript Class generator.
Inspired by [tkrajina/typescriptify-golang-structs](https://github.com/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:
```go
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:
```ts
// ... 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).
```
## Advanced
### Custom output per model
```golang
type CustomTypescript interface {
RenderCustomTypescript(w io.Writer) (err error)
}
```
If your model implements a ```RenderCustomTypescript(w io.Writer) (err error)``` function it will inject what ever you
write to the writer at the end of the model. struct2ts will handle the first level of indenting for you.
## TODO
* Use [xast](https://github.com/OneOfOne/xast) to skip reflection.
* Support annoymous structs.
* ~~Support ES6.~~
## License
This project is released under the [BSD 3-clause "New" or "Revised" License](https://github.com/golang/go/blob/master/LICENSE).
没有合适的资源?快使用搜索试试~ 我知道了~
struct2ts:从Go结构中生成Typescript类接口
共16个文件
go:9个
sh:2个
ts:1个
5星 · 超过95%的资源 需积分: 50 2 下载量 126 浏览量
2021-02-05
01:36:46
上传
评论
收藏 17KB ZIP 举报
温馨提示
struct2ts 一个非常简单且功能强大的Go结构到Typescript类生成器。 受启发。 安装 go get -u -v github.com/OneOfOne/struct2ts/... 产品特点 如果您不想自己编写生成器,则可以使用相当不错的命令行界面。 自动处理Go int64时间戳<-> Javascript Date 。 自动处理json标签。 选件 还有一个额外的struct标签来控制输出ts ,有效的选项是 -忽略此字段。 date句柄转换time.Time{}.Unix() <-> javascript Date 。 ,no-null仅对结构字段有效,强制创建
资源详情
资源评论
资源推荐
收起资源包目录
struct2ts-master.zip (16个子文件)
struct2ts-master
helpers.ts 1KB
testdata
testmodel1
testmodel1.sh 344B
testmodel1.go 444B
cmd
struct2ts
main.go 6KB
genHelpers.sh 665B
s2ts.go 5KB
struct.go 4KB
field.go 4KB
helpers_gen.go 3KB
.gitignore 86B
s2ts_test.go 4KB
go.sum 1KB
tabscanner.go 587B
tabscanner_test.go 2KB
README.md 5KB
go.mod 356B
共 16 条
- 1
曲奇小朋友
- 粉丝: 17
- 资源: 4575
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于pygame实现的烟花代码
- mcu-printf关于51单片机使用printf函数进行串口调试的方法
- MySQL和数据表操作
- 微信小程序面试题.pdf
- 基于matlab实现电力系统仿真计算软件包,包括潮流计算,最优潮流计算等.rar
- 基于matlab实现电力系统各种故障波形仿真,单相接地故障,两相间短路,两相接地短路,三相短路等.rar
- 基于matlab实现电动汽车动力性,爬坡性,续驶里程等性能仿真.rar
- Python动态烟花代码.pdf
- 基于matlab实现串口发送接收数据 可配置端口,波特率等 发送可选择ASCII方式或HEX方式
- matlab基于BP神经网络手写字母识别(单一).zip代码9
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1