copier icon indicating copy to clipboard operation
copier copied to clipboard

custom type convert rules

Open fuguohong opened this issue 2 years ago • 1 comments

Describe the feature

Custom type conversion rules to allow different types of conversion example:

type User struct {
	CreateAt time.Time
	BirthDay time.Time
}

type Person struct {
	CreateAt uint64
	BirthDay string
}

func TestTimeToString(t *testing.T) {
	RegisterConverter(reflect.TypeOf(""), reflect.TypeOf(time.Time{}), func(v interface{}) interface{} {
		t := v.(time.Time)
		return t.Format(time.RFC3339)
	})

	from := &User{
		BirthDay: time.Unix(123456, 0),
	}
	to := &Person{}
	Copy(to, from)
	if to.BirthDay != "1970-01-02T18:17:36+08:00" {
		t.Error("time to string copy failed")
	}
}

Motivation

Related Issues

fuguohong avatar Jan 04 '22 03:01 fuguohong

hey @fuguohong, the PR #115 was merged today :partying_face:. With it, you can make these kinds of type conversions via a converter option. Have a look into the PR for an example. cheers tom

tomdevelops avatar Jan 19 '22 16:01 tomdevelops

Seems to be address by #115

uded avatar Jan 06 '23 13:01 uded