leebenson/conform

embedded struct pointer panic

zenpk opened this issue · 0 comments

zenpk commented

I always get a panic when my struct has the similar structure as below, here's a minimal example

package main

import (
	"github.com/leebenson/conform"
	"log"
)

type Child struct {
	ChildName string `conform:"trim"`
	Numbers   []uint64
}

type Parent struct {
	Child *Child
}

func main() {
	child := Child{
		ChildName: "   child   ",
		Numbers:   []uint64{1, 2, 3},
	}
	parent := Parent{
		Child: &child,
	}
	if err := conform.Strings(&parent); err != nil {
		log.Println(err)
	} else {
		log.Println(*parent.Child)
	}
}

output:

panic: reflect.Value.Convert: value of type string cannot be converted to type uint64

possible fix

conform.go:243

if elType.ConvertibleTo(reflect.TypeOf(str)) || elType.ConvertibleTo(reflect.TypeOf(&str)) {

change to

if elType == reflect.TypeOf(str) || elType == reflect.TypeOf(&str) {

output:

2022/12/06 22:11:56 {child [1 2 3]}

I might try to make a pr to fix it later, any help or explanation is appreciated, thanks in advance!