具有 go 反射的复杂对象的 Unmarshal 方法

Unmarshal method for complex object with go reflections

我开始编写更复杂的 go 代码,我的对象节点将列表从 JSON 对象转换为具有特定键的映射。这个操作帮助我加快了我的算法。但是我现在有一个问题,我的容器结构有几个复杂的 JSON 并且我无法编写通用解决方案来实现通用解决方案。我想到的唯一方法是使用大开关盒,但我认为这不是正确的解决方案。

这是我目前的代码,其中 statusChannel 是代码中的映射,但它是 JSON 字符串中的列表

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
}

当我解码对象时,我收到以下错误:

➜  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

谁能解释一下我如何以通用方式执行此操作?

https://play.golang.org/p/jzU_lHj1wk7

type MetricOne struct {
    // ...

    // Have this field be ignored.
    ChannelsInfo map[string]*statusChannel `json:"-"`
}
func (m MetricOne) MarshalJSON() ([]byte, error) {
    // Declare a new type using the definition of MetricOne,
    // the result of this is that M will have the same structure
    // as MetricOne but none of its methods (this avoids recursive
    // calls to MarshalJSON).
    //
    // Also because M and MetricOne have the same structure you can
    // easily convert between those two. e.g. M(MetricOne{}) and
    // MetricOne(M{}) are valid expressions.
    type M MetricOne

    // Declare a new type that has a field of the "desired" type and
    // also **embeds** the M type. Embedding promotes M's fields to T
    // and encoding/json will marshal those fields unnested/flattened, 
    // i.e. at the same level as the channels_info field.
    type T struct {
        M
        ChannelsInfo []*statusChannel `json:"channels_info"`
    }

    // move map elements to slice
    channels := make([]*statusChannel, 0, len(m.ChannelsInfo))
    for _, c := range m.ChannelsInfo {
        channels = append(channels, c)
    }

    // Pass in an instance of the new type T to json.Marshal.
    // For the embedded M field use a converted instance of the receiver.
    // For the ChannelsInfo field use the channels slice.
    return json.Marshal(T{
        M:            M(m),
        ChannelsInfo: channels,
    })
}
// Same as MarshalJSON but in reverse.
func (m *MetricOne) UnmarshalJSON(data []byte) error {
    type M MetricOne
    type T struct {
        *M
        ChannelsInfo []*statusChannel `json:"channels_info"`
    }
    t := T{M: (*M)(m)}
    if err := json.Unmarshal(data, &t); err != nil {
        panic(err)
    }

    m.ChannelsInfo = make(map[string]*statusChannel, len(t.ChannelsInfo))
    for _, c := range t.ChannelsInfo {
        m.ChannelsInfo[c.ChannelId] = c
    }

    return nil
}