3

I faced such a problem. I need to compare two structure if they type and name of field is equal. To assign value from sour to dist. I write some code, but here I can assign reflect.Field() value. Could you help me? And I create the test in the bellow

import (
    "reflect"
    "testing"
)

func Assign(sour interface{}, dist interface{}) uint {
    counter := 0
    source  := reflect.ValueOf(sour)

    target  := reflect.ValueOf(dist)

    typeSource := reflect.TypeOf(sour)


    typeTarget := reflect.TypeOf(dist)
    for i:=0; i<source.NumField(); i++{
        for j:=0; j<target.NumField();j++{
            if (typeSource.Field(i).Type==typeTarget.Field(j).Type && typeSource.Field(i).Name==typeTarget.Field(j).Name){
                counter = counter + 1
                target.FieldByName(typeSource.Field(i).Name).Set(source.Field(i))


            }
        }
    }

    return uint(counter)
}

func TestAssign(t *testing.T) {
    type A struct {
        A string
        B uint
        C string
    }
    type B struct {
        AA string
        B  int
        C  string
    }
    var (
        a = A{
            A: "Тест A",
            B: 55,
            C: "Test C",
        }
        b = B{
            AA: "OKOK",
            B:  10,
            C:  "FAFA",
        }
    )
    result := Assign(a, b)
    switch true {
    case b.B != 10:
        t.Errorf("b.B = %d; need to be 10", b.B)
    case b.C != "Test C":
        t.Errorf("b.C = %v; need to be  'Test C'", b.C)
    case result != 1:
        t.Errorf("Assign(a,b) = %d; need to be 1", result)
    }
}
Aibaend
  • 67
  • 2
  • 12

1 Answers1

2

For Assign to work, the second argument must be addressable, i.e. you need to pass a pointer to the struct value.

// the second argument MUST be a pointer to the struct
Assing(source, &target)

Then you need to slightly modify your implementation of Assign since a pointer does not have fileds. You can use the Elem() method to get the struct value to which the pointer points.

func Assign(sour interface{}, dist interface{}) uint {
    counter := 0
    source := reflect.ValueOf(sour)

    // dist is expected to be a pointer, so use Elem() to
    // get the type of the value to which the pointer points
    target := reflect.ValueOf(dist).Elem()

    typeSource := reflect.TypeOf(sour)

    typeTarget := target.Type()
    for i := 0; i < source.NumField(); i++ {
        for j := 0; j < target.NumField(); j++ {
            if typeSource.Field(i).Type == typeTarget.Field(j).Type && typeSource.Field(i).Name == typeTarget.Field(j).Name {
                counter = counter + 1
                target.FieldByName(typeSource.Field(i).Name).Set(source.Field(i))

            }
        }
    }

    return uint(counter)
}
mkopriva
  • 35,176
  • 4
  • 57
  • 71