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

Shouldn't the slices.Map receiver for UnmarshalJSON be a pointer? #654

Closed
Tracked by #770
naemono opened this issue Jun 11, 2021 · 0 comments · Fixed by #655
Closed
Tracked by #770

Shouldn't the slices.Map receiver for UnmarshalJSON be a pointer? #654

naemono opened this issue Jun 11, 2021 · 0 comments · Fixed by #655
Assignees
Labels
bug Something isn't working s: fixed was fixed or solution offered
Milestone

Comments

@naemono
Copy link
Contributor

naemono commented Jun 11, 2021

Description

It seems as though unmarshaling to a struct that contains a slices.Map doesn't work as expected, and the only solution is to adjust the receiver for Map.UnmarshalJSON to be a pointer, as below shows

package main

import (
	"encoding/json"
	"log"

	"github.com/gobuffalo/pop/v5/slices"
)

type Account struct {
	Credentials slices.Map `json:"credentials" db:"credentials"`
}

func main() {
	m := map[string]interface{}{
		"credentials": map[string]interface{}{
			"test": "data",
		},
	}
	b, err := json.Marshal(&m)
	account := &Account{}
	json.Unmarshal(b, account)
	log.Printf("err: %v, account: %v", err, account)
}

you'll see this

$ go run map.go
2021/06/11 15:24:13 err: <nil>, account: &{map[]}

If I update UnmarshalJSON to be

func (m *Map) UnmarshalJSON(b []byte) error {
	var stuff map[string]interface{}
	err := json.Unmarshal(b, &stuff)
	if err != nil {
		return err
	}
	if *m == nil {
		*m = Map{}
	}
	for key, value := range stuff {
		(*m)[key] = value
	}
	return nil
}

like here https://github.com/naemono/pop/tree/map-unmarshal-pointer-receiver

$ go mod edit -replace github.com/gobuffalo/pop/v5=github.com/naemono/pop/v5@map-unmarshal-pointer-receiver                                                                                                                                                        
$ go run map.go
go: downloading github.com/naemono/pop/v5 v5.3.2-0.20210611203100-4a801ab183ef
2021/06/11 15:38:05 err: <nil>, account: &{map[test:data]}

I'll open a PR shortly referencing this issue

Steps to Reproduce the Problem

See above

Expected Behavior

I'd expect the map data to be contained within the slices.Map object with a struct during json.Unmarshal

Actual Behavior

map is empty

Info

If it's relevant

$ go version
go version go1.15 darwin/amd64
@sio4 sio4 closed this as completed in #655 Sep 18, 2022
@sio4 sio4 self-assigned this Sep 18, 2022
@sio4 sio4 added bug Something isn't working s: fixed was fixed or solution offered labels Sep 18, 2022
@sio4 sio4 added this to the v6.0.7 milestone Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working s: fixed was fixed or solution offered
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants