在Go语言中,根据字符串将JSON反序列化为特定类型的惯用方式是什么?

huangapple go评论87阅读模式
英文:

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

问题

我正在使用Go v1.17.3。

我对Go还不太熟悉,来自面向对象编程背景的我很清楚我还没有完全理解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 struct {
    Name string `json:"name"`
}

func (dto *DTOA) Deserialize(attributes json.RawMessage) error {
  // 将JSON反序列化为t的地址
}

func (dto *DTOA) ToEntity() (*entity.Entity, error) {
  // 处理EntityA的创建
}

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

func (dto *DTOB) Deserialize(attributes json.RawMessage) error {
  // 将JSON反序列化为t的地址
}

func (dto *DTOB) ToEntity() (*entity.Entity, error) {
  // 处理EntityB的创建
}

为了提供上下文,Entity是另一个包中的一个接口。

我通过遵循这个StackOverflow问题中提出的答案创建了一个类型注册表。

代码如下:

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] 不是一个注册的类型", 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 {
        // 我有一个实现DTO接口的类型的零值实例
        dto, err := dto.MakeInstance("path_to_package." + v.Type)
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            return
        }
        // 每个注册的类型都实现了Deserialize方法
        err = dto.Deserialize(v.Attributes)
        if err != nil {
            w.WriteHeader(http.StatusBadRequest)
            return
        }
        // 每个注册的类型都实现了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方法工作正常。

英文:

I'm using Go v1.17.3

I'm pretty new with Go and coming from an OOP background I'm very aware I'm not in the Gopher mindset yet! So I've split the question in 2 sections, the first is the problem I'm trying to solve, the second is what I've done so far. That way if I've approached the solution in a really strange way from what idiomatic Go should look like the problem should still be clear.

1. Problem I'm trying to solve:

Deserialise a JSON request to a struct where the struct name is specified in one of the fields on the request.

Example code

Request:

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

And I want to end up with a collection of interface types.

2. What I've done so far

I've got a package called dto which models the behviours each DTO is capable of.

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
}

For context, Entity is an interface in another package.

I've created a type registry by following the answers suggested from this StackOverflow question

This looks like:

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)
}

When I bring this all together:

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)
}

The issue

When I execute this code and send a request, I get the following error:

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

I can't figure out why this is happening. The Deserialize method works fine.

答案1

得分: 3

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类型中使用自定义的解组器(unmarshaler)而不是RawDTO类型。
type CreateCommand struct {
	ID   string               `json:"id"`
	Name string               `json:"name"`
	DTOs []dto.DTOUnmarshaler `json:"dtos"`
}

完成。


额外奖励:由于不再需要Deserialize,你可以简化DTOs。

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

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

func (dto *DTOA) ToEntity() (*entity.Entity, error) {
	// 处理EntityA的创建
}

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

func (dto *DTOB) ToEntity() (*entity.Entity, error) {
	// 处理EntityB的创建
}
英文:
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)
}

Your handler could look like the above if you do the following:

  1. Drop the reflection from the registry.
var typeRegistry = map[string]func() DTO{
	"DTOA": func() DTO { return new(DTOA) },
	"DTOB": func() DTO { return new(DTOB) },
}
  1. Implement a custom 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. In the CreateCommand type use the custom unmarshaler instead of the RawDTO type.
type CreateCommand struct {
	ID   string               `json:"id"`
	Name string               `json:"name"`
	DTOs []dto.DTOUnmarshaler `json:"dtos"`
}

Done.


Bonus: you get to simplify your DTOs since you don't need Deserialize anymore.

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
}

huangapple
  • 本文由 发表于 2022年2月2日 20:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/70955529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定