2022-12-13 14:34:46 +01:00
|
|
|
# protoc-gen-go-value-scan
|
|
|
|
|
|
|
|
Implements scanner and valuer interfaces for proto messages.
|
|
|
|
|
|
|
|
![README](./README.gif)
|
|
|
|
|
|
|
|
## What it does
|
|
|
|
|
|
|
|
Takes this proto
|
|
|
|
|
|
|
|
```proto
|
|
|
|
syntax = "proto3";
|
|
|
|
|
|
|
|
package foo.v1;
|
|
|
|
|
|
|
|
option go_package = "git.wobcom.de/smartmetering/protoc-gen-go-value-scan/test/go/foo/v1;foov1";
|
|
|
|
|
|
|
|
message Foo {
|
|
|
|
string bar = 1;
|
|
|
|
string zonk = 2;
|
|
|
|
}
|
2022-12-13 14:38:47 +01:00
|
|
|
|
|
|
|
message ExcludeThis {
|
|
|
|
string bar = 1;
|
|
|
|
string zonk = 2;
|
|
|
|
}
|
2022-12-13 14:34:46 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
and creates this go code
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Code generated by protoc-gen-go-value-scan. DO NOT EDIT.
|
|
|
|
|
|
|
|
package foov1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
protojson "google.golang.org/protobuf/encoding/protojson"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (x Foo) Value() (driver.Value, error) {
|
|
|
|
f := &x
|
|
|
|
|
|
|
|
opts := protojson.MarshalOptions{}
|
|
|
|
opts.UseProtoNames = false
|
|
|
|
opts.EmitUnpopulated = true
|
|
|
|
|
|
|
|
return opts.Marshal(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Foo) Scan(value interface{}) error {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case []byte:
|
|
|
|
return protojson.Unmarshal(v, x)
|
|
|
|
case string:
|
|
|
|
return protojson.Unmarshal([]byte(v), x)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("type assertion failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-12-13 14:38:47 +01:00
|
|
|
with this `buf.gen.yaml`
|
2022-12-13 14:34:46 +01:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
version: v1
|
|
|
|
plugins:
|
|
|
|
- name: go
|
|
|
|
out: go/
|
|
|
|
opt:
|
|
|
|
- paths=source_relative
|
|
|
|
- name: go-value-scan
|
|
|
|
out: go/
|
|
|
|
opt:
|
|
|
|
- paths=source_relative,use_proto_names=false,emit_unpopulated=true,exclude=git.wobcom.de/smartmetering/protoc-gen-go-value-scan/test/go/foo/v1.ExcludeThis
|
|
|
|
```
|
2022-12-13 14:38:47 +01:00
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
| Option | Meaning | Default |
|
|
|
|
| ------------------ | -------------------------------------------------------------------------------------- | ------- |
|
|
|
|
| `use_proto_names` | UseProtoNames uses proto field name instead of lowerCamelCase name in JSON field names | `true` |
|
|
|
|
| `emit_unpopulated` | EmitUnpopulated specifies whether to emit unpopulated fields | `true` |
|
|
|
|
| `exclude` | Takes a message identifier. Can be multiple identifier delimited by `,` | none |
|