gpt4 book ai didi

go - 将 JSON 反序列化为基于 Go 中的字符串类型的惯用方法

转载 作者:行者123 更新时间:2023-12-05 03:29:47 25 4
gpt4 key购买 nike

我正在使用 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"
}
}
]
}

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

<强>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 是另一个包中的接口(interface)。

我按照 this StackOverflow 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 ToEntitygoroutine 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
}

关于go - 将 JSON 反序列化为基于 Go 中的字符串类型的惯用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70955529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com