ruiaylin / goprotobuf

Automatically exported from code.google.com/p/goprotobuf
Other
0 stars 0 forks source link

Getters that return a pointer for fields. #58

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have some protobufs that share common fields. A simple example is

message A {
  optional string name = 1;
  ...
}
message B {
  optional string name = 1;
  ...
}

I can write interfaces for these and write code to do quite a bit.

type i interface {
    GetName() string
}

However there is no way to mutate name through the interface. There are 
different hacks that one might do here such as move name into another shared 
proto but that isn't always possible and isn't a complete solution either.

I propose something like GetPtrName() that returns a pointer to the field in 
the struct. For message types it would return a pointer to a pointer. If the 
containing message is nil the returned pointer would be nil.

Original issue reported on code.google.com by mk...@google.com on 7 Nov 2014 at 9:10

GoogleCodeExporter commented 9 years ago
We aren't going to add any more per-field methods; if anything we will be 
moving in the opposite direction.

In your case, you can either write a type switch, or use reflection.

Original comment by dsymo...@golang.org on 7 Nov 2014 at 10:09

GoogleCodeExporter commented 9 years ago
> you can either write a type switch, or use reflection

Both of which feel like hacks but I guess reflection will do.

Original comment by mk...@google.com on 7 Nov 2014 at 10:14