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 nil aware encoder #299

Open
vmihailenco opened this issue Mar 16, 2021 · 2 comments
Open

Add nil aware encoder #299

vmihailenco opened this issue Mar 16, 2021 · 2 comments

Comments

@vmihailenco
Copy link
Owner

No description provided.

@tmm1
Copy link
Contributor

tmm1 commented May 12, 2021

@vmihailenco could you expand on what you're thinking here?

@vmihailenco
Copy link
Owner Author

vmihailenco commented May 13, 2021

@tmm1 I am looking into implementing a function decorator that does common nil checks (ideally during encoder initialization). See nilAwareDecoder and this commit that introduced that change.

Should be something like

func nilAwareEncoder(typ reflect.Type, fn encoderFunc) encoderFunc {
	if nilable(typ.Kind()) {
		return func(d *Decoder, v reflect.Value) error {
			if v.IsNil() {
				return e.EncodeNil()
			}
			return fn(d, v)
		}
	}
	return fn
}

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