Skip to content

ORM and http Request deserialization should not rely on gconv.Struct #4099

@cyjaysong

Description

@cyjaysong

Is your feature request related to a problem?

Option Yes

Describe the solution you'd like

目前 Orm和 http Request反序列化 均使用 gconv.Struct 反序列化。但两者在场景的反序列化逻辑上存在冲突。
Orm 查询返回的结果反序列化到对象场景中,数据库的 json字段数据应当严格按照Json协议反序列化到对象中的对应字段

type User struct {
   Id int64 `orm:id`
   Name   string `orm:name`
   Other  UserOther ` orm:other` // 对应数据库中的json数据类型
}

type UserOther struct {
         ...
}

数据中的other 为Json数据,应按照Json的反序列化方式反序列化到UserOther 对象

http Request反序列化反序列化的场景中,情况比较复杂,由于query和formData 允许一键多值,那么实际query和formData 的整体数据结构是map[string][]string。 但是接收参数的对象结构可能存在两种情况

type Req1 struct {
    Keys []string   `p:keys`
}
type Req2 struct {
    Key string  `p:key`
} 

反序列化到Req1中时,query或formData中keys参数无论传递了多少个值,都会被放到[]string中;反序列化到Req2中时若key传递了一个值,那么Key就等于key参数的值,若key 传递多个值,只会把多个key值的最后一个值赋值到Key

gconv的定位应当是方便开发数据类型转换的工具。目前gconv为了兼容http Request反序列化场景以及解决#3465 这种问题,用gconv.Struct转换时,会把某个字段值为塞入切片中。看似解决了http Req1 场景,但这也使得,在orm的JSON解析场景时,如果数据库的某json字段值为null,然后该字段的在结构体中字段对于的数据类型为[]string, 反序列化回来的值为["null"],预期结果不符,详见#4086

Describe alternatives you've considered

orm 反序列化方案可以产考 https://gitea.com/xorm/xorm
http Request 反序列化方案可以产考 https://github.com/gorilla/schema

Additional

xorm 也支持我在 #4086 中提到的需求

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions