mercredi 18 août 2021

Unmarshal method for complex object with go reflections

I'm starting to writing more complex go code, and my object node it to convert a list from a JSON object to a map with a particular key. This operation helps me to speed up my algorithm. But I have a problem now, my container struct has several complex JSON and I'm not able to write a generic solution to achieve a generic solution. The only way that I have in mind is to use a big switch case, but I think this is not the right solution.

This is my code at the moment, where the statusChannel is a map in the code but it is a list in the JSON string

type MetricOne struct {
    // Internal id to identify the metric
    id int `json:"-"`
    // Version of metrics format, it is used to migrate the
    // JSON payload from previous version of plugin.
    Version int `json:"version"`
    // Name of the metrics
    Name   string  `json:"metric_name"`
    NodeId string  `json:"node_id"`
    Color  string  `json:"color"`
    OSInfo *osInfo `json:"os_info"`
    // timezone where the node is located
    Timezone string `json:"timezone"`
    // array of the up_time
    UpTime []*status `json:"up_time"`
    // map of informatonof channel information
    ChannelsInfo map[string]*statusChannel `json:"channels_info"`
}

func (instance *MetricOne) MarshalJSON() ([]byte, error) {
    jsonMap := make(map[string]interface{})
    reflectType := reflect.TypeOf(*instance)
    reflectValue := reflect.ValueOf(*instance)
    nFiled := reflectValue.Type().NumField()

    for i := 0; i < nFiled; i++ {
        key := reflectType.Field(i)
        valueFiled := reflectValue.Field(i)
        jsonName := key.Tag.Get("json")
        switch jsonName {
        case "-":
            // skip
            continue
        case "channels_info":
            // TODO convert the map[string]*statusChannel in a list of statusChannel
            statusChannels := make([]*statusChannel, 0)
            for _, value := range valueFiled.Interface().(map[string]*statusChannel) {
                statusChannels = append(statusChannels, value)
            }
            jsonMap[jsonName] = statusChannels
        default:
            jsonMap[jsonName] = valueFiled.Interface()
        }
    }

    return json.Marshal(jsonMap)
}

func (instance *MetricOne) UnmarshalJSON(data []byte) error {
    var jsonMap map[string]interface{}
    err := json.Unmarshal(data, &jsonMap)
    if err != nil {
        log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
        return err
    }
    instance.Migrate(jsonMap)
    reflectValue := reflect.ValueOf(instance)
    reflectStruct := reflectValue.Elem()
    // reflectType := reflectValue.Type()
    for key, value := range jsonMap {
        fieldName, err := utils.GetFieldName(key, "json", *instance)
        if err != nil {
            log.GetInstance().Info(fmt.Sprintf("Error: %s", err))
            if strings.Contains(key, "dev_") {
                log.GetInstance().Info("dev propriety skipped if missed")
                continue
            }
            return err
        }
        field := reflectStruct.FieldByName(*fieldName)
        fieldType := field.Type()
        filedValue := field.Interface()
        val := reflect.ValueOf(filedValue)

        switch key {
        case "channels_info":
            statusChannelsMap := make(map[string]*statusChannel)
            toArray := value.([]interface{})
            for _, status := range toArray {
                var statusType statusChannel
                jsonVal, err := json.Marshal(status)
                if err != nil {
                    return err
                }
                err = json.Unmarshal(jsonVal, &statusType)
                if err != nil {
                    return err
                }
                statusChannelsMap[statusType.ChannelId] = &statusType
            }
            field.Set(reflect.ValueOf(statusChannelsMap))
        default:
            field.Set(val.Convert(fieldType))
        }
    }
    return nil
}

And when I will decode the object I receive the following error:

➜  go-metrics-reported git:(dev) ✗ make check
go test -v ./...
?       github.com/OpenLNMetrics/go-metrics-reported/cmd/go-metrics-reported    [no test files]
?       github.com/OpenLNMetrics/go-metrics-reported/init/persistence   [no test files]
=== RUN   TestJSONSerializzation
--- PASS: TestJSONSerializzation (0.00s)
=== RUN   TestJSONDeserializzation
--- FAIL: TestJSONDeserializzation (0.00s)
panic: reflect.Value.Convert: value of type map[string]interface {} cannot be converted to type *plugin.osInfo [recovered]
    panic: reflect.Value.Convert: value of type map[string]interface {} cannot be converted to type *plugin.osInfo

goroutine 7 [running]:
testing.tRunner.func1.1(0x61b440, 0xc0001d69a0)
    /home/vincent/.gosdk/go/src/testing/testing.go:1072 +0x30d
testing.tRunner.func1(0xc000001e00)
    /home/vincent/.gosdk/go/src/testing/testing.go:1075 +0x41a
panic(0x61b440, 0xc0001d69a0)
    /home/vincent/.gosdk/go/src/runtime/panic.go:969 +0x1b9
reflect.Value.Convert(0x6283e0, 0xc0001bb1a0, 0x15, 0x6b93a0, 0x610dc0, 0x610dc0, 0xc00014cb40, 0x196)
    /home/vincent/.gosdk/go/src/reflect/value.go:2447 +0x229
github.com/OpenLNMetrics/go-metrics-reported/internal/plugin.(*MetricOne).UnmarshalJSON(0xc00014cb00, 0xc0001d8000, 0x493, 0x500, 0x7f04d01453d8, 0xc00014cb00)
    /home/vincent/Github/OpenLNMetrics/go-metrics-reported/internal/plugin/metrics_one.go:204 +0x5b3
encoding/json.(*decodeState).object(0xc00010be40, 0x657160, 0xc00014cb00, 0x16, 0xc00010be68, 0x7b)
    /home/vincent/.gosdk/go/src/encoding/json/decode.go:609 +0x207c
encoding/json.(*decodeState).value(0xc00010be40, 0x657160, 0xc00014cb00, 0x16, 0xc000034698, 0x54ec19)
    /home/vincent/.gosdk/go/src/encoding/json/decode.go:370 +0x6d
encoding/json.(*decodeState).unmarshal(0xc00010be40, 0x657160, 0xc00014cb00, 0xc00010be68, 0x0)
    /home/vincent/.gosdk/go/src/encoding/json/decode.go:180 +0x1ea
encoding/json.Unmarshal(0xc0001d8000, 0x493, 0x500, 0x657160, 0xc00014cb00, 0x500, 0x48cba6)
    /home/vincent/.gosdk/go/src/encoding/json/decode.go:107 +0x112
github.com/OpenLNMetrics/go-metrics-reported/internal/plugin.TestJSONDeserializzation(0xc000001e00)
    /home/vincent/Github/OpenLNMetrics/go-metrics-reported/internal/plugin/metric_one_test.go:87 +0x95
testing.tRunner(0xc000001e00, 0x681000)
    /home/vincent/.gosdk/go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
    /home/vincent/.gosdk/go/src/testing/testing.go:1168 +0x2b3
FAIL    github.com/OpenLNMetrics/go-metrics-reported/internal/plugin    0.008s
?       github.com/OpenLNMetrics/go-metrics-reported/pkg/db [no test files]
?       github.com/OpenLNMetrics/go-metrics-reported/pkg/graphql    [no test files]
?       github.com/OpenLNMetrics/go-metrics-reported/pkg/log    [no test files]
?       github.com/OpenLNMetrics/go-metrics-reported/pkg/utils  [no test files]
FAIL
make: *** [Makefile:15: check] Error 1

can someone explain how I can do this operation in a generic way?





Aucun commentaire:

Enregistrer un commentaire