Skip to content

accept custom encoder for payload#135

Merged
ripienaar merged 2 commits intochoria-io:mainfrom
msaboor35:feat/custom-payload-encoder
May 28, 2025
Merged

accept custom encoder for payload#135
ripienaar merged 2 commits intochoria-io:mainfrom
msaboor35:feat/custom-payload-encoder

Conversation

@msaboor35
Copy link
Contributor

A Custom Task Payload Encoder can be provided using TaskPayloadEncoder TaskOpt. The default behaviour of json.Marshal is kept, if TaskPayloadEncoder is not provided. Only the first provided encoder is used to marshal, rest are discarded, because it does not make sense that the user would want to encode same payload multiple times with no effects. This behaviour is documented.

I did not proceed with a separate API mainly due to the need of duplicate logic of creating new task, which would result in extracting all the code of NewTask in a non-exported function, except the json.Marshal(payload) part.

task.go Outdated
rawPayload: payload,
}

opts = append(opts, TaskPayloadEncoder(json.Marshal))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a bit magical and hard to decipher.

I think it would be clearer if Task had a unexpected key marshaller that held a TaskPayloadEncoderFunc - and defaults to the json one.

This way we can have the option just set the marshaller and then call the marshaller here.

You can remove the magical "first marshaller is used" thing by just checking its nil before setting it and erroring when not nil, thats a bit more explicit.

Finally if its nil after opts parsing then set json one and use it after.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this approach, users can provide multiple TaskPayloadEncoderFunc options without causing multiple encodings, since these options only set the encoder function during task creation. The actual encoding happens once, after all options are processed. This means we can drop the current restriction that only the first encoder is used and instead follow the more common pattern where the last option set takes precedence.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thats what I ment. But even better since you can error if more than one marshaller is given if you check the marshaller is nil before setting it.

You can remove the magical "first marshaller is used" thing by just checking its nil before setting it and erroring when not nil, thats a bit more explicit.

Copy link
Member

@ripienaar ripienaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, this looks good thank you

@ripienaar ripienaar merged commit f1ba225 into choria-io:main May 28, 2025
2 of 3 checks passed
@msaboor35 msaboor35 deleted the feat/custom-payload-encoder branch May 28, 2025 16:05
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

Successfully merging this pull request may close these issues.

2 participants