Parse Go types to JavaScript Flow types
Currently a WIP, but very usable. Please create an issue for any edge-cases.
A project that runs Go as the REST server and utilizes Flow (which is fantastic) to keep their types straight on the client side. A personal project of mine needed something like this and I'm able to fit this in to my makefile so each type-change and recompile on the server side results in an update in the API-specific types on the client-side, and the code that needs altered is noticeable, and predictable.
Features include:
- override json names
- override types
- ignore types entirely
- use 'strict' mode
- parse single files, or entire directories (recursively or not)
go get github.com/natdm/goflow
go install
goflow -help
to see useage
flow:"custom_name.custom_type"
will override the Go field type with a custom name amd/or type. Useful for JS Promises and Generators- Use
flow:".custom_type"
to get a custom type with no name-change andflow:"custom_name"
to just change the name
- Add
// @flowignore
to the bottom of any comment above a struct to not parse that to your types.
- Add
// @strict
to the bottom of any comment above a struct to parse the more strict flow object.
Currently, embedded types are not working. Coming soon.- Nested structs are parsed as Objects (but could be overridden by using the
flow
tag). error
andtime.*
are parsed asstring
- Slices of pointers are removing the flow decoration,
?
. You will not be able to check if null yet.
// Person has many types and should all convert correctly
type Person struct {
Name string `json:"name"` // This is a name comment
Age int `json:"age"`
StringOverride string `json:"string_override" flow:"StringOverride.String"` // Override `string` with `String`
Age64 int64 `json:"age64"`
FlowIsAwesome bool `json:"flow_is_awesome"`
Ignore string `json:"-"`
Nullable *string `json:"nullable"`
AnimalsArray []Animal `json:"animals_array"` // I have no pointer
AnimalsArrayPtr *[]Animal `json:"animals_array_ptr"` // I am a pointer
AnimalsArrayPtr2 []*Animal `json:"animals_array_ptr_2"` // I hold pointers
Payrate Payrate `json:"payrate"`
HasComma string `json:"hascomma,omitempty"`
HasParseTag string `json:"hasParseTag" flow:"some_generator.Generator"`
HasLotsOfTags string `gorm:"column:first_name;index:fl_idx" json:"has_lots_of_tags"`
InnerStruct struct {
Name string `json:"name"`
Age int `json:"age"`
Child struct {
Toys []string `json:"toys"`
Name string `json:"name" fname:"innername"`
Friends struct {
Name string `json:"name"`
Age int `json:"age"`
Buddies map[string]Person `json:"buddies"`
EmptyStr struct{} `json:"empty_struct"`
} `json:"friends"`
} `json:"child"`
} `json:"inner_struct"` // I have a comment in a nested struct
Fn func() string
somedata string
MapData map[string]int `json:"map_data"`
}
// Person has many types and should all convert correctly
export type Person = {
name: string, //This is a name comment
age: number,
StringOverride: String, //Override `string` with `String`
age64: number,
flow_is_awesome: boolean,
nullable: ?string,
animals_array: Array<Animal>, //I have no pointer
animals_array_ptr: ?Array<Animal>, //I am a pointer
animals_array_ptr_2: Array<Animal>, //I hold pointers
payrate: Payrate,
hascomma: string,
some_generator: Generator,
has_lots_of_tags: string,
inner_struct: Object, //I have a comment in a nested struct
map_data: { [key: string]: number },
}
Parse inline structsDone with some caveats, room for improvementParse function types within structs (or ignore?)Done (ignoring things with no json tags)Ignore any commas in json tagDoneIgnore anything not exported (structs and fields)DoneSupport type override with tag `Doneflow:"SomeType"
`Parse struct comments and carry over (leave as an optional flag)Done, no flagParse struct comments for an ignore flag (Done// @flowignore
)Allow flow exactsDone, use// @strict
in commentsAllow for primitives (Done - do it with ftypeString
as well as the currentstring
)Speed up parsing of large files. 297 types and 817 fields take 30 secondsDone (cut time in half), but could always be better- Don't blow up on unexported fields with json tags, although that shouldn't be a thing
Parse embedded typesDone- Slices of pointers are removing pointer reference