go-model
go-model copied to clipboard
Feature Request: Copy Arrays/Splices
Great project! It would be nice if go-model could copy between arrays.
I believe I have added array/slice support long ago. Are you facing any issue? please describe your issue in details.
Given the following program:
package main
import (
"fmt"
mmapper "gopkg.in/jeevatkm/go-model.v0"
)
type A struct {
Name string
}
type B struct {
Name string
}
func main() {
aRay := []A{A{Name: "Bob"}, A{Name: "Jane"}}
bRay := []B{}
errs := mmapper.Copy(bRay, aRay)
if len(errs) > 0 {
panic(errs[0])
}
fmt.Printf("bRay:%#v", bRay)
}
Errors with:
panic: Source or Destination is not a struct
Now I get it, you mean slice of struct.
Currently library is capable of manipulatingstruct
. In other words you would like to propose variable to variable copy (binding).
I might need time to think of design.
Thanks! It doesn't necessarily need to go in .Copy and could be a separate method since this could have a performance impact with doing the reflection check for array. In the meantime we'll have a utility method to help us out :thumbsup:
I'm thinking of method called Bind
for variables. If you have implementation please send PR. We can improving it from there.
+1
func ExampleMap_10() { type Aa struct { Name string }
type A struct {
Name string
Cs Aa
}
type Bb struct {
Name string
}
type B struct {
Name string
Cs Bb
}
a:=&A{
Name:"A",
Cs:Aa{
Name:"Aa",
},
}
b:=&B{ }
model.Copy( b,a)
fmt.Println(b.Cs.Name)
// output:Aa
}
func ExampleMap_11() { type Aa struct { Name string }
type A struct {
Name string
Cs []Aa
}
type Bb struct {
Name string
}
type B struct {
Name string
Cs []Bb
}
a:=&A{
Name:"A",
Cs:[]Aa{
Aa{Name:"Aa"},
},
}
b:=&B{ }
model.Copy( b,a)
fmt.Println(b.Cs)
// output:Aa
}
https://github.com/stroiman/go-automapper
@kingreatwill I believe you were trying to giving a reference for this feature implementation, correct?