Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protobuf3 support for "Any" #60

Closed
grepory opened this issue Aug 11, 2015 · 5 comments
Closed

protobuf3 support for "Any" #60

grepory opened this issue Aug 11, 2015 · 5 comments

Comments

@grepory
Copy link

grepory commented Aug 11, 2015

Is there an idiomatic way to support Any's in messages in Go? I have been struggling with reflect for a couple of days, and I think if I'm having this much trouble, I must be doing it wrong.

@grepory
Copy link
Author

grepory commented Aug 11, 2015

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.

@chai2010
Copy link

@grepory
Copy link
Author

grepory commented Aug 11, 2015

Yeah. I'm just not going to use Any.

@grepory grepory closed this as completed Aug 11, 2015
@grepory
Copy link
Author

grepory commented Aug 11, 2015

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.

@jmhodges
Copy link
Contributor

For folks like me coming to this from gooling around, there's now helper functions at ptypes.MarshalAny and ptypes.UnmarshalAny

@golang golang locked as resolved and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants