- Notifications
You must be signed in to change notification settings - Fork15
OneOfOne/struct2ts
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
An extremely simple and powerful Go struct to Typescript Class generator.
Inspired bytkrajina/typescriptify-golang-structs.
go get -u -v github.com/OneOfOne/struct2ts/...- Fairly decent command line interface if you don't wanna write a generator yourself.
- Automatically handles Go
int64timestamps<->JavascriptDate. - Automatically handles json tags.
-omit this field.datehandle convertingtime.Time{}.Unix() <-> javascript Date.,no-nullonly valid for struct fields, forces creating a new class rather than usingnullin TS.,nullallows any field type to benull.
- 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...
➤ 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).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.
- Usexast to skip reflection.
- Support annoymous structs.
Support ES6.
This project is released under theBSD 3-clause "New" or "Revised" License.
About
Generate Typescript classes/interfaces out of Go structs
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.