jinzhu / copier

Copier for golang, copy value from struct to struct and more
MIT License
5.52k stars 485 forks source link

Custom function for merging fields #194

Open dcrystalj opened 1 year ago

dcrystalj commented 1 year ago

Describe the feature

Ability to merge/join multiple fields into one.

I would like to have option to add custom rules for copying fields. Lets say we have date and time separate fields. I would like to copy them as merged.

type From struct {
  date string
  time string
}

type To struct {
  datetime time.Time
}

func (t *To) FromDateTime(f *From) error {
  x := //... logic to merge and convert datetime here
  t.datetime = x
}

Motivation

Feels like this would be possible already if parameter to receiving function would get the reference to From struct.

func (t *To) date(d string) error {
   // missing access to "time"
  t.datetime = x
}

Related Issues