All Projects → OneOfOne → Struct2ts

OneOfOne / Struct2ts

Generate Typescript classes/interfaces out of Go structs

Programming Languages

go
31211 projects - #10 most used programming language
typescript
32286 projects
golang
3204 projects
es6
455 projects

Projects that are alternatives of or similar to Struct2ts

Coolie
Coolie(苦力) helps you to create models (& their constructors) from a JSON file.
Stars: ✭ 508 (+337.93%)
Mutual labels:  struct, class, generator
goverter
Generate type-safe Go converters by simply defining an interface
Stars: ✭ 100 (-13.79%)
Mutual labels:  generator, struct
Toml To Go
Translates TOML into a Go type in your browser instantly
Stars: ✭ 134 (+15.52%)
Mutual labels:  struct, generator
Structvsclassperformance
POC for my Medium article
Stars: ✭ 11 (-90.52%)
Mutual labels:  struct, class
Faker
Go (Golang) Fake Data Generator for Struct
Stars: ✭ 1,698 (+1363.79%)
Mutual labels:  struct, generator
Go Envparser
a go generator based env to go-struct decoder.
Stars: ✭ 17 (-85.34%)
Mutual labels:  struct, generator
Jsoncsharpclassgenerator
JsonCSharpClassGenerator from http://jsonclassgenerator.codeplex.com/
Stars: ✭ 70 (-39.66%)
Mutual labels:  class, generator
Generators
Laravel File Generators with config and publishable stubs
Stars: ✭ 102 (-12.07%)
Mutual labels:  class
Cray
A Laravel package to help you generate nearly complete CRUD pages like crazy!
Stars: ✭ 108 (-6.9%)
Mutual labels:  generator
Thispersondoesnotexist Js
Api for https://thispersondoesnotexist.com Generates an image of a person that does not exist in real life
Stars: ✭ 101 (-12.93%)
Mutual labels:  generator
Ember Initials
Advanced avatars generator for Ember applications. Initials Ⓜ, images 🌇, gravatars 👩🏻 and adorables 😍 All you need for your users profiles!
Stars: ✭ 99 (-14.66%)
Mutual labels:  generator
Bangajs
Bàngá is a CLI generator for scaffolding ExpressJS applications with speed and efficiency.
Stars: ✭ 102 (-12.07%)
Mutual labels:  generator
Papers Notebook
📄 🇨🇳 📃 论文阅读笔记(分布式系统、虚拟化、机器学习)Papers Notebook (Distributed System, Virtualization, Machine Learning), created by @gaocegege
Stars: ✭ 1,678 (+1346.55%)
Mutual labels:  class
Laravel Table
Generate tables from Eloquent models.
Stars: ✭ 101 (-12.93%)
Mutual labels:  generator
Vue Generator
A CLI generator for Vue components, views and store modules
Stars: ✭ 111 (-4.31%)
Mutual labels:  generator
Lipi
A simple static blog generator.
Stars: ✭ 100 (-13.79%)
Mutual labels:  generator
Idea Plugin Jpa Support
Generate entity/repositroy for JPA/Lombok/Spring Data JPA.
Stars: ✭ 114 (-1.72%)
Mutual labels:  generator
Generact
Generate React components by replicating your own
Stars: ✭ 1,471 (+1168.1%)
Mutual labels:  generator
Rando Php
RandoPhp is a open source library that implements random generators (Integer, Char, Byte, Sequences, Boolean) and take random sample from arrays
Stars: ✭ 107 (-7.76%)
Mutual labels:  generator
Structlinq
Implementation in C# of LINQ concept with struct
Stars: ✭ 106 (-8.62%)
Mutual labels:  struct

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).

Advanced

Custom output per model

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 to skip reflection.
  • Support annoymous structs.
  • Support ES6.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].