Skip to content

pointer bug #11

Description

@anpic
type A struct {
	BS []B
}

type B struct {
	C
}
	
type C struct {
	Index int
}

func (c C) String() string {
	return fmt.Sprint("index", c.Index)
}

func main() {
	mapper := mapify.Mapper{}
	mapper.MapValue = func(path string, e mapify.Element) (any, error) {
		fmt.Println("path:", path)
		return e.Value.Interface(), nil
	}
	a := A{}
	a.BS = []B{B{C{Index:1}},B{C{Index:2}}}
	fmt.Println(mapper.MapAny(a))
}
path: .BS
path: .BS[0].C
path: .BS[0].C.Index
path: .BS[1].C
path: .BS[1].C.Index
map[BS:[map[C:map[Index:1]] map[C:map[Index:2]]]] <nil>

but

type A struct {
	BS []*B
}

type B struct {
	C
}
	
type C struct {
	Index int
}

func (c C) String() string {
	return fmt.Sprint("index", c.Index)
}

func main() {
	mapper := mapify.Mapper{}
	mapper.MapValue = func(path string, e mapify.Element) (any, error) {
		fmt.Println("path:", path)
		return e.Value.Interface(), nil
	}
	a := A{}
	a.BS = []*B{&B{C{Index:1}},&B{C{Index:2}}}
	fmt.Println(mapper.MapAny(a))
}
path: .BS
map[BS:[index1 index2]] <nil>

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions