Comments (5)
I figured it out, for posterity's sake:
We're going with a combination of a type switch and:
var (
logger = logging.GetLogger("checker")
registry = make(map[string]reflect.Type)
)
func init() {
registry["HttpCheck"] = reflect.TypeOf(HttpCheck{})
}
func UnmarshalAny(any *Any) (interface{}, error) {
class := any.TypeUrl
bytes := any.Value
instance := reflect.New(registry[class]).Interface()
err := proto.Unmarshal(bytes, instance.(proto.Message))
if err != nil {
return nil, err
}
logger.Debug("instance: %v", instance)
return instance, nil
}
This could be made more elegant, but it works for now.
from protobuf.
For folks like me coming to this from gooling around, there's now helper functions at ptypes.MarshalAny and ptypes.UnmarshalAny
from protobuf.
An example:
from a .proto:
type TestCheckRequest struct {
MaxHosts int32 `protobuf:"varint,1,opt,name=max_hosts" json:"max_hosts,omitempty"`
Deadline *Timestamp `protobuf:"bytes,2,opt,name=deadline" json:"deadline,omitempty"`
CheckSpec *Any `protobuf:"bytes,3,opt,name=check_spec" json:"check_spec,omitempty"`
}
type HttpCheck struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
Path string `protobuf:"bytes,2,opt,name=path" json:"path,omitempty"`
Protocol string `protobuf:"bytes,3,opt,name=protocol" json:"protocol,omitempty"`
Port int32 `protobuf:"varint,4,opt,name=port" json:"port,omitempty"`
Verb string `protobuf:"bytes,5,opt,name=verb" json:"verb,omitempty"`
Target *Target `protobuf:"bytes,6,opt,name=target" json:"target,omitempty"`
Headers []*Header `protobuf:"bytes,7,rep,name=headers" json:"headers,omitempty"`
Body string `protobuf:"bytes,8,opt,name=body" json:"body,omitempty"`
}
I would put an HttpCheck object in the CheckSpec field on a TestCheckRequest message.
Unmarshaling this has proven difficult.
from protobuf.
You can use google/protobuf/any.proto
:
https://github.com/chai2010/notepadplus-protobuf/blob/master/testdata/googleapis/google/protobuf/any.proto
from protobuf.
Yeah. I'm just not going to use Any.
from protobuf.
Related Issues (20)
- go_features.proto has incorrect package and error-prone file path HOT 5
- Tracking issue: Fully Lazy Extension decoding HOT 7
- feature: Generate test client / `bufconn` constructors HOT 4
- v1.33.0 breaks bazel build HOT 14
- Reflection: protopath parsing and path+message access HOT 7
- Proposal: Set go.mod Go language version to oldest supported Go version (1.21 currently) HOT 8
- Proposal: Add `Override` mode to `proto.Merge` HOT 2
- editions: maps are incorrectly serialized if file has default message encoding of DELIMITED HOT 8
- protodesc: too strict about proto3 field names when creating descriptors from FileDescriptorProto
- undefined: descriptorpb.Default_FileOptions_PhpGenericServices HOT 2
- `ProtoReflect().GetUnknown()` Not Working HOT 4
- Editions language feature: (pb.go).struct_tag HOT 11
- encoding/protojson: handling google.protobuf.Empty HOT 15
- Make M option overwrite `option go_package` in the `.proto` file (as opposed to return an error) HOT 5
- related to #1054 and similar "plugin output is unparseable" errors HOT 4
- Random 0xfeff / ZERO WIDTH NO-BREAK SPACE being added to returned string values HOT 3
- custom options doesn't seem to work for protobuf map fields HOT 2
- Can protobuf provide tags for customized message generated fields? HOT 2
- encoding/protojson: unmarshalBytes assumes no padding if byte string length is not divisible by 4 HOT 27
- Service crash caused by encoding HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf.