Skip to content

Synchronize your front-end models with your Go models

Notifications You must be signed in to change notification settings

natdm/typewriter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typewriter

Parse Go JSON-tagged types to other language types. Focused on front-end languages.

Currently supports JavaScript Flow, TypeScript, and (some) Elm.

For custom types, add the tag, tw:"<CustomTypeName>,<PointerBool>"

Please create an Issue for requests, and include examples of Go types to the requested language.

Does not support: Nested structs (changes to the closest form, eg 'Object' in flow) Interfaces within structs

Example:

In

package stubs

import "time"

// IgnoredField is ignored with @ignore and will not be parsed.
// @ignore
type IgnoredField string

// Example represents most of what TW can do.
type Example struct {
	Embedded
	Basic    string           `json:"basic"`         // basic types
	Maps     map[string]Event `json:"maps"`          // map types
	Slices   []Event          `json:"slices_too"`    // slices
	Pointers *Event           `json:"event_pointer"` // pointers
}

// Event ..
type Event struct {
	Name string `json:"name"`
}

// Embedded is testing an embedded struct
type Embedded struct {
	created_at time.Time `json:"created_at" tw:"Date"` // manually overriding field with a name
}

Out (Flow)

// @flow
// Automatically generated by typewriter. Do not edit.
// http://www.github.com/natdm/typewriter


// Embedded is testing an embedded struct
export type Embedded = {
	created_at: Date// manually overriding field with a name
}

// Event ..
export type Event = {
	name: string
}

// Example represents most of what TW can do.
export type Example = Embedded & {
	basic: string, // basic types
	maps: { [key: string]: Event }, // map types
	slices_too: Array<Event>, // slices
	event_pointer: ?Event, // pointers
}

Usage:

$ go get github.com/natdm/typewriter
$ $GOPATH/bin/typewriter -dir ./your/models/directory -lang flow -v -out ./save/to/models.js
$ typewriter -h
Flags:
	-dir <dir>
		Parse a complete directory
		example: 	-dir= ../src/appname/models/
		default: 	./

	-file <gofile>
		Parse a single go file
		example: 	-file= ../src/appname/models/app.go
		overrides 	-dir and -recursive

	-out <path>
		Saves content to folder
		example: 	-out= ../src/appname/models/
					-out= ../src/appname/models/customname.js
		default: 	./models.

	-lang <lang>
		Language to parse to. One of ["elm", "flow", "ts"]
		example:	-lang flow
		default:	will not parse

	-r
		Transcends directories
		default:	true

	-e
		Expand embedded structs into fields.
		If false, intersection types will be used instead (for "flow" and "ts").
		default:	false

	-v
		Verbose logging, detailing every skipped type, file, or field.
		default: 	false

TODO:

  • More tests
  • Parse types used from other packages