// agent.proto
message Agent {
Permission permission = 1;
google.protobuf.Timestamp born_time = 2;
message Permission {
Type type = 1;
enum Type {
KILLNONE = 0;
KILLALL = 1;
DANCE = 2;
}
}
}
Then scanning SQL row into agent protobuf struct:
// main.go
var a proto.Agent
.....
... row.Scan(&a.Permission.Type,...)
That permission type is stored as simple MariaDB INT() value = 0
for default type. So, i can't scan it directly. So i made temp struct where Type int32
and scaned row into that temp struct after what tried to map temp struct fields to protobuf struct, but with no luck. Similar issues i had when want to scan MariaDB string values into []byte type fields but i solved that with my temp struct []byte(tmp.UUID)
.
What is common patterns to scan database ROW (single row) into protubuf message when non-standard protobuf field types are used?
EDIT: Does there should be some additional 0
value handling?
I usually work with Go types within the business domain, and use adapters to convert to/from the protobuf type.
// Role represents a set of permissions
type Role struct {
KILLNONE = iota
KILLALL
DANCE
}
// Permission represents a set of agent permissions
type Permission struct {
Role Role
}
// ToProto converts a Permission Go type to a protobuf Permission
func (p Permission) ToProto() (proto.Permission) {
pb := proto.Permission{}
// assign p's properties to proto's respective properties
// with necessary type conversions.
...
return pb
}
The protobuf examples usually show working with the protobuf types directly, but it seems like adapters are more common in the field.