将结构转换为切片结构

Transform struct to slice struct

我试图通过字符串输入 select 一个结构,然后根据 return JSON 对象或数组,解组 JSON。想办法把struct反映到slice struct上是不是正确?如果是这样,如何通过反思来做到这一点? 问候, 彼得

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
)

type NameStruct struct {
    Name string
}

func main() {

    jsonData := []byte(`[{"name":"james"},{"name":"steven"}]`)
    returnModel := InitializeModel("NameStruct", jsonData)
    fmt.Println(returnModel)

    jsonData = []byte(`{"name":"james"}`)
    returnModel = InitializeModel("NameStruct", jsonData)
    fmt.Println(returnModel)

}

func getModelByName(modelType string) interface{} {
    modelMap := make(map[string]interface{})
    modelMap["NameStruct"] = new(NameStruct)

    //don't want to do this
    modelMap["arrNameStruct"] = new([]NameStruct)
    return modelMap[modelType]
}

func InitializeModel(modelName string, jsonData []byte) interface{} {
    switch IsArray(jsonData) {
    case true:
        // some conversion here, how?
        returnModel := getModelByName("NameStruct")
        if err := json.Unmarshal(jsonData, &returnModel); err != nil {
            log.Println(err)
        }
        return returnModel
    case false:
        returnModel := getModelByName("NameStruct")
        if err := json.Unmarshal(jsonData, &returnModel); err != nil {
            log.Println(err)
        }
        return returnModel
    }
    return nil
}

func IsArray(jsonData []byte) bool {
    return (bytes.HasPrefix(jsonData, []byte("["))) && (bytes.HasSuffix(jsonData, []byte("]")))
}

扩展我的评论,您可以创建一个 Factory 注册预定义类型的地方:

type Factory struct {
    m map[string]reflect.Type
}

func (f *Factory) Register(v interface{}) {
    vt := reflect.TypeOf(v)
    n := vt.Name()
    f.m[n] = vt
    f.m["[]"+n] = reflect.SliceOf(vt) // implicitly register a slice of type too
}

这些类型可以在运行时按名称查找并使用 JSON 数据初始化:

func (f *Factory) Make(k string, bs []byte) (interface{}, error) {
    vt, ok := f.m[k]
    if !ok {
        return nil, fmt.Errorf("type %q not registered", k)
    }

    pv := reflect.New(vt).Interface()

    err := json.Unmarshal(bs, pv)
    if err != nil {
        return nil, err
    }

    return pv, nil
}

使用:

type Place struct {
    City string `json:"city"`
}

factory.Register(Place{})

p, err := factory.Make("Place", []byte(`{"city":"NYC"}`))

fmt.Printf("%#v\n", p) // &main.Place{City:"NYC"}

切片也有效:

ps, err := factory.Make("[]Place", []byte(`[{"city":"NYC"},{"city":"Dublin"}]`))

fmt.Printf("%#v\n", p, p) // &[]main.Place{main.Place{City:"NYC"}, main.Place{City:"Dublin"}}

游乐场:https://play.golang.org/p/qWEdwk-YUug