Generate sql.Scanner and driver.Valuer implementations for your Protobufs.
We want the generated struct for this Person message to implement sql.Scanner and driver.Valuer so we can easily write and read it as JSON from Postgres.
So we compile the person.proto file:
syntax = "proto3";
import "github.com/travisjeffery/proto-go-sql/sql.proto";
message Person {
option (sql.all) = "json";
string id = 1;
}
And run:
$ protoc --sql_out=. person.proto
Generating this person_sql.go:
func (t *Person) Scan(val interface{}) error {
return json.Unmarshal(val.([]byte), t)
}
func (t *Person) Value() (driver.Value, error) {
return json.Marshal(t)
}
And we're done!
MIT
- travisjeffery.com
- GitHub @travisjeffery
- Twitter @travisjeffery
- Medium @travisjeffery