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

Event payload to protobuf code #77

Open
maguro opened this issue May 23, 2019 · 4 comments
Open

Event payload to protobuf code #77

maguro opened this issue May 23, 2019 · 4 comments

Comments

@maguro
Copy link
Contributor

maguro commented May 23, 2019

We have some code that translates the GitLab Golang event payloads to their protobuf equivalent. Would you be interested in including it?

@deankarn
Copy link
Collaborator

I would definitely be interested in including the option for sure, send me some examples, references ect. and we'll see how it could be integrated :)

@maguro
Copy link
Contributor Author

maguro commented May 24, 2019

Here's a rough sketch of what I've been using

type converter func(payload interface{}) (proto.Message, error)

var converters map[reflect.Type]converter

func init() {
    converters = make(map[reflect.Type]converter)

    converters[reflect.TypeOf(gitlab.IssueEventPayload{})] = func(payload interface{}) (proto.Message, error) {
        issue := payload.(gitlab.IssueEventPayload)
        return NewIssueEventPayload(issue)
    }
}

func ToMessage(payload interface{}) (proto.Message, error) {
    pType := reflect.TypeOf(payload)
    convert, ok := converters[pType]
    if !ok {
        return nil, errors.Errorf("Unable to find converter for %s", pType)
    }
    return convert(payload)
}

@maguro
Copy link
Contributor Author

maguro commented Jun 1, 2019

I'll assume that this is ok.

@maguro
Copy link
Contributor Author

maguro commented Jun 3, 2019

I'm also adding a NewMessage() function to facilitate the deserialization of messages serialized in Any:

func NewMessage(typeUrl string) (proto.Message, error) {
	mType, ok := anyTypes[typeUrl]
	if !ok {
		return nil, errors.Errorf("Unable to find Message constructor for type URL %s", typeUrl)
	}
	return reflect.New(mType).Interface().(proto.Message), nil
}

The resulting instance could then be used with ptypes.UnmarshalAny().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants