Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added support of unmarshalling and marshalling of pointers #32

Merged

Conversation

danielbairstow97
Copy link
Contributor

During development of my own project I found some issues when trying to marshal php Nulls N into any Go struct fields.

Was seeing uncaught panics such as reflect: call of reflect.Value.Set on zero Value if the value trying to be set on a struct field was Null.
Additionally if a value was being set on a struct field of type reflect.Ptr that was nil a panic would occur when calling the .Set method on the struct field e.g. reflect.Set: value of type string is not assignable to type *string

I've added testing to showcase the now supported functionality:

  • When serializing if a pointer value is Nil the MarshalNil function will be called
  • When unserializing if the value passed in is a nil interface the struct field will be left unset (set as default value) rather then panic
  • If unmarshalling to a pointer struct field, the field will first be instantiated before attempting to set the value of the pointer

I used this Go playground to try base behaviour off of the encoding/json package: https://go.dev/play/p/5td0XqinlIP

@elliotchance elliotchance merged commit 9905247 into elliotchance:master Jan 31, 2024
@elliotchance
Copy link
Owner

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants