Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generate Typescript classes/interfaces out of Go structs

NotificationsYou must be signed in to change notification settings

OneOfOne/struct2ts

Repository files navigation

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

Inspired bytkrajina/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 Goint64 timestamps<-> JavascriptDate.
  • Automatically handles json tags.

Options

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

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

Example

  • Input:
typeOtherStructstruct {T time.Time`json:"t,omitempty"`}typeComplexStructstruct {Sstring`json:"s,omitempty"`Iint`json:"i,omitempty"`Ffloat64`json:"f,omitempty"`TS*int64`json:"ts,omitempty" ts:"date,null"`T           time.Time`json:"t,omitempty"`// automatically handledNullOther*OtherStruct`json:"o,omitempty"`NoNullOther*OtherStruct`json:"nno,omitempty" ts:",no-null"`}
  • Output:
// ... helpers...// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructOtherStructclassComplexStructOtherStruct{t:Date;constructor(data?:any){constd:any=(data&&typeofdata==='object') ?ToObject(data) :{};this.t=('t'ind) ?ParseDate(d.t) :newDate();}toObject():any{constcfg:any={};cfg.t='string';returnToObject(this,cfg);}}// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructclassComplexStruct{s:string;i:number;f:number;ts:Date|null;t:Date;o:ComplexStructOtherStruct|null;nno:ComplexStructOtherStruct;constructor(data?:any){constd:any=(data&&typeofdata==='object') ?ToObject(data) :{};this.s=('s'ind) ?d.sasstring :'';this.i=('i'ind) ?d.iasnumber :0;this.f=('f'ind) ?d.fasnumber :0;this.ts=('ts'ind) ?ParseDate(d.ts) :null;this.t=('t'ind) ?ParseDate(d.t) :newDate();this.o=('o'ind) ?newComplexStructOtherStruct(d.o) :null;this.nno=newComplexStructOtherStruct(d.nno);}toObject():any{constcfg:any={};cfg.i='number';cfg.f='number';cfg.t='string';returnToObject(this,cfg);}}// ...exports...

Command Line Usage

➤ struct2ts -husage: struct2ts [<flags>] [<pkg.struct>...]Flags:-h, --help                  Show context-sensitive help (also try --help-longand --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 () <-> JSDate().-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 otheroptions).-s, --src-only              Only output the Go code (helpful if you want toedit 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-onlyis 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

typeCustomTypescriptinterface {RenderCustomTypescript(w io.Writer) (errerror)}

If your model implements aRenderCustomTypescript(w io.Writer) (err error) function it will inject what ever youwrite to the writer at the end of the model. struct2ts will handle the first level of indenting for you.

TODO

  • Usexast to skip reflection.
  • Support annoymous structs.
  • Support ES6.

License

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

About

Generate Typescript classes/interfaces out of Go structs

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp