travisjeffery / proto-go-sql

Generate SQL Scanner and Valuer implementations for your Protobufs.

Home Page:https://twitter.com/travisjeffery

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proto-go-sql

Generate sql.Scanner and driver.Valuer implementations for your Protobufs.

Example

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!

License

MIT


About

Generate SQL Scanner and Valuer implementations for your Protobufs.

https://twitter.com/travisjeffery


Languages

Language:Go 96.3%Language:Makefile 3.7%