Synchronize your front-end models with your Go models
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
parse
template
.gitignore
EXAMPLES.md
Makefile
README.md
main.go
models.js
out.js
typewriter

README.md

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 = {
	basic: string, // basic types
	maps: { [key: string]: Event }, // map types
	slices_too: Array<Event>, // slices
	event_pointer: ?Event, // pointers
	created_at: Date// manually overriding field with a name
}

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

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

TODO:

  • More tests
  • Parse types used from other packages