Skip to content

pointer bug #11

Open
Open

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

  1. elgopher commented on Jan 15, 2026

    @elgopher
    Owner

    thank you @anpic for reporting a bug! Somehow I missed the notification about it. I will look at the issue in the evening.

  2. elgopher commented on Jan 15, 2026

    @elgopher
    Owner

    @anpic I confirm, that mapping of slices of pointers is not implemented at all. Thank you for finding this out! The reason why this is not implemented is pretty simple - I don't use such slices, so I never wanted such feature :) But I admit, that some people may want to use them and that this feature should be implemented.

    I've created a PR with the fix: #12

    So far, in the PR, I have added support for slices of pointers to structs. But there are other types of pointers like []*map or []*[]string, which should be supported as well, I guess. It will take some time to finish the implementation, so I marked the PR as a draft (work in progress). I will let you know when it is finished.

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