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

Add the ability to provide a struct to data handlers which passes data through before returning the message #11

Open
jpwilliams opened this issue Feb 6, 2017 · 1 comment
Assignees
Milestone

Comments

@jpwilliams
Copy link
Owner

jpwilliams commented Feb 6, 2017

Currently, to get more structured data out of a message, we need to do something like:

type IncomingData struct {
	GivenId  string `bson:"id"`
	TargetId string `bson:"target"`
}

data := new(IncomingData)
b, _ := bson.Marshal(event.Data)
bson.Unmarshal(b, &data)

If we could get this into adding data handlers to do this automatically, that'd be awesome, seeing as it'll be a very common task.

Google's github.com/google/jsonapi would be a good one to go for, though need to figure out how it works.

Currently, as above, most endpoints used for our systems utilise gopkg.in/mgo.v2/bson.

@jpwilliams jpwilliams self-assigned this Feb 6, 2017
@jpwilliams jpwilliams added this to the 1.0.0 milestone Feb 6, 2017
@jpwilliams
Copy link
Owner Author

Maybe we could provide a set remit.Parser function that returns an EventDataHandler?
We'd have to add something like a Custom interface{} on Events that we can tap into, then:

package remit

import (
	"fmt"

	"gopkg.in/mgo.v2/bson"
)

func Parser(s interface{}) EventDataHandler {
	return func(e Event) {
		data := new(s)
		b, err := bson.Unmarshal(e.Data)
		if err != nil {
			e.Failure <- err
			return
		}
		bson.Unmarshal(b, &data)
		e.Custom = data
		e.Next <- true
	}
}

This means you could use it like so:

endpoint := remitSession.createEndpoint("math.sum")
endpoint.OnData(remit.Parser(IncomingData), addNumbers)

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

No branches or pull requests

1 participant