protoc-gen-go-gorm

command module
v0.0.0-...-fafdb79 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

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

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL