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 ability to customize internal json marshaler #318

Merged
merged 1 commit into from May 20, 2021

Conversation

povsister
Copy link
Contributor

@povsister povsister commented May 17, 2021

Added a package level variable InterfaceMarshalFunc. It's used to marshal interface to JSON encoded byte slice, mostly when event.Interface("key", v) is called.

This enables user to replace the internal json marshaler at runtime, be noticed that using it improperly will cause potential data race.
e.g: Changing InterfaceMarshalFunc while lots of events are being processed.

Fixes #317

encoder.go Outdated Show resolved Hide resolved
// the functions of Encoder being invoked.
// DO REMEMBER to set this variable at importing, or
// you might get a nil pointer dereference panic at runtime.
var JSONMarshaler func(v interface{}) ([]byte, error)
Copy link
Owner

Choose a reason for hiding this comment

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

Why nor init this as var JSONMarshFunc = json.Marshal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it's an internal package, designed for non-independent usage. I'd like to keep the setting explicitly to avoid someone happens to left out the configuration. Also I think it makes the configuration clear and global once, no confusion will be made with the default marshal func.

Added a package level variable "InterfaceMarshalFunc".
It's used to marshal interface to JSON encoded byte slice,
mostly when event.Interface("key", v) is called.
@povsister povsister force-pushed the customized-interface-marshaler branch from 9eab6bb to aecc284 Compare May 20, 2021 03:03
@povsister
Copy link
Contributor Author

@rs Updated. PTAL

@rs rs merged commit 47a03bc into rs:master May 20, 2021
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.

Feature: customizd interface marshaler
2 participants