反序列化 JSON 以在 Go 中基于字符串键入的惯用方法

Idiomatic way to deserialise JSON to type based on string in Go

我正在使用 Go v1.17.3

我是 Go 的新手并且来自 OOP 背景我很清楚我还没有进入 Gopher 思维模式!所以我把问题分成两部分,第一部分是我要解决的问题,第二部分是我到目前为止所做的。那样的话,如果我以一种非常奇怪的方式从惯用的 Go 应该看起来像问题的解决方案应该仍然很清楚。

1.我要解决的问题:

将 JSON 请求反序列化为一个结构,其中在请求的字段之一中指定了结构名称。

示例代码

要求:

{
    "id": "1",
    "name": "example-document",
    "dtos": [
        {
            "type": "DTOA",
            "attributes": {
                "name": "Geoff"
            }
        },
        {
            "type": "DTOB",
            "attributes": {
                "length": "24cm"
            }
        }
    ]
}

我想以接口类型的集合结束。

2。到目前为止我做了什么

我有一个名为 dto 的包,它模拟了每个 DTO 能够执行的行为。

package dto

type DTO interface {
    Deserialize(attributes json.RawMessage) error
    ToEntity() (*entity.Entity, error)
}

type RawDTO struct {
    Type       string `json:"type"`
    Attributes json.RawMessage
}

type DTOA {
    Name string `json:"name"`
}

func (dto *DTOA) Deserialize(attributes json.RawMessage) error {
  // Unmarshall json to address of t
}

func (dto *DTOA) ToEntity() (*entity.Entity, error) {
  // Handle creation of EntityA
}

type DTOB {
    Length string `json:"length"`
}

func (dto *DTOB) Deserialize(attributes json.RawMessage) error {
  // Unmarshall json to address of t
}

func (dto *DTOB) ToEntity() (*entity.Entity, error) {
  // Handle creation of EntityB
}

对于上下文,Entity 是另一个包中的接口。

我按照 this Whosebug question

中建议的答案创建了一个类型注册表

这看起来像:

package dto

var typeRegistry = make(map[string]reflect.Type)

func registerType(typedNil interface{}) {
    t := reflect.TypeOf(typedNil).Elem()
    typeRegistry[t.PkgPath()+"."+t.Name()] = t
}

func LoadTypes() {
    registerType((*DTOA)(nil))
    registerType((*DTOB)(nil))
}

func MakeInstance(name string) (DTO, error) {
    if _, ok := typeRegistry[name]; ok {
        return reflect.New(typeRegistry[name]).Elem().Addr().Interface().(DTO), nil
    }

    return nil, fmt.Errorf("[%s] is not a registered type", name)
}

当我把这些放在一起时:

package commands

type CreateCommand struct {
    ID   string       `json:"id"`
    Name string       `json:"name"`
    DTOs []dto.RawDTO `json:"dtos"`
}

func CreateCommandHandler(w http.ResponseWriter, r *http.Request) {
    var cmd CreateCommand
    bodyBytes, err := ioutil.ReadAll(r.Body)
    if err != nil {
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    err = json.Unmarshal(bodyBytes, &cmd)
    if err != nil {
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    var entities []*entity.Entity
    for _, v := range cmd.DTOs {
        // I have a zero instance of a type that implements the DTO interface
        dto, err := dto.MakeInstance("path_to_package." + v.Type)
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            return
        }
        // Each registered type implements Deserialize
        err = dto.Deserialize(v.Attributes)
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            return
        }
        // Each registered type implements ToEntity
        e, err := dto.ToEntity()
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            return
        }
        entities = append(entities, e)
    }

    w.WriteHeader(http.StatusOK)
}

问题

当我执行此代码并发送请求时,出现以下错误:

http: panic serving 127.0.0.1:34020: interface conversion: *dto.DTOA is not dto.DTO: missing method ToEntity goroutine 18 [running]:

我不明白为什么会这样。 Deserialize 方法工作正常。

func CreateCommandHandler(w http.ResponseWriter, r *http.Request) {
    var cmd CreateCommand
    if err := json.NewDecoder(r.Body).Decode(&cmd); err != nil {
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    var entities []*entity.Entity
    for _, v := range cmd.DTOs {
        e, err := v.DTO.ToEntity()
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            return
        }
        entities = append(entities, e)
    }

    w.WriteHeader(http.StatusOK)
}

如果您执行以下操作,您的处理程序可能类似于上面的内容:

  1. 从注册表中删除反射。
var typeRegistry = map[string]func() DTO{
    "DTOA": func() DTO { return new(DTOA) },
    "DTOB": func() DTO { return new(DTOB) },
}
  1. 实施自定义 json.Unmarshaler。
type DTOUnmarshaler struct {
    DTO DTO
}

func (u *DTOUnmarshaler) UnmarshalJSON(data []byte) error {
    var raw struct {
        Type       string `json:"type"`
        Attributes json.RawMessage
    }
    if err := json.Unmarshal(data, &raw); err != nil {
        return err
    }
    u.DTO = typeRegistry[raw.Type]()
    return json.Unmarshal(raw.Attributes, u.DTO)
}
  1. CreateCommand 类型中使用自定义解组器而不是 RawDTO 类型。
type CreateCommand struct {
    ID   string               `json:"id"`
    Name string               `json:"name"`
    DTOs []dto.DTOUnmarshaler `json:"dtos"`
}

完成。


好处:您可以简化 DTO,因为您不再需要 Deserialize

type DTO interface {
    ToEntity() (*entity.Entity, error)
}

type DTOA struct {
    Name string `json:"name"`
}

func (dto *DTOA) ToEntity() (*entity.Entity, error) {
    // Handle creation of EntityA
}

type DTOB struct {
    Length string `json:"length"`
}

func (dto *DTOB) ToEntity() (*entity.Entity, error) {
    // Handle creation of EntityB
}