Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom type convert rules #123

Closed
fuguohong opened this issue Jan 4, 2022 · 2 comments
Closed

custom type convert rules #123

fuguohong opened this issue Jan 4, 2022 · 2 comments
Assignees

Comments

@fuguohong
Copy link

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

@tomdevelops
Copy link
Contributor

tomdevelops commented Jan 19, 2022

hey @fuguohong, the PR #115 was merged today 🥳. 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

@uded
Copy link
Collaborator

uded commented Jan 6, 2023

Seems to be address by #115

@uded uded closed this as completed Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants