protoc-gen-go-gorm
Protoc plugin to generate gorm models and helper functions.
Generation
We've opted to use go text templates for generation because we believe it's easier to understand holistically and for newcomers than using lots of P() calls. The template file is plugin/gorm_template.go
Default behavior
Id fields are auto generated by the database using uuid generation functions
Timestamp fields are automatically handled by the database using now()
Json tags are generated using the json name so that json marshalling will always be the same on the models as on the generated protos
Model Types
Fields of type google.protobuf.Timestamp
are generated as *time.Time
and the generated gorm tag is gorm:"default:now()
Fields named Id
in any case are treated as primary keys and the generated gorm tag is gorm:"primaryKey;"
Primitive repeated types use their pq
type: pq.Float64Array
, pq.Float32Array
, pq.Int32Array
, pq.Int64Array
, pq.StringArray
, pq.ByteaArray
Fields marked as optional
become pointers
All fields of type message
become pointers. This is because all golang fields of type message
are pointers
Supported Proto Types
Not all proto types are supported yet. Support for less frequently used types will be added as it is needed. The following proto types are supported
- bool
- enum
- int32
- int64
- float
- double
- string
- bytes
- message
Tests
Tests use gnomock to run a database, then use gorm's auto migrate feature to create the schema, then exercise the generated types and functions against the database.
Test data is populated gofakeit using struct tags injected into the generated files using the protoc-go-inject-tag post processor
To run the tests run make build-example && cd test && go test