go-model icon indicating copy to clipboard operation
go-model copied to clipboard

Feature Request: Copy Arrays/Splices

Open kdeenanauth opened this issue 7 years ago • 10 comments

Great project! It would be nice if go-model could copy between arrays.

kdeenanauth avatar Aug 22 '16 19:08 kdeenanauth

I believe I have added array/slice support long ago. Are you facing any issue? please describe your issue in details.

jeevatkm avatar Aug 22 '16 20:08 jeevatkm

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

kdeenanauth avatar Aug 22 '16 22:08 kdeenanauth

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.

jeevatkm avatar Aug 23 '16 17:08 jeevatkm

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:

kdeenanauth avatar Aug 23 '16 17:08 kdeenanauth

I'm thinking of method called Bind for variables. If you have implementation please send PR. We can improving it from there.

jeevatkm avatar Aug 23 '16 18:08 jeevatkm

+1

learnmove avatar Nov 23 '17 19:11 learnmove

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

}

kingreatwill avatar Nov 28 '18 08:11 kingreatwill

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

}

kingreatwill avatar Nov 28 '18 08:11 kingreatwill

https://github.com/stroiman/go-automapper

kingreatwill avatar Nov 28 '18 08:11 kingreatwill

@kingreatwill I believe you were trying to giving a reference for this feature implementation, correct?

jeevatkm avatar Nov 30 '18 01:11 jeevatkm