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

Pass struct as result value? #70

Closed
pgruenbacher opened this issue Mar 14, 2016 · 3 comments
Closed

Pass struct as result value? #70

pgruenbacher opened this issue Mar 14, 2016 · 3 comments

Comments

@pgruenbacher
Copy link

Is there an opportunity for a task to return a struct or map value in order to pass more information. For instance if I'm doing file processing tasks, I'd like to convey information like filepaths, sizes, stats, etc. Currently I'm just writing the data to a json file and sending back a string value of the config filepath as a result. Is it problematic for passing such a struct value to chained tasks?

@RichardKnop
Copy link
Owner

Structs are difficult as all return values must be JSON serializable. Maps might be easier to support but only restricted to maps that can be represented as a valid JSON object. So the implementation might be a bit trickier.

@pgruenbacher
Copy link
Author

Actually what I can do is just serialize the structs I need into strings manually myself using the encoding/json package for the time being. It seems similar questions are asked with Redis and the response is to serialize manually. http://stackoverflow.com/questions/27468458/go-how-to-save-and-retrieve-a-struct-to-redis-using-redigo

@RichardKnop
Copy link
Owner

This sounds like a bad design. Any tasks exposed via machinery should be quite simple and complex logic (methods that return, accept structs) should be private and called from the simple exposed public tasks.

What I would probably do is have some sort of a Result struct, which has a corresponding table in your database. Just return the primary ID of result object. And then whatever code where you are handling result values should just take the primary key and fetch the object from the database.

I would suggest something similar for input arguments, just passing primary keys as arguments and fetching from database / creating structs inside the function. This should keep the public API clean and simple. Bulk of the business logic can be inside private functions / methods.

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