Home > database >  Create instance of struct via reflection and set values
Create instance of struct via reflection and set values

Time:12-03

what I try to do

I try to pass an instance of a struct - including json tags to a func, create a new instance, and set value on field
after this i try to serialize (JSON), but the values are empty

NOTICE: i looked up loads of articles on SO about setting values via reflection, but it seems i missed a little detail

struct definition

this part defines the struct with json and xml tags

type Person struct {
    Name string `json:"Name" xml:"Person>FullName"`
    Age  int    `json:"Age" xml:"Person>Age"`
}

create instance ( wrapping into empty interface)

afterwards I create an instance and store it in an interface{} - why? because in my production code this stuff will be done in a func which accepts a interface{}

var iFace interface{} = Person{
        Name: "Test",
        Age:  666,
    }

creating a new instance of the struct and setting values via reflection

iFaceType := reflect.TypeOf(iFace)
    item := reflect.New(iFaceType)
    s := item.Elem()
    if s.Kind() == reflect.Struct {
        fName := s.FieldByName("Name")
        if fName.IsValid() {
            // A Value can be changed only if it is
            // addressable and was not obtained by
            // the use of unexported struct fields.
            if fName.CanSet() {
                // change value of N
                switch fName.Kind() {
                case reflect.String:
                    fName.SetString("reflectedNameValue")
                    fmt.Println("Name was set to reflectedNameValue")
                }
            }
        }
        fAge := s.FieldByName("Age")
        if fAge.IsValid() {
            // A Value can be changed only if it is
            // addressable and was not obtained by
            // the use of unexported struct fields.
            if fAge.CanSet() {
                // change value of N
                switch fAge.Kind() {
                case reflect.Int:
                    x := int64(42)
                    if !fAge.OverflowInt(x) {
                        fAge.SetInt(x)
                        fmt.Println("Age was set to", x)
                    }
                }
            }
        }
    }

Question

what am I doing wrong?
in production code I fill multiple copies with data and add it to a slice...
but this only makes sense if the json tags are kept in place and the stuff serializes just the same way.

code sample for play

package main

import (
    "encoding/json"
    "fmt"
    "reflect"
)

func main() {
    type Person struct {
        Name string `json:"Name" xml:"Person>FullName"`
        Age  int    `json:"Age" xml:"Person>Age"`
    }

    var iFace interface{} = Person{
        Name: "Test",
        Age:  666,
    }
    fmt.Println("normal: \n"   JSONify(iFace))
    iFaceType := reflect.TypeOf(iFace)
    item := reflect.New(iFaceType)
    s := item.Elem()
    if s.Kind() == reflect.Struct {
        fName := s.FieldByName("Name")
        if fName.IsValid() {
            // A Value can be changed only if it is
            // addressable and was not obtained by
            // the use of unexported struct fields.
            if fName.CanSet() {
                // change value of N
                switch fName.Kind() {
                case reflect.String:
                    fName.SetString("reflectedNameValue")
                    fmt.Println("Name was set to reflectedNameValue")
                }
            }
        }
        fAge := s.FieldByName("Age")
        if fAge.IsValid() {
            // A Value can be changed only if it is
            // addressable and was not obtained by
            // the use of unexported struct fields.
            if fAge.CanSet() {
                // change value of N
                switch fAge.Kind() {
                case reflect.Int:
                    x := int64(42)
                    if !fAge.OverflowInt(x) {
                        fAge.SetInt(x)
                        fmt.Println("Age was set to", x)
                    }
                }
            }
        }
    }
    fmt.Println("reflected: \n"   JSONify(item))
}

func JSONify(v interface{}) string {
    var bytes []byte
    bytes, _ = json.MarshalIndent(v, "", "\t")
    return string(bytes)
}

CodePudding user response:

Your item is of type reflect.Value. You have to call Value.Interface() to obtain the value wrapped in it:

fmt.Println("reflected: \n"   JSONify(item.Interface()))

With this change, output will be (try it on the Go Playground):

normal: 
{
    "Name": "Test",
    "Age": 666
}
Name was set to reflectedNameValue
Age was set to 42
reflected: 
{
    "Name": "reflectedNameValue",
    "Age": 42
}

reflect.Value itself is also a struct, but obviously trying to marshal it will not be identical to marshaling a Person struct value. reflect.Value does not implement marshaling the wrapped data to JSON.

  • Related