Created
May 1, 2017 20:14
-
-
Save weivall/a6d9604f523987a96944aa48b983cdb5 to your computer and use it in GitHub Desktop.
protobuf Any example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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