Skip to content

custom type convert rules #123

@fuguohong

Description

@fuguohong

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions