├── .gitignore ├── EXAMPLES.md ├── Makefile ├── README.md ├── examples ├── example.go ├── example_models.go └── package │ └── example.go ├── go.mod ├── go.sum ├── main.go ├── models.js ├── out.js ├── parse └── parse.go ├── template ├── draw.go ├── fragments.go ├── funcmaps.go ├── language_string.go ├── template.go ├── template_test.go ├── valid_ecma.go └── valid_ecma_test.go └── typewriter /.gitignore: -------------------------------------------------------------------------------- 1 | ./models.js -------------------------------------------------------------------------------- /EXAMPLES.md: -------------------------------------------------------------------------------- 1 | 2 | ## Flags in tags: 3 | Use flags in your types to create custom types. If there is an issue parsing a specific type, use the `tw` tag to create one on your own 4 | #### Syntax: `tw:","` 5 | 6 | ```go 7 | type Data struct { 8 | SomeType ArbitraryType `json:"some_type" tw:"overrideType,true"` 9 | } 10 | ``` 11 | 12 | ```js 13 | export type Data = { 14 | some_type: ?overrideType 15 | } 16 | ``` 17 | 18 | ____ 19 | ## Flags in comments: 20 | * `@strict` (flow only) 21 | Set strict to true, making sure the flow object will have exactly these key/value pairs. 22 | ```go 23 | // Data holds data 24 | // @strict 25 | type Data struct { 26 | SomeType ArbitraryType `json:"some_type" tw:"overrideType,true"` 27 | } 28 | ``` 29 | 30 | ```js 31 | export type Data = {| 32 | some_type: ?overrideType 33 | |} 34 | ``` 35 | 36 | 37 | * `@ignore` 38 | Ignore this type while parsing your code 39 | ```go 40 | // Data holds data 41 | // @ignore 42 | type Data struct { 43 | SomeType ArbitraryType `json:"some_type" tw:"overrideType,true"` 44 | } 45 | ``` 46 | will not parse 47 | 48 | ____ 49 | ### Examples of compex Go types to other types: 50 | Go types: 51 | ```go 52 | package stubs 53 | 54 | // Data should all parse right. 55 | // It's hard to get them to do that. 56 | // @strict 57 | type Data struct { 58 | MapStringInt map[string]int `json:"map_string_to_int" tw:"override_map_name,false"` // I am a map of strings and ints 59 | MapStringInts map[string][]int `json:"map_string_to_ints"` // I am a map of strings to a slice of ints 60 | MapStringMap map[string]map[string]int `json:"map_string_to_maps" tw:"override_map_name2,true"` // I am a map of strings to maps 61 | MapIgnore map[int]int `json:"-"` 62 | Peeps People `json:"peeps"` 63 | } 64 | 65 | // Person ... 66 | type Person struct { 67 | Name string `json:"name"` 68 | Age int `json:"age"` 69 | } 70 | 71 | // People is a map of strings to person 72 | type People map[string]Person 73 | 74 | // Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 75 | type Nested struct { 76 | Person struct{} `json:"person"` 77 | } 78 | 79 | // Embedded will take all types from the embedded types and insert them in to the new type. 80 | type Embedded struct { 81 | Person 82 | } 83 | 84 | ``` 85 | 86 | To Flow Types: 87 | ```js 88 | // @flow 89 | // Automatically generated by typewriter. Do not edit. 90 | // http://www.github.com/natdm/typewriter 91 | 92 | 93 | // Data should all parse right. 94 | // It's hard to get them to do that. 95 | // @strict 96 | export type Data = {| 97 | map_string_to_int: override_map_name, // I am a map of strings and ints 98 | 99 | map_string_to_ints: { [key: string]: Array }, // I am a map of strings to a slice of ints 100 | 101 | map_string_to_maps: ?override_map_name2, // I am a map of strings to maps 102 | 103 | peeps: People 104 | |} 105 | 106 | // Embedded will take all types from the embedded types and insert them in to the new type. 107 | export type Embedded = { 108 | name: string, 109 | age: number 110 | } 111 | 112 | export type MyNumber = number 113 | 114 | // Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 115 | export type Nested = { 116 | person: Object 117 | } 118 | 119 | // People is a map of strings to person 120 | export type People = { [key: string]: Person } 121 | 122 | // Person ... 123 | export type Person = { 124 | name: string, 125 | age: number 126 | } 127 | 128 | export type Thing = { 129 | name: number 130 | } 131 | ``` 132 | 133 | To TypeScript types 134 | ```js 135 | // Automatically generated by typewriter. Do not edit. 136 | // http://www.github.com/natdm/typewriter 137 | 138 | 139 | // Data should all parse right. 140 | // It's hard to get them to do that. 141 | // @strict 142 | type Data = { 143 | map_string_to_int: override_map_name, // I am a map of strings and ints 144 | 145 | map_string_to_ints: { [key: string]: Array }, // I am a map of strings to a slice of ints 146 | 147 | map_string_to_maps: ?override_map_name2, // I am a map of strings to maps 148 | 149 | peeps: People 150 | } 151 | 152 | // Embedded will take all types from the embedded types and insert them in to the new type. 153 | type Embedded = { 154 | name: string, 155 | age: number 156 | } 157 | 158 | type MyNumber = number 159 | 160 | // Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 161 | type Nested = { 162 | person: Object 163 | } 164 | 165 | // People is a map of strings to person 166 | type People = { [key: string]: Person } 167 | 168 | // Person ... 169 | type Person = { 170 | name: string, 171 | age: number 172 | } 173 | 174 | type Thing = { 175 | name: number 176 | } 177 | 178 | ``` 179 | 180 | To Elm types (work in progress) 181 | ```elm 182 | -- Automatically generated by typewriter. Do not edit. 183 | -- http://www.github.com/natdm/typewriter 184 | 185 | 186 | --Data should all parse right. 187 | --It's hard to get them to do that. 188 | --@strict 189 | type alias Data : { 190 | map_string_to_int : override_map_name, --I am a map of strings and ints 191 | map_string_to_ints :Dict String List Int, --I am a map of strings to a slice of ints 192 | map_string_to_maps : override_map_name2, --I am a map of strings to maps 193 | peeps : People 194 | } 195 | 196 | --Embedded will take all types from the embedded types and insert them in to the new type. 197 | type alias Embedded : { 198 | name : String, age : Int 199 | } 200 | 201 | type alias MyNumber : Int 202 | 203 | --Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 204 | type alias Nested : { 205 | person : Maybe 206 | } 207 | 208 | --People is a map of strings to person 209 | type alias People : Dict String Person 210 | 211 | --Person ... 212 | type alias Person : { 213 | name : String, age : Int 214 | } 215 | 216 | type alias Thing : { 217 | name : Int 218 | } 219 | 220 | ``` 221 | --- 222 | 223 | ### Example use in a package like [Ponzu](https://github.com/ponzu-cms/ponzu): 224 | `$ ponzu gen content review title:"string" body:"string" rating:"int" tags:"[] 225 | string" && typewriter -file ./content/review.go -lang flow -out ./models.js` 226 | 227 | Ponzu: 228 | 229 | ```go 230 | ... 231 | 232 | type Review struct { 233 | item.Item 234 | 235 | Title string `json:"title"` 236 | Body string `json:"body"` 237 | Rating int `json:"rating"` 238 | Tags []string `json:"tags"` 239 | } 240 | 241 | ... 242 | ``` 243 | 244 | Typewriter generates: 245 | ```js 246 | // @flow 247 | // Automatically generated by typewriter. Do not edit. 248 | // http://www.github.com/natdm/typewriter 249 | 250 | 251 | export type Review = { 252 | title: string, 253 | body: string, 254 | rating: number, 255 | tags: Array 256 | } 257 | ``` -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | GOCMD=go 2 | GOBUILD=$(GOCMD) build 3 | GOCLEAN=$(GOCMD) clean 4 | GOINSTALL=$(GOCMD) install 5 | GOTEST=$(GOCMD) test 6 | 7 | .PHONY: test 8 | 9 | test: 10 | $(GOTEST) -v ./template 11 | 12 | build: 13 | $(GOBUILD) -v ./ 14 | 15 | flow: 16 | ./typewriter -file ./examples/example.go -lang flow -out ./models.js -v 17 | 18 | elm: 19 | ./typewriter -dir ./examples -lang elm -out ./models.js 20 | 21 | ts: 22 | ./typewriter -dir ./examples -lang ts -out ./models.js 23 | 24 | testts: 25 | ./typewriter -file=./stubs/struct.go -r=false -out=./stubs/typescript -lang=typescript -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Typewriter 2 | 3 | ### Parse Go JSON-tagged types to other language types. Focused on front-end languages. 4 | 5 | 6 | Currently supports JavaScript Flow, TypeScript, and (some) Elm. 7 | 8 | For custom types, add the tag, `tw:","` 9 | 10 | Please create an Issue for requests, and include examples of Go types to the requested language. 11 | 12 | Does not support: 13 | Nested structs (changes to the closest form, eg 'Object' in flow) 14 | Interfaces within structs 15 | 16 | ### Example: 17 | 18 | #### In 19 | ```go 20 | package stubs 21 | 22 | import "time" 23 | 24 | // IgnoredField is ignored with @ignore and will not be parsed. 25 | // @ignore 26 | type IgnoredField string 27 | 28 | // Example represents most of what TW can do. 29 | type Example struct { 30 | Embedded 31 | Basic string `json:"basic"` // basic types 32 | Maps map[string]Event `json:"maps"` // map types 33 | Slices []Event `json:"slices_too"` // slices 34 | Pointers *Event `json:"event_pointer"` // pointers 35 | } 36 | 37 | // Event .. 38 | type Event struct { 39 | Name string `json:"name"` 40 | } 41 | 42 | // Embedded is testing an embedded struct 43 | type Embedded struct { 44 | created_at time.Time `json:"created_at" tw:"Date"` // manually overriding field with a name 45 | } 46 | ``` 47 | #### Out (Flow) 48 | ```js 49 | // @flow 50 | // Automatically generated by typewriter. Do not edit. 51 | // http://www.github.com/natdm/typewriter 52 | 53 | 54 | // Embedded is testing an embedded struct 55 | export type Embedded = { 56 | created_at: Date// manually overriding field with a name 57 | } 58 | 59 | // Event .. 60 | export type Event = { 61 | name: string 62 | } 63 | 64 | // Example represents most of what TW can do. 65 | export type Example = Embedded & { 66 | basic: string, // basic types 67 | maps: { [key: string]: Event }, // map types 68 | slices_too: Array, // slices 69 | event_pointer: ?Event, // pointers 70 | } 71 | ``` 72 | 73 | ### Usage: 74 | 75 | ``` 76 | $ go get github.com/natdm/typewriter 77 | $ $GOPATH/bin/typewriter -dir ./your/models/directory -lang flow -v -out ./save/to/models.js 78 | ``` 79 | 80 | ```bash 81 | $ typewriter -h 82 | Flags: 83 | -dir 84 | Parse a complete directory 85 | example: -dir= ../src/appname/models/ 86 | default: ./ 87 | 88 | -file 89 | Parse a single go file 90 | example: -file= ../src/appname/models/app.go 91 | overrides -dir and -recursive 92 | 93 | -out 94 | Saves content to folder 95 | example: -out= ../src/appname/models/ 96 | -out= ../src/appname/models/customname.js 97 | default: ./models. 98 | 99 | -lang 100 | Language to parse to. One of ["elm", "flow", "ts"] 101 | example: -lang flow 102 | default: will not parse 103 | 104 | -r 105 | Transcends directories 106 | default: true 107 | 108 | -e 109 | Expand embedded structs into fields. 110 | If false, intersection types will be used instead (for "flow" and "ts"). 111 | default: false 112 | 113 | -v 114 | Verbose logging, detailing every skipped type, file, or field. 115 | default: false 116 | ``` 117 | 118 | ___ 119 | #### TODO: 120 | * More tests 121 | * Parse types used from other packages 122 | -------------------------------------------------------------------------------- /examples/example.go: -------------------------------------------------------------------------------- 1 | package stubs 2 | 3 | import ( 4 | "github.com/jinzhu/gorm" 5 | pkg "github.com/natdm/typewriter/examples/package" 6 | "github.com/ponzu-cms/ponzu/system/item" 7 | ) 8 | 9 | // Data should all parse right. 10 | // It's hard to get them to do that. 11 | // @strict 12 | type Data struct { 13 | MapStringInt map[string]int `json:"map_string_to_int" tw:"override_map_name,false"` // I am a map of strings and ints 14 | MapStringInts map[string][]int `json:"map_string_to_ints"` // I am a map of strings to a slice of ints 15 | MapStringMap map[string]map[string]int `json:"map_string_to_maps" tw:"override_map_name2,true"` // I am a map of strings to maps 16 | MapIgnore map[int]int `json:"-"` 17 | Peeps People `json:"peeps"` 18 | ExternalMap pkg.DataType `json:"external_embedded"` 19 | KebabCase string `json:"kebab-case"` 20 | } 21 | 22 | type MyInvalidJsType struct { 23 | someProperty string `json:"some-property"` // wow, why did we do this? totally valid JS though 24 | anotherProperty string `json:"property/another"` // this is simply absurd 25 | additionalProperty string `json:"properties#additional"` // darn, it's all over our code! 26 | furtherProperty string `json:"属性"` // 我们没有时间啊! 27 | } 28 | 29 | // Person ... 30 | type Person struct { 31 | Name string `json:"name"` 32 | Age int `json:"age"` 33 | } 34 | 35 | // People is a map of strings to person 36 | type People map[string]Person 37 | 38 | // Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 39 | type Nested struct { 40 | Person struct{} `json:"person"` 41 | } 42 | 43 | // EmbeddedGormModelTest represents a model that has an embedded type in it. 44 | type EmbeddedGormModelTest struct { 45 | gorm.Model 46 | Name string `gorm:"column:name;index" json:"name"` 47 | Description string `gorm:"column:description" json:"description"` 48 | EditEvent bool `gorm:"column:edit_event" json:"edit_event"` 49 | DelBid bool `gorm:"column:del_bid" json:"del_bid"` 50 | AddBid bool `gorm:"column:add_bid" json:"add_bid"` 51 | Billing bool `gorm:"column:billing" json:"billing"` 52 | } 53 | 54 | type ExternalEmbedded struct { 55 | item.Item 56 | Name string `json:"name"` 57 | } 58 | 59 | type Items []item.Item 60 | -------------------------------------------------------------------------------- /examples/example_models.go: -------------------------------------------------------------------------------- 1 | package stubs 2 | 3 | import "time" 4 | 5 | // Date is to assist things 6 | // @ignore 7 | type Date string 8 | 9 | // Time .. 10 | type Time Date 11 | 12 | // Example represents most of what TW can do. 13 | type Example struct { 14 | Embedded 15 | Basic string `json:"basic"` // basic types 16 | Maps map[string]Event `json:"maps"` // map types 17 | Slices []Event `json:"slices_too"` // slices 18 | Pointers *Event `json:"event_pointer"` // pointers 19 | } 20 | 21 | // Event .. 22 | type Event struct { 23 | Name string `json:"name"` 24 | } 25 | 26 | // Embedded is testing an embedded struct 27 | type Embedded struct { 28 | created_at time.Time `json:"created_at" tw:"Date"` // manually overriding field with a name 29 | } 30 | -------------------------------------------------------------------------------- /examples/package/example.go: -------------------------------------------------------------------------------- 1 | package elm 2 | 3 | type MyNumber int 4 | 5 | type Thing struct { 6 | Name int `json:"name"` 7 | } 8 | 9 | type DataType map[string]string 10 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/natdm/typewriter 2 | 3 | go 1.13 4 | 5 | require ( 6 | github.com/blevesearch/bleve v1.0.9 // indirect 7 | github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d // indirect 8 | github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect 9 | github.com/cznic/strutil v0.0.0-20181122101858-275e90344537 // indirect 10 | github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect 11 | github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect 12 | github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect 13 | github.com/gofrs/uuid v3.3.0+incompatible // indirect 14 | github.com/jinzhu/gorm v1.9.12 15 | github.com/jmhodges/levigo v1.0.0 // indirect 16 | github.com/ponzu-cms/ponzu v0.11.0 17 | github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect 18 | github.com/sirupsen/logrus v1.6.0 19 | github.com/stretchr/testify v1.4.0 20 | github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect 21 | ) 22 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 2 | github.com/RoaringBitmap/roaring v0.4.21 h1:WJ/zIlNX4wQZ9x8Ey33O1UaD9TCTakYsdLFSBcTwH+8= 3 | github.com/RoaringBitmap/roaring v0.4.21/go.mod h1:D0gp8kJQgE1A4LQ5wFLggQEyvDi06Mq5mKs52e1TwOo= 4 | github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= 5 | github.com/blevesearch/bleve v1.0.9 h1:kqw/Ank/61UV9/Bx9kCcnfH6qWPgmS8O5LNfpsgzASg= 6 | github.com/blevesearch/bleve v1.0.9/go.mod h1:tb04/rbU29clbtNgorgFd8XdJea4x3ybYaOjWKr+UBU= 7 | github.com/blevesearch/blevex v0.0.0-20190916190636-152f0fe5c040 h1:SjYVcfJVZoCfBlg+fkaq2eoZHTf5HaJfaTeTkOtyfHQ= 8 | github.com/blevesearch/blevex v0.0.0-20190916190636-152f0fe5c040/go.mod h1:WH+MU2F4T0VmSdaPX+Wu5GYoZBrYWdOZWSjzvYcDmqQ= 9 | github.com/blevesearch/go-porterstemmer v1.0.3 h1:GtmsqID0aZdCSNiY8SkuPJ12pD4jI+DdXTAn4YRcHCo= 10 | github.com/blevesearch/go-porterstemmer v1.0.3/go.mod h1:angGc5Ht+k2xhJdZi511LtmxuEf0OVpvUUNrwmM1P7M= 11 | github.com/blevesearch/mmap-go v1.0.2 h1:JtMHb+FgQCTTYIhtMvimw15dJwu1Y5lrZDMOFXVWPk0= 12 | github.com/blevesearch/mmap-go v1.0.2/go.mod h1:ol2qBqYaOUsGdm7aRMRrYGgPvnwLe6Y+7LMvAB5IbSA= 13 | github.com/blevesearch/segment v0.9.0 h1:5lG7yBCx98or7gK2cHMKPukPZ/31Kag7nONpoBt22Ac= 14 | github.com/blevesearch/segment v0.9.0/go.mod h1:9PfHYUdQCgHktBgvtUOF4x+pc4/l8rdH0u5spnW85UQ= 15 | github.com/blevesearch/snowballstem v0.9.0 h1:lMQ189YspGP6sXvZQ4WZ+MLawfV8wOmPoD/iWeNXm8s= 16 | github.com/blevesearch/snowballstem v0.9.0/go.mod h1:PivSj3JMc8WuaFkTSRDW2SlrulNWPl4ABg1tC/hlgLs= 17 | github.com/blevesearch/zap/v11 v11.0.9 h1:wlSrDBeGN1G4M51NQHIXca23ttwUfQpWaK7uhO5lRSo= 18 | github.com/blevesearch/zap/v11 v11.0.9/go.mod h1:47hzinvmY2EvvJruzsSCJpro7so8L1neseaGjrtXHOY= 19 | github.com/blevesearch/zap/v12 v12.0.9 h1:PpatkY+BLVFZf0Ok3/fwgI/I4RU0z5blXFGuQANmqXk= 20 | github.com/blevesearch/zap/v12 v12.0.9/go.mod h1:paQuvxy7yXor+0Mx8p2KNmJgygQbQNN+W6HRfL5Hvwc= 21 | github.com/blevesearch/zap/v13 v13.0.1 h1:NSCM6uKu77Vn/x9nlPp4pE1o/bftqcOWZEHSyZVpGBQ= 22 | github.com/blevesearch/zap/v13 v13.0.1/go.mod h1:XmyNLMvMf8Z5FjLANXwUeDW3e1+o77TTGUWrth7T9WI= 23 | github.com/blevesearch/zap/v14 v14.0.0 h1:HF8Ysjm13qxB0jTGaKLlatNXmJbQD8bY+PrPxm5v4hE= 24 | github.com/blevesearch/zap/v14 v14.0.0/go.mod h1:sUc/gPGJlFbSQ2ZUh/wGRYwkKx+Dg/5p+dd+eq6QMXk= 25 | github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= 26 | github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= 27 | github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= 28 | github.com/couchbase/ghistogram v0.1.0/go.mod h1:s1Jhy76zqfEecpNWJfWUiKZookAFaiGOEoyzgHt9i7k= 29 | github.com/couchbase/moss v0.1.0/go.mod h1:9MaHIaRuy9pvLPUJxB8sh8OrLfyDczECVL37grCIubs= 30 | github.com/couchbase/vellum v1.0.1 h1:qrj9ohvZedvc51S5KzPfJ6P6z0Vqzv7Lx7k3mVc2WOk= 31 | github.com/couchbase/vellum v1.0.1/go.mod h1:FcwrEivFpNi24R3jLOs3n+fs5RnuQnQqCLBJ1uAg1W4= 32 | github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= 33 | github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d h1:SwD98825d6bdB+pEuTxWOXiSjBrHdOl/UVp75eI7JT8= 34 | github.com/cznic/b v0.0.0-20181122101859-a26611c4d92d/go.mod h1:URriBxXwVq5ijiJ12C7iIZqlA69nTlI+LgI6/pwftG8= 35 | github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso= 36 | github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM= 37 | github.com/cznic/strutil v0.0.0-20181122101858-275e90344537 h1:MZRmHqDBd0vxNwenEbKSQqRVT24d3C05ft8kduSwlqM= 38 | github.com/cznic/strutil v0.0.0-20181122101858-275e90344537/go.mod h1:AHHPPPXTw0h6pVabbcbyGRK1DckRn7r/STdZEeIDzZc= 39 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 40 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 41 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 42 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= 43 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= 44 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= 45 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= 46 | github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0= 47 | github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= 48 | github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A= 49 | github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg= 50 | github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk= 51 | github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= 52 | github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= 53 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 54 | github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2 h1:Ujru1hufTHVb++eG6OuNDKMxZnGIvF6o/u8q/8h2+I4= 55 | github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= 56 | github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31 h1:gclg6gY70GLy3PbkQ1AERPfmLMMagS60DKF78eWwLn8= 57 | github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= 58 | github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= 59 | github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= 60 | github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84= 61 | github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= 62 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= 63 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= 64 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 65 | github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= 66 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 67 | github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 68 | github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= 69 | github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 70 | github.com/gopherjs/gopherjs v0.0.0-20190910122728-9d188e94fb99 h1:twflg0XRTjwKpxb/jFExr4HGq6on2dEOmnL6FV+fgPw= 71 | github.com/gopherjs/gopherjs v0.0.0-20190910122728-9d188e94fb99/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 72 | github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= 73 | github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= 74 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 75 | github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 76 | github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= 77 | github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= 78 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 79 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 80 | github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= 81 | github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 82 | github.com/jmhodges/levigo v1.0.0 h1:q5EC36kV79HWeTBWsod3mG11EgStG3qArTKcvlksN1U= 83 | github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ= 84 | github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= 85 | github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= 86 | github.com/kljensen/snowball v0.6.0/go.mod h1:27N7E8fVU5H68RlUmnWwZCfxgt4POBJfENGMvNRhldw= 87 | github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= 88 | github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 89 | github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= 90 | github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 91 | github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= 92 | github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= 93 | github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= 94 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= 95 | github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= 96 | github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg= 97 | github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM= 98 | github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw= 99 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 100 | github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= 101 | github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 102 | github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= 103 | github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= 104 | github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= 105 | github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ= 106 | github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= 107 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 108 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 109 | github.com/ponzu-cms/ponzu v0.11.0 h1:1OtAVUPRZaV+itp7W1x6NwDRMI3HHUBRS8gusGIYmuA= 110 | github.com/ponzu-cms/ponzu v0.11.0/go.mod h1:KLPKpSX4TA1ChtQ/NkGmk+3qyUev5uOITRt0RmbChQo= 111 | github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= 112 | github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= 113 | github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= 114 | github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= 115 | github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= 116 | github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= 117 | github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= 118 | github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= 119 | github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= 120 | github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= 121 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 122 | github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= 123 | github.com/steveyen/gtreap v0.1.0 h1:CjhzTa274PyJLJuMZwIzCO1PfC00oRa8d1Kc78bFXJM= 124 | github.com/steveyen/gtreap v0.1.0/go.mod h1:kl/5J7XbrOmlIbYIXdRHDDE5QxHqpk0cmkT7Z4dM9/Y= 125 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 126 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 127 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 128 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 129 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 130 | github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= 131 | github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= 132 | github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok= 133 | github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8= 134 | github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU= 135 | github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= 136 | github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= 137 | github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc= 138 | github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= 139 | github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= 140 | go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg= 141 | go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= 142 | golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 143 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 144 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 145 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= 146 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 147 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 148 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 149 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= 150 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 151 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 152 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 153 | golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 154 | golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 155 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 156 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 157 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= 158 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 159 | golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 160 | golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= 161 | golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 162 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 163 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 164 | google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= 165 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 166 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 167 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 168 | gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= 169 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 170 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= 171 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 172 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 173 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 174 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 175 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // Licensed under the Apache License, Version 2.0 (the "License"); 2 | // you may not use this file except in compliance with the License. 3 | // You may obtain a copy of the License at 4 | // 5 | // http://www.apache.org/licenses/LICENSE-2.0 6 | // 7 | // Unless required by applicable law or agreed to in writing, software 8 | // distributed under the License is distributed on an "AS IS" BASIS, 9 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | // See the License for the specific language governing permissions and 11 | // limitations under the License. 12 | package main 13 | 14 | import ( 15 | "flag" 16 | "fmt" 17 | "io" 18 | "os" 19 | 20 | "github.com/natdm/typewriter/parse" 21 | "github.com/natdm/typewriter/template" 22 | log "github.com/sirupsen/logrus" 23 | ) 24 | 25 | func main() { 26 | inFlag := flag.String("dir", "./", "dir is to specify what folder to parse types from") 27 | fileFlag := flag.String("file", "", "file is to parse a single file. Will override a directory") 28 | langFlag := flag.String("lang", "", "determine the language. One of 'flow', 'ts") 29 | outFlag := flag.String("out", "", "file and path to save output to") 30 | vFlag := flag.Bool("v", false, "verbose logging") 31 | recursiveFlag := flag.Bool("r", true, "to recursively ascend all folders in dir") 32 | expandEmbeddedFlag := flag.Bool("e", false, "expand embedded structs inline") 33 | flag.Usage = usage 34 | flag.Parse() 35 | 36 | var lang template.Language 37 | switch *langFlag { 38 | case "flow": 39 | lang = template.Flow 40 | case "elm": 41 | lang = template.Elm 42 | if !*expandEmbeddedFlag { 43 | log.Fatalln( 44 | "You have to use -e flag with Elm, which does not support intersection types") 45 | } 46 | case "ts": 47 | lang = template.Typescript 48 | default: 49 | log.Fatalln("Please pick a proper language ['elm', 'flow', 'ts']") 50 | } 51 | 52 | var out io.Writer 53 | 54 | if *outFlag != "" { 55 | f, err := os.Create(*outFlag) 56 | if err != nil { 57 | log.Fatalln(err) 58 | } 59 | defer f.Close() 60 | out = f 61 | } else { 62 | out = os.Stdout 63 | } 64 | 65 | var ( 66 | files []string 67 | types map[string]*template.PackageType 68 | err error 69 | ) 70 | 71 | if *fileFlag != "" { 72 | types, err = parse.Files([]string{*fileFlag}, *vFlag, *expandEmbeddedFlag) 73 | if err != nil { 74 | log.Fatalln(err) 75 | } 76 | } else { 77 | if err = parse.Directory(*inFlag, *recursiveFlag, &files, *vFlag); err != nil { 78 | log.Fatalln(err) 79 | } 80 | types, err = parse.Files(files, *vFlag, *expandEmbeddedFlag) 81 | if err != nil { 82 | log.Fatalln(err) 83 | } 84 | } 85 | ct, err := template.Draw(types, out, lang, *vFlag) 86 | if err != nil { 87 | log.Fatalln(err) 88 | } 89 | log.WithField("output_type_ct", ct).Info("Done") 90 | } 91 | 92 | func usage() { 93 | fmt.Print(` 94 | Typewriter 95 | Convert Go types to other languages 96 | Visit http://www.github.com/natdm/typewriter for more detailed example useage. 97 | 98 | Flags: 99 | -dir 100 | Parse a complete directory 101 | example: -dir= ../src/appname/models/ 102 | default: ./ 103 | 104 | -file 105 | Parse a single go file 106 | example: -file= ../src/appname/models/app.go 107 | overrides -dir and -recursive 108 | 109 | -out 110 | Saves content to folder 111 | example: -out= ../src/appname/models/ 112 | -out= ../src/appname/models/customname.js 113 | default: ./models. 114 | 115 | -lang 116 | Language to parse to. One of ["flow", "elm", "ts"] 117 | example: -lang flow 118 | default: will not parse 119 | 120 | -r 121 | Transcends directories 122 | default: true 123 | 124 | -v 125 | Verbose logging, detailing every skipped type, file, or field. 126 | default: false 127 | `) 128 | } 129 | -------------------------------------------------------------------------------- /models.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | // Automatically generated by typewriter. Do not edit. 3 | // http://www.github.com/natdm/typewriter 4 | 5 | 6 | // Data should all parse right. 7 | // It's hard to get them to do that. 8 | // @strict 9 | export type Data = {| 10 | map_string_to_int: override_map_name, // I am a map of strings and ints 11 | map_string_to_ints: { [key: string]: Array }, // I am a map of strings to a slice of ints 12 | map_string_to_maps: ?override_map_name2, // I am a map of strings to maps 13 | peeps: People, 14 | external_embedded: DataType, 15 | "kebab-case": string 16 | |} 17 | 18 | // Embedded will take all types from the embedded types and insert them in to the new type. 19 | export type Embedded = { 20 | name: string, 21 | age: number 22 | } 23 | 24 | // EmbeddedGormModelTest represents a model that has an embedded type in it. 25 | export type EmbeddedGormModelTest = { 26 | name: string, 27 | description: string, 28 | edit_event: boolean, 29 | del_bid: boolean, 30 | add_bid: boolean, 31 | billing: boolean 32 | } 33 | 34 | export type ExternalEmbedded = { 35 | name: string 36 | } 37 | 38 | export type Items = Array 39 | 40 | export type MyInvalidJsType = { 41 | "some-property": string, // wow, why did we do this? totally valid JS though 42 | "property/another": string, // this is simply absurd 43 | "properties#additional": string, // darn, it's all over our code! 44 | "属性": string// 我们没有时间啊! 45 | } 46 | 47 | // Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 48 | export type Nested = { 49 | person: Object 50 | } 51 | 52 | // People is a map of strings to person 53 | export type People = { [key: string]: Person } 54 | 55 | // Person ... 56 | export type Person = { 57 | name: string, 58 | age: number 59 | } 60 | -------------------------------------------------------------------------------- /out.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | // Automatically generated by typewriter. Do not edit. 3 | // http://www.github.com/natdm/typewriter 4 | 5 | 6 | // Data should all parse right. 7 | // It's hard to get them to do that. 8 | // @strict 9 | export type Data = {| 10 | map_string_to_int: override_map_name, // I am a map of strings and ints 11 | 12 | map_string_to_ints: { [key: string]: Array }, // I am a map of strings to a slice of ints 13 | 14 | map_string_to_maps: ?override_map_name2, // I am a map of strings to maps 15 | 16 | peeps: People, 17 | external_embedded: DataType 18 | |} 19 | 20 | // Embedded will take all types from the embedded types and insert them in to the new type. 21 | export type Embedded = { 22 | name: string, 23 | age: number 24 | } 25 | 26 | export type ExternalEmbedded = { 27 | name: string, 28 | uuid: UUID, 29 | id: number, 30 | slug: string, 31 | timestamp: number, 32 | updated: number 33 | } 34 | 35 | export type Items = Array 36 | 37 | // Nested defaults to the closest "Object" type in any language. Utilize the `tw` tag if needed. 38 | export type Nested = { 39 | person: Object 40 | } 41 | 42 | // People is a map of strings to person 43 | export type People = { [key: string]: Person } 44 | 45 | // Person ... 46 | export type Person = { 47 | name: string, 48 | age: number 49 | } 50 | -------------------------------------------------------------------------------- /parse/parse.go: -------------------------------------------------------------------------------- 1 | package parse 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "go/ast" 7 | "go/parser" 8 | "go/token" 9 | "io/ioutil" 10 | "strings" 11 | 12 | "os" 13 | 14 | "github.com/natdm/typewriter/template" 15 | log "github.com/sirupsen/logrus" 16 | ) 17 | 18 | var ( 19 | errSkipType = errors.New("not a supported type") 20 | errTypeAssert = errors.New("type assertion failed") 21 | errParsingTypeDetails = errors.New("failed to parse type within type") 22 | errEmbeddedType = errors.New("embedded type") 23 | ) 24 | 25 | // commentFlags are flags declared in package-level types to be handed down the parsing logic 26 | type commentFlags struct { 27 | // strict is for flow types only. 28 | strict bool 29 | 30 | // ignore ignores the type from being parsed 31 | ignore bool 32 | } 33 | 34 | // Directory parses a directory and returns all the go files that are not test files 35 | // It takes a directory, a recursive boolean option, and an out to put the files in. 36 | func Directory(d string, r bool, out *[]string, verbose bool) error { 37 | fs, err := ioutil.ReadDir(d) 38 | if err != nil { 39 | return err 40 | } 41 | for _, v := range fs { 42 | name := v.Name() 43 | if v.IsDir() { 44 | if r { 45 | if err := Directory(d+"/"+name, r, out, verbose); err != nil { 46 | return err 47 | } 48 | } 49 | } else if strings.HasSuffix(name, "go") && !strings.Contains(name, "_test.go") { 50 | *out = append(*out, strings.Replace(fmt.Sprintf("%s/%s", d, name), "//", "/", -1)) 51 | } 52 | } 53 | return nil 54 | } 55 | 56 | // findImports should keep either the alias name of the import or the package name of an imported package 57 | func findImports(f *ast.File) map[string]string { 58 | r := strings.NewReplacer("\"", "") 59 | imports := make(map[string]string) 60 | for _, v := range f.Imports { 61 | srcpath := os.Getenv("GOPATH") + "/src/" 62 | if v.Name != nil { 63 | imports[v.Name.String()] = r.Replace(srcpath + v.Path.Value) 64 | } else { 65 | _n := strings.Split(v.Path.Value, "/") 66 | n := r.Replace(_n[len(_n)-1]) 67 | imports[n] = r.Replace(srcpath + v.Path.Value) 68 | } 69 | } 70 | return imports 71 | } 72 | 73 | // Files parses files and returns the type information 74 | func Files(files []string, verbose bool, expandEmbedded bool) (map[string]*template.PackageType, error) { 75 | typs := make(map[string]*template.PackageType) 76 | externals := make(map[string]string) 77 | for _, name := range files { 78 | fset := token.NewFileSet() // positions are relative to fset 79 | 80 | // Parse the file given in arguments 81 | f, err := parser.ParseFile(fset, name, nil, parser.ParseComments) 82 | if err != nil { 83 | return nil, err 84 | } 85 | 86 | for k, v := range findImports(f) { 87 | externals[k] = v 88 | } 89 | 90 | comments := make(map[string]string) 91 | for _, v := range f.Comments { 92 | c := v.Text() 93 | comments[firstWord(c)] = c 94 | } 95 | 96 | bs, err := ioutil.ReadFile(name) 97 | if err != nil { 98 | return nil, err 99 | } 100 | 101 | OBJLOOP: 102 | for _, v := range f.Scope.Objects { 103 | if v.Kind == ast.Typ { 104 | comment := comments[v.Name] 105 | flags := commentFlags{ 106 | strict: strings.Contains(comment, "@strict"), 107 | ignore: strings.Contains(comment, "@ignore"), 108 | } 109 | if flags.ignore { 110 | if verbose { 111 | log.WithField("type_name", v.Name).WithField("file_name", name).Info("skipping type with '@ignore' flag") 112 | } 113 | continue 114 | } 115 | ts, ok := v.Decl.(*ast.TypeSpec) 116 | if !ok { 117 | continue OBJLOOP 118 | } 119 | t, err := Type(bs, ts, verbose, flags) 120 | if err != nil { 121 | if verbose { 122 | log.WithError(err).WithField("type_name", v.Name).WithField("file_name", name).Error("error parsing type, skipped") 123 | } 124 | continue OBJLOOP 125 | } 126 | t.Comment = comment 127 | typs[v.Name] = t 128 | } 129 | } 130 | } 131 | 132 | if expandEmbedded { 133 | expandEmbeddedTypes(typs, externals) 134 | } 135 | return typs, nil 136 | } 137 | 138 | // parseEmbedded nests embedded type fields in the structs containing embedded types 139 | func expandEmbeddedTypes(types map[string]*template.PackageType, pkgs map[string]string) { 140 | for _, v := range types { 141 | switch v.Type.(type) { 142 | case *template.Struct: 143 | s := v.Type.(*template.Struct) 144 | EMBEDLOOP: 145 | for _, v := range s.Embedded { 146 | _v := strings.TrimSpace(v) 147 | if _, ok := types[_v]; ok { 148 | if s2, ok := types[_v].Type.(*template.Struct); ok { 149 | s.Fields = append(s.Fields, s2.Fields...) 150 | } 151 | continue EMBEDLOOP 152 | } 153 | if strings.Contains(v, ".") { 154 | // Type is in another package 155 | str := strings.Split(v, ".") 156 | pkg := strings.TrimSpace(str[0]) 157 | name := strings.TrimSpace(str[1]) 158 | 159 | files := []string{} 160 | err := Directory(pkgs[pkg], false, &files, false) 161 | typs, err := Files(files, false, true) 162 | if err != nil { 163 | log.WithError(err).Error("error parsing files") 164 | continue 165 | } 166 | 167 | if _, ok := types[name]; ok { 168 | // type already exists 169 | continue 170 | } 171 | 172 | if typ, ok := typs[name]; ok { 173 | switch typ.Type.(type) { 174 | case *template.Struct: 175 | st := typ.Type.(*template.Struct) 176 | s.Fields = append(s.Fields, st.Fields...) 177 | default: 178 | panic("Embedded type is not struct") 179 | } 180 | } else { 181 | log.WithField("type", _v).Warn("could not find embedded type in external package") 182 | } 183 | 184 | } else { 185 | // do nothing, we can't find the type 186 | } 187 | } 188 | s.Embedded = nil 189 | default: 190 | 191 | } 192 | } 193 | } 194 | 195 | // Type creates a package level type. 196 | func Type(bs []byte, ts *ast.TypeSpec, verbose bool, flags commentFlags) (*template.PackageType, error) { 197 | s := &template.PackageType{} 198 | s.Name = ts.Name.Name 199 | if ts.Comment != nil { 200 | s.Comment = ts.Comment.Text() 201 | } 202 | 203 | switch x := ts.Type.(type) { 204 | case *ast.ChanType, *ast.FuncLit, *ast.FuncType: 205 | return nil, errSkipType 206 | 207 | case *ast.InterfaceType: 208 | return nil, errSkipType 209 | 210 | case *ast.ArrayType: 211 | t, err := parseType(x.Elt) 212 | if err != nil { 213 | return nil, err 214 | } 215 | s.Type = &template.Array{ 216 | Type: t, 217 | } 218 | return s, nil 219 | 220 | case *ast.MapType: 221 | key, err := parseType(x.Key) 222 | if err != nil { 223 | return nil, err 224 | } 225 | val, err := parseType(x.Value) 226 | if err != nil { 227 | return nil, err 228 | } 229 | s.Type = &template.Map{ 230 | Key: key, 231 | Value: val, 232 | } 233 | return s, nil 234 | 235 | case *ast.StructType: 236 | str := &template.Struct{} 237 | str.Strict = flags.strict 238 | FIELDLOOP: 239 | for _, v := range x.Fields.List { 240 | typ, err := parseType(v.Type) 241 | if err != nil { 242 | log.WithError(err).Error("error parsing types") 243 | continue FIELDLOOP 244 | } 245 | 246 | fld := template.Field{} 247 | fld.Type = typ 248 | if v.Names == nil { 249 | // No names on a field means it is embedded 250 | jsonName := "" 251 | if v.Tag != nil { 252 | jsonName = strings.Split(template.GetTag("json", v.Tag.Value), ",")[0] 253 | } 254 | if jsonName == "" { 255 | str.Embedded = append(str.Embedded, strings.TrimSpace(string(bs[v.Type.Pos()-2:v.Type.End()-1]))) 256 | continue FIELDLOOP 257 | } else { 258 | // A hack to try and process an embedded field as a normal one 259 | fld.Name = jsonName 260 | } 261 | } else if v.Names[0] == nil { 262 | continue FIELDLOOP 263 | } else { 264 | fld.Name = v.Names[0].Name 265 | } 266 | 267 | if v.Doc != nil { 268 | fld.DocComment = strings.TrimSuffix(v.Doc.Text(), "\n") 269 | } 270 | if v.Comment != nil { 271 | fld.LineComment = strings.TrimSuffix(v.Comment.Text(), "\n") 272 | } 273 | 274 | if v.Tag != nil && strings.Contains(v.Tag.Value, "json:\"-\"") { 275 | // skip ignored json fields 276 | continue FIELDLOOP 277 | } 278 | 279 | // If no tag, still export -- it will still get parsed as json. 280 | // so use the name of the field. 281 | if v.Tag == nil { 282 | fld.Tag = v.Names[0].Name 283 | } else { 284 | fld.Tag = v.Tag.Value 285 | } 286 | 287 | str.Fields = append(str.Fields, fld) 288 | } 289 | s.Type = str 290 | return s, nil 291 | 292 | default: 293 | t := inspectNode(x) 294 | s.Type = &t 295 | return s, nil 296 | 297 | } 298 | } 299 | 300 | // parseType parses a non-package level type. 301 | func parseType(exp ast.Expr) (template.TypeSpec, error) { 302 | switch exp.(type) { 303 | case *ast.ChanType, *ast.FuncLit, *ast.FuncType: 304 | // Not supporting goofy things. 305 | return nil, errSkipType 306 | 307 | case *ast.InterfaceType: 308 | x, ok := exp.(*ast.InterfaceType) 309 | if !ok { 310 | return nil, errTypeAssert 311 | } 312 | // Empty interface should be the closes to "any" that we can 313 | // get in any language 314 | if x.Methods != nil && x.Methods.NumFields() == 0 { 315 | return &template.Basic{ 316 | Type: template.EmptyInterface, 317 | Pointer: true, 318 | }, nil 319 | } 320 | return nil, errSkipType 321 | 322 | case *ast.ArrayType: 323 | x, ok := exp.(*ast.ArrayType) 324 | if !ok { 325 | return nil, errTypeAssert 326 | } 327 | t, err := parseType(x.Elt) 328 | if err != nil { 329 | return nil, err 330 | } 331 | return &template.Array{Type: t}, nil 332 | 333 | case *ast.MapType: 334 | x, ok := exp.(*ast.MapType) 335 | if !ok { 336 | return nil, errTypeAssert 337 | } 338 | key, err := parseType(x.Key) 339 | if err != nil { 340 | return nil, err 341 | } 342 | val, err := parseType(x.Value) 343 | if err != nil { 344 | return nil, err 345 | } 346 | return &template.Map{ 347 | Key: key, 348 | Value: val, 349 | }, nil 350 | 351 | case *ast.StructType: 352 | // TODO: Check for structs that need to be parsed. 353 | // Example: time.Time should be "Date" in Flow. 354 | return &template.Basic{ 355 | Type: template.NestedStruct, 356 | Pointer: false, 357 | }, nil 358 | 359 | case *ast.BasicLit: 360 | x, ok := exp.(*ast.BasicLit) 361 | if !ok { 362 | return nil, errTypeAssert 363 | } 364 | return &template.Basic{ 365 | Type: x.Kind.String(), 366 | Pointer: false, 367 | }, nil 368 | 369 | default: 370 | t := inspectNode(exp) 371 | return &t, nil 372 | } 373 | } 374 | 375 | // inspectNode checks what is determined to be the value of a node based on a type-assertion. 376 | func inspectNode(node ast.Node) template.Basic { 377 | var t template.Basic 378 | ast.Inspect(node, func(n ast.Node) bool { 379 | switch y := n.(type) { 380 | case *ast.BasicLit: 381 | t.Type = y.Value 382 | case *ast.Ident: 383 | if t.Type == "" { 384 | t.Type = y.Name 385 | } else { 386 | // Selector expr: package.Type 387 | t.Type += "." + y.Name 388 | } 389 | if y.Obj != nil { 390 | 391 | } 392 | case *ast.StarExpr: 393 | t.Pointer = true 394 | } 395 | return true 396 | }) 397 | return t 398 | } 399 | 400 | // first word returns the first word of a string 401 | func firstWord(value string) string { 402 | for i := range value { 403 | if value[i] == ' ' { 404 | return value[0:i] 405 | } 406 | } 407 | return value 408 | } 409 | -------------------------------------------------------------------------------- /template/draw.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import ( 4 | "io" 5 | 6 | "sort" 7 | 8 | log "github.com/sirupsen/logrus" 9 | ) 10 | 11 | // Draw draws all types to a writer. 12 | func Draw(t map[string]*PackageType, out io.Writer, lang Language, verbose bool) (int, error) { 13 | if err := Header(out, lang); err != nil { 14 | return 0, err 15 | } 16 | 17 | keys := make([]string, 0, len(t)) 18 | for k := range t { 19 | keys = append(keys, k) 20 | } 21 | sort.Strings(keys) 22 | 23 | for _, k := range keys { 24 | v := t[k] 25 | if err := v.Template(out, lang); err != nil { 26 | return 0, err 27 | } 28 | if err := Raw(out, "\n"); err != nil && verbose { 29 | log.WithField("type", k).Warn("unable to create new line") 30 | } 31 | if verbose { 32 | log.Infof("created type: %s", k) 33 | } 34 | } 35 | return len(keys), nil 36 | } 37 | -------------------------------------------------------------------------------- /template/fragments.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import ( 4 | "text/template" 5 | ) 6 | 7 | // This file contains all the dynamic template fragments for each language. 8 | 9 | // language templates 10 | var templates = map[Language]langTemplates{ 11 | Elm: elmTemplates, 12 | Flow: flowTemplates, 13 | Typescript: tsTemplates, 14 | } 15 | 16 | type langTemplates struct { 17 | header string 18 | arrayOpen string 19 | arrayClose string 20 | arrayShortOpen string 21 | arrayShortClose string 22 | basic string 23 | fieldDocComment string 24 | declaration string 25 | fieldClose string 26 | lastFieldClose string 27 | fieldName string 28 | mapClose string 29 | mapKey string 30 | mapValue string 31 | structClose string 32 | structOpen string 33 | timeType string 34 | } 35 | 36 | // newTemplate returns the template string for a language and a string 37 | func newTemplate(tpl string) *template.Template { 38 | return template.Must(template.New("dummy"). 39 | Funcs(funcMap). 40 | Parse(tpl)) 41 | } 42 | 43 | var elmTemplates = langTemplates{ 44 | header: `-- Automatically generated by typewriter. Do not edit. 45 | -- http://www.github.com/natdm/typewriter 46 | 47 | `, 48 | arrayOpen: ` List`, 49 | arrayClose: ``, 50 | arrayShortOpen: ` List`, 51 | arrayShortClose: ``, 52 | basic: ` {{updateElmType .Type}}`, 53 | fieldDocComment: `{{elmMultilineComment .DocComment 1}}`, 54 | declaration: ` 55 | {{elmMultilineComment .Comment 0}}type alias {{.Name}} : `, 56 | fieldClose: `,{{elmComment .LineComment}} 57 | `, 58 | lastFieldClose: `{{elmComment .LineComment}} 59 | `, // Elm has no trailing comma support 60 | fieldName: ` {{.Name}} :`, 61 | mapClose: ``, 62 | mapKey: `Dict `, 63 | mapValue: ` `, 64 | structClose: `}`, 65 | structOpen: ` 66 | {`, 67 | timeType: "Date", 68 | } 69 | 70 | var flowTemplates = langTemplates{ 71 | header: `// @flow 72 | // Automatically generated by typewriter. Do not edit. 73 | // http://www.github.com/natdm/typewriter 74 | 75 | `, 76 | arrayOpen: `Array<`, 77 | arrayClose: `>`, 78 | arrayShortOpen: ``, 79 | arrayShortClose: `[]`, 80 | basic: `{{if .Pointer}}?{{end}}{{updateFlowType .Type}}`, 81 | fieldDocComment: `{{flowMultilineComment .DocComment 1}}`, 82 | declaration: ` 83 | {{flowMultilineComment .Comment 0}}export type {{.Name}} = `, 84 | fieldClose: `,{{flowComment .LineComment}} 85 | `, 86 | fieldName: ` {{.Name}}: `, 87 | mapClose: ` }`, 88 | mapKey: `{ [key: `, 89 | mapValue: `]: `, 90 | structClose: `{{ range .Embedded}}{{.}} & {{end}}{{if .Strict}}|}{{else}}}{{end}}`, 91 | structOpen: `{{"{"}}{{if .Strict}}|{{end}} 92 | `, 93 | timeType: "Date", 94 | } 95 | 96 | var tsTemplates = langTemplates{ 97 | header: `// Automatically generated by typewriter. Do not edit. 98 | // http://www.github.com/natdm/typewriter 99 | 100 | `, 101 | arrayOpen: `Array<`, 102 | arrayClose: `>`, 103 | arrayShortOpen: ``, 104 | arrayShortClose: `[]`, 105 | basic: `{{updateTSType .Type}}{{if .Pointer}} | undefined{{end}}`, 106 | fieldDocComment: `{{tsMultilineComment .DocComment 1}}`, 107 | declaration: ` 108 | {{tsMultilineComment .Comment 0}}type {{.Name}} = `, 109 | fieldClose: `,{{tsComment .LineComment}} 110 | `, 111 | fieldName: ` {{.Name}}{{if .Type.IsPointer}}?{{end}}: `, 112 | mapClose: ` }`, 113 | mapKey: `{ [key: `, 114 | mapValue: `]: `, 115 | structClose: `}`, 116 | structOpen: `{{ range .Embedded}}{{ . }} & {{end}}{ 117 | `, 118 | timeType: "Date", 119 | } 120 | -------------------------------------------------------------------------------- /template/funcmaps.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import ( 4 | "regexp" 5 | "strings" 6 | "text/template" 7 | ) 8 | 9 | // Language is a parsable language 10 | // stringer -type=Language 11 | type Language int 12 | 13 | // languages 14 | const ( 15 | Typescript Language = iota 16 | Flow 17 | Elm 18 | ) 19 | 20 | // custom types 21 | const ( 22 | EmptyInterface = "emptyIface" 23 | NestedStruct = "struct" 24 | TimeStruct = "Date" 25 | ) 26 | 27 | var funcMap = template.FuncMap{ 28 | "updateFlowType": updateTypes(conversions[Flow]), 29 | "updateElmType": updateTypes(conversions[Elm]), 30 | "updateTSType": updateTypes(conversions[Typescript]), 31 | "flowComment": lineComment("//"), 32 | "elmComment": lineComment("--"), 33 | "tsComment": lineComment("//"), 34 | "flowMultilineComment": multilineComment("//"), 35 | "elmMultilineComment": multilineComment("--"), 36 | "tsMultilineComment": multilineComment("//"), 37 | } 38 | 39 | const goInt = "int64|int32|int16|int8|int|uint64|uint32|uint16|uint8|uint|byte|rune" 40 | const goFloat = "float32|float64|complex64|complex128" 41 | const goNumbers = goInt + "|" + goFloat 42 | 43 | func asWord(baseRegex string) *regexp.Regexp { 44 | return regexp.MustCompile("\\b(" + baseRegex + ")\\b") 45 | } 46 | 47 | var conversions = map[Language]map[string]*regexp.Regexp{ 48 | Flow: map[string]*regexp.Regexp{ 49 | "any": asWord(EmptyInterface), 50 | "Object": asWord(NestedStruct), 51 | "Date": asWord(TimeStruct), 52 | "number": asWord(goNumbers), 53 | "boolean": asWord("bool"), 54 | }, 55 | Typescript: map[string]*regexp.Regexp{ 56 | "any": asWord(EmptyInterface), 57 | "object": asWord(NestedStruct), 58 | "Date": asWord(TimeStruct), 59 | "number": asWord(goNumbers), 60 | "boolean": asWord("bool"), 61 | }, 62 | Elm: map[string]*regexp.Regexp{ 63 | "string": asWord("string"), 64 | "Maybe": asWord(EmptyInterface + "|" + NestedStruct), 65 | "Date": asWord(TimeStruct), 66 | "Bool": asWord("bool"), 67 | "Int": asWord(goInt), 68 | "Float": asWord(goFloat), 69 | }, 70 | } 71 | 72 | // updateTypes takes a conversion slice and returns 73 | // a function used as a string replacer 74 | func updateTypes(replacements map[string]*regexp.Regexp) func(string) string { 75 | return func(s string) string { 76 | for target, regex := range replacements { 77 | s = regex.ReplaceAllString(s, target) 78 | } 79 | return s 80 | } 81 | } 82 | 83 | func multilineComment(prefix string) func(string, int) string { 84 | return func(c string, indent int) string { 85 | if c == "" { 86 | return c 87 | } 88 | lineStart := strings.Repeat("\t", indent) + prefix + " " 89 | return lineStart + strings.ReplaceAll(strings.Trim(c, "\n"), "\n", "\n"+lineStart) + "\n" 90 | } 91 | } 92 | 93 | func lineComment(prefix string) func(string) string { 94 | return func(c string) string { 95 | if c == "" { 96 | return c 97 | } 98 | return " " + prefix + " " + c 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /template/language_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -type=Language"; DO NOT EDIT 2 | 3 | package template 4 | 5 | import "fmt" 6 | 7 | const _Language_name = "TypescriptFlowElm" 8 | 9 | var _Language_index = [...]uint8{0, 10, 14, 17} 10 | 11 | func (i Language) String() string { 12 | if i < 0 || i >= Language(len(_Language_index)-1) { 13 | return fmt.Sprintf("Language(%d)", i) 14 | } 15 | return _Language_name[_Language_index[i]:_Language_index[i+1]] 16 | } 17 | -------------------------------------------------------------------------------- /template/template.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "fmt" 7 | "io" 8 | "regexp" 9 | "strconv" 10 | "strings" 11 | "text/template" 12 | 13 | log "github.com/sirupsen/logrus" 14 | ) 15 | 16 | // this file contains all the logic for creting types based on each language utilizing `fragments.go` within the template. 17 | 18 | // Templater interface is able to write a template to a writer, based on a Language 19 | type Templater interface { 20 | Template(w io.Writer, lang Language) error 21 | } 22 | 23 | type TypeSpec interface { 24 | Templater 25 | IsPointer() bool 26 | } 27 | 28 | var errNoType = errors.New("type not stored in package level type declaration") 29 | 30 | // Header is the file header 31 | func Header(w io.Writer, lang Language) error { 32 | return newTemplate(templates[lang].header).Execute(w, nil) 33 | } 34 | 35 | // Raw is a template with raw input in it 36 | func Raw(w io.Writer, raw string) error { 37 | tmpl, err := template.New("raw").Parse(raw) 38 | if err != nil { 39 | return err 40 | } 41 | return tmpl.Execute(w, nil) 42 | } 43 | 44 | // TimeType is specifically for Go's time.Time 45 | type TimeType struct { 46 | Name string 47 | Comment string 48 | Tag string 49 | } 50 | 51 | func (t *TimeType) Template(w io.Writer, lang Language) error { 52 | return newTemplate(templates[lang].timeType).Execute(w, t) 53 | } 54 | 55 | // PackageType is a package-level type. Any package type will 56 | // be templated with a full type creation statement and possibly a comment 57 | type PackageType struct { 58 | Name string 59 | Comment string 60 | Type Templater 61 | Tag string 62 | } 63 | 64 | func (t *PackageType) Template(w io.Writer, lang Language) error { 65 | if err := newTemplate(templates[lang].declaration).Execute(w, t); err != nil { 66 | return err 67 | } 68 | if t.Type == nil { 69 | log.WithError(errNoType).WithField("name", t.Name).Error("error while writing package type") 70 | return errNoType 71 | } 72 | 73 | return t.Type.Template(w, lang) 74 | } 75 | 76 | // Basic is a basic type. Ints, strings, bools, etc.. or a custom type. 77 | type Basic struct { 78 | Type string 79 | Pointer bool 80 | } 81 | 82 | func (t *Basic) Template(w io.Writer, lang Language) error { 83 | return newTemplate(templates[lang].basic).Execute(w, t) 84 | } 85 | 86 | func (t *Basic) IsPointer() bool { 87 | return t.Pointer 88 | } 89 | 90 | type Map struct { 91 | Key Templater 92 | Value Templater 93 | } 94 | 95 | func (t *Map) Template(w io.Writer, lang Language) error { 96 | if err := newTemplate(templates[lang].mapKey).Execute(w, t); err != nil { 97 | return err 98 | } 99 | if err := t.Key.Template(w, lang); err != nil { 100 | return err 101 | } 102 | if err := newTemplate(templates[lang].mapValue).Execute(w, t); err != nil { 103 | return err 104 | } 105 | 106 | if err := t.Value.Template(w, lang); err != nil { 107 | return err 108 | } 109 | return newTemplate(templates[lang].mapClose).Execute(w, t) 110 | } 111 | 112 | func (t *Map) IsPointer() bool { 113 | return false 114 | } 115 | 116 | // Array has a type 117 | type Array struct { 118 | Type Templater 119 | } 120 | 121 | var simpleType = regexp.MustCompile("^[a-zA-Z0-9_.]+$") 122 | 123 | func (t *Array) Template(w io.Writer, lang Language) error { 124 | buf := bytes.Buffer{} 125 | if err := t.Type.Template(&buf, lang); err != nil { 126 | return err 127 | } 128 | elemTypeAsBytes := buf.Bytes() 129 | 130 | open := templates[lang].arrayOpen 131 | close := templates[lang].arrayClose 132 | 133 | if simpleType.Find(elemTypeAsBytes) != nil { 134 | open = templates[lang].arrayShortOpen 135 | close = templates[lang].arrayShortClose 136 | } 137 | 138 | if err := newTemplate(open).Execute(w, t); err != nil { 139 | return err 140 | } 141 | if _, err := w.Write(elemTypeAsBytes); err != nil { 142 | return err 143 | } 144 | return newTemplate(close).Execute(w, t) 145 | } 146 | 147 | func (t *Array) IsPointer() bool { 148 | return false // TODO: track pointers to arrays 149 | } 150 | 151 | // Struct only has fields 152 | type Struct struct { 153 | Fields []Field 154 | 155 | // Strict is just for Flow types. 156 | Strict bool 157 | 158 | // Embedded are the embedded types for a struct 159 | Embedded []string 160 | } 161 | 162 | func (t *Struct) Template(w io.Writer, lang Language) error { 163 | if err := newTemplate(templates[lang].structOpen).Execute(w, t); err != nil { 164 | return err 165 | } 166 | for i, v := range t.Fields { 167 | if v.DocComment != "" { 168 | w.Write([]byte{'\n'}) 169 | if err := newTemplate(templates[lang].fieldDocComment).Execute(w, v); err != nil { 170 | return err 171 | } 172 | } 173 | if err := v.Template(w, lang); err != nil { 174 | return err 175 | } 176 | if i < len(t.Fields)-1 { 177 | if err := newTemplate(templates[lang].fieldClose).Execute(w, v); err != nil { 178 | return err 179 | } 180 | } else { 181 | tpl := templates[lang].lastFieldClose 182 | if tpl == "" { 183 | tpl = templates[lang].fieldClose 184 | } 185 | if err := newTemplate(tpl).Execute(w, v); err != nil { 186 | return err 187 | } 188 | } 189 | } 190 | return newTemplate(templates[lang].structClose).Execute(w, t) 191 | } 192 | 193 | // Field is a struct field 194 | type Field struct { 195 | Name string 196 | Type TypeSpec 197 | DocComment string 198 | LineComment string 199 | Tag string 200 | } 201 | 202 | func (t *Field) Template(w io.Writer, lang Language) error { 203 | jsonName := strings.Split(GetTag("json", t.Tag), ",")[0] 204 | if jsonName != "" { 205 | t.Name = jsonName 206 | } 207 | 208 | // Golang allows any valid JSON property name to be provided in the JSON tag. 209 | // Some aren't valid JS identifiers, so we want to quote them. 210 | switch lang { 211 | case Typescript, Flow: 212 | if propertyShouldBeQuoted(t.Name) { 213 | t.Name = fmt.Sprintf(`"%s"`, t.Name) 214 | } 215 | default: 216 | } 217 | 218 | basicType, isBasic := t.Type.(*Basic) 219 | 220 | if err := newTemplate(templates[lang].fieldName).Execute(w, t); err != nil { 221 | return err 222 | } 223 | 224 | if lang == Typescript && isBasic { 225 | // Special case for TS: top-level nullable type is written as 226 | // field?: T 227 | // but if that's the type parameter, it should become 228 | // T | undefined 229 | // So, we drop the Pointer flag for top-level types, since the field 230 | // already has "?" in it. 231 | t.Type = &Basic{ 232 | Type: basicType.Type, 233 | Pointer: false, 234 | } 235 | } 236 | 237 | // If there is an override type on the struct field 238 | override := strings.Split(GetTag("tw", t.Tag), ",") 239 | switch len(override) { 240 | case 2: 241 | ptr, err := strconv.ParseBool(string(override[1])) 242 | if err != nil { 243 | log.WithError(err).Errorf("error parsing bool for type %s", t.Name) 244 | } 245 | t.Type = &Basic{ 246 | Type: string(override[0]), 247 | Pointer: ptr, 248 | } 249 | case 1: 250 | if string(override[0]) != "" { 251 | t.Type = &Basic{ 252 | Type: string(override[0]), 253 | Pointer: false, 254 | } 255 | } 256 | } 257 | return t.Type.Template(w, lang) 258 | } 259 | 260 | func GetTag(tag string, tags string) string { 261 | loc := strings.Index(tags, fmt.Sprintf("%s:\"", tag)) 262 | if loc <= -1 { 263 | return "" 264 | } 265 | bs := []byte(tags) 266 | bs = bs[loc+len(tag)+2:] 267 | loc = strings.Index(string(bs), "\"") 268 | if loc == -1 { 269 | return "" 270 | } 271 | return string(bs[:loc]) 272 | } 273 | -------------------------------------------------------------------------------- /template/template_test.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import ( 4 | "bytes" 5 | "testing" 6 | 7 | "github.com/stretchr/testify/suite" 8 | ) 9 | 10 | type TemplateTestSuite struct { 11 | suite.Suite 12 | } 13 | 14 | func TestTemplateTestSuite(t *testing.T) { 15 | suite.Run(t, new(TemplateTestSuite)) 16 | } 17 | 18 | func (s *TemplateTestSuite) TestFlowTemplatePackageTypes() { 19 | p := &PackageType{ 20 | Name: "Maps", 21 | Comment: "... Comment\n", 22 | Type: &Struct{ 23 | Fields: []Field{ 24 | { 25 | Name: "MapStringMap", 26 | Type: &Map{Key: &Basic{"string", true}, Value: &Map{Key: &Basic{"string", true}, Value: &Basic{"string", true}}}, 27 | LineComment: "I am a map of strings and ints", 28 | Tag: `json:"map_string_map"`, 29 | }, { 30 | Name: "MapStringInts", 31 | Type: &Map{Key: &Basic{"string", false}, Value: &Array{Type: &Basic{"number", false}}}, 32 | LineComment: "I am a map of strings to a slice of ints", 33 | Tag: `json:"map_string_ints"`, 34 | }, 35 | }, 36 | }, 37 | } 38 | 39 | buf := new(bytes.Buffer) 40 | s.Require().NoError(p.Template(buf, Flow)) 41 | expected := ` 42 | // ... Comment 43 | export type Maps = { 44 | map_string_map: { [key: ?string]: { [key: ?string]: ?string } }, // I am a map of strings and ints 45 | map_string_ints: { [key: string]: number[] }, // I am a map of strings to a slice of ints 46 | }` 47 | s.EqualValues(expected, buf.String()) 48 | } 49 | 50 | func (s *TemplateTestSuite) TestFlowTemplateArrayOfMaps() { 51 | p := &PackageType{ 52 | Name: "Array", 53 | Comment: "... Comment\n", 54 | Type: &Array{ 55 | Type: &Map{ 56 | Key: &Basic{"int", false}, 57 | Value: &Basic{"string", true}, 58 | }, 59 | }, 60 | } 61 | 62 | buf := new(bytes.Buffer) 63 | s.Require().NoError(p.Template(buf, Flow)) 64 | expected := ` 65 | // ... Comment 66 | export type Array = Array<{ [key: number]: ?string }>` 67 | s.Equal(expected, buf.String()) 68 | } 69 | 70 | func (s *TemplateTestSuite) TestFlowTemplateArrayOfCustom() { 71 | p := &PackageType{ 72 | Name: "CustomTypeArray", 73 | Comment: "... Comment\n", 74 | Type: &Array{ 75 | Type: &Basic{ 76 | Type: "CustomType", 77 | Pointer: false, 78 | }, 79 | }, 80 | } 81 | 82 | buf := new(bytes.Buffer) 83 | s.Require().NoError(p.Template(buf, Flow)) 84 | expected := ` 85 | // ... Comment 86 | export type CustomTypeArray = CustomType[]` 87 | s.Equal(expected, buf.String()) 88 | } 89 | 90 | func (s *TemplateTestSuite) TestFlowTemplateArrayOfInts() { 91 | p := &PackageType{ 92 | Name: "Array", 93 | Comment: "... Comment\n", 94 | Type: &Array{ 95 | Type: &Basic{ 96 | Type: "int", 97 | Pointer: false, 98 | }, 99 | }, 100 | } 101 | 102 | buf := new(bytes.Buffer) 103 | s.Require().NoError(p.Template(buf, Flow)) 104 | expected := ` 105 | // ... Comment 106 | export type Array = number[]` 107 | s.Equal(expected, buf.String()) 108 | } 109 | 110 | func (s *TemplateTestSuite) TestFlowTemplateMapOfStringsInts() { 111 | p := &PackageType{ 112 | Name: "MapOfStringInts", 113 | Comment: "... Comment\n", 114 | Type: &Map{ 115 | Key: &Basic{"string", false}, 116 | Value: &Basic{"int", false}, 117 | }, 118 | } 119 | 120 | buf := new(bytes.Buffer) 121 | s.Require().NoError(p.Template(buf, Flow)) 122 | expected := ` 123 | // ... Comment 124 | export type MapOfStringInts = { [key: string]: number }` 125 | s.Equal(expected, buf.String()) 126 | } 127 | 128 | func (s *TemplateTestSuite) TestFlowInt() { 129 | p := &PackageType{ 130 | Name: "AliasToInt", 131 | Comment: "... Comment\n", 132 | Type: &Basic{"int", false}, 133 | } 134 | 135 | buf := new(bytes.Buffer) 136 | s.Require().NoError(p.Template(buf, Flow)) 137 | expected := ` 138 | // ... Comment 139 | export type AliasToInt = number` 140 | s.Equal(expected, buf.String()) 141 | } 142 | 143 | func (s *TemplateTestSuite) TestTime() { 144 | p := &PackageType{ 145 | Name: "TimeToDate", 146 | Comment: "... Comment\n", 147 | Type: &TimeType{"TestTime", "", ""}, 148 | } 149 | 150 | buf := new(bytes.Buffer) 151 | s.Require().NoError(p.Template(buf, Flow)) 152 | expected := ` 153 | // ... Comment 154 | export type TimeToDate = Date` 155 | s.Equal(expected, buf.String()) 156 | } 157 | -------------------------------------------------------------------------------- /template/valid_ecma.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import "regexp" 4 | 5 | // This file contains utilities for validating ECMAScript tokens prior to emitting them 6 | 7 | var ecmaReservedWords = map[string]struct{}{ 8 | "break": {}, 9 | "case": {}, 10 | "catch": {}, 11 | "class": {}, 12 | "const": {}, 13 | "continue": {}, 14 | "debugger": {}, 15 | "default": {}, 16 | "delete": {}, 17 | "do": {}, 18 | "else": {}, 19 | "export": {}, 20 | "extends": {}, 21 | "finally": {}, 22 | "for": {}, 23 | "function": {}, 24 | "if": {}, 25 | "import": {}, 26 | "in": {}, 27 | "instanceof": {}, 28 | "new": {}, 29 | "return": {}, 30 | "super": {}, 31 | "switch": {}, 32 | "this": {}, 33 | "throw": {}, 34 | "try": {}, 35 | "typeof": {}, 36 | "var": {}, 37 | "void": {}, 38 | "while": {}, 39 | "with": {}, 40 | "yield": {}, 41 | "enum": {}, 42 | "implements": {}, 43 | "interface": {}, 44 | "let": {}, 45 | "package": {}, 46 | "private": {}, 47 | "protected": {}, 48 | "public": {}, 49 | "static": {}, 50 | "abstract": {}, 51 | "boolean": {}, 52 | "byte": {}, 53 | "char": {}, 54 | "double": {}, 55 | "final": {}, 56 | "float": {}, 57 | "goto": {}, 58 | "int": {}, 59 | "long": {}, 60 | "native": {}, 61 | "short": {}, 62 | "synchronized": {}, 63 | "throws": {}, 64 | "transient": {}, 65 | "volatile": {}, 66 | "await": {}, 67 | } 68 | 69 | // Greedy regex that permits common valid identifiers like `$apply` or `_` 70 | // This quotes more than is strictly necessary, because unicode "letters" in identifier names 71 | // are valid. See: https://stackoverflow.com/questions/2008279/validate-a-javascript-function-name 72 | var validIdentifier = regexp.MustCompile(`^[$\w]+$`) 73 | 74 | /** 75 | * propertyShouldBeQuoted takes a name and returns true if it ought to be quoted as part of a 76 | * javascript or Typescript/Flow typedef property name. Those conditions are: 77 | * A - Property is a reserved word or future reserved word in ECMAScript as of Sep 2017 78 | * B - Property contains a non-word character (note, quotes some valid identifiers) 79 | */ 80 | func propertyShouldBeQuoted(name string) bool { 81 | _, isReservedWord := ecmaReservedWords[name] 82 | return isReservedWord || !validIdentifier.MatchString(name) 83 | } 84 | -------------------------------------------------------------------------------- /template/valid_ecma_test.go: -------------------------------------------------------------------------------- 1 | package template 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/suite" 7 | ) 8 | 9 | type ValidEcmaTestSuite struct { 10 | suite.Suite 11 | } 12 | 13 | func TestValidEcmaTestSuite(t *testing.T) { 14 | suite.Run(t, new(ValidEcmaTestSuite)) 15 | } 16 | 17 | func (s *ValidEcmaTestSuite) TestValidEcma() { 18 | s.True(propertyShouldBeQuoted("hello-world")) 19 | s.True(propertyShouldBeQuoted("hello#world")) 20 | s.True(propertyShouldBeQuoted("你好世界")) 21 | s.True(propertyShouldBeQuoted("hello/world")) 22 | s.False(propertyShouldBeQuoted("$helloWorld")) 23 | s.False(propertyShouldBeQuoted("helloWorld")) 24 | s.False(propertyShouldBeQuoted("hello_world")) 25 | } 26 | -------------------------------------------------------------------------------- /typewriter: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natdm/typewriter/fa88777820a5b77415785302be8e6aad54962f3d/typewriter --------------------------------------------------------------------------------