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

Allow unmarshalling slices.Map to nil map #417

Closed
ThePrzeglo opened this issue Jul 22, 2019 · 0 comments · Fixed by #609
Closed

Allow unmarshalling slices.Map to nil map #417

ThePrzeglo opened this issue Jul 22, 2019 · 0 comments · Fixed by #609

Comments

@ThePrzeglo
Copy link

I've tried to bind from JSON request struct:

cmt := Comment{
    // Data: slices.Map{}, // after uncomment works
}
c.Bind(&cmt)
type Comment struct {
	ID        uuid.UUID  `json:"id" db:"id"`
	CreatedAt time.Time  `json:"created_at" db:"created_at"`
	UpdatedAt time.Time  `json:"updated_at" db:"updated_at"`
	Data      slices.Map `json:"data" db:"data"`
}

And it turned out that it crashes on slices.Map on line 51 with assignment to entry in nil map
https://github.com/gobuffalo/pop/blob/master/slices/map.go#L51
Small change to UnmarshalJSON seems to fix issue:

func (m *Map) UnmarshalJSON(b []byte) error { // ptr here
	var stuff map[string]interface{}
	err := json.Unmarshal(b, &stuff)
	if err != nil {
		return err
	}
	if *m == nil { // if map that this pointer points to is nil:
		*m = Map{} //  allocate new one
	}
	for key, value := range stuff {
		(*m)[key] = value
	}
	return nil
}
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 a pull request may close this issue.

2 participants