Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save weivall/a6d9604f523987a96944aa48b983cdb5 to your computer and use it in GitHub Desktop.
Save weivall/a6d9604f523987a96944aa48b983cdb5 to your computer and use it in GitHub Desktop.
protobuf Any example
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment