因此,我的用例包括将不同的 JSON 模式解析为新的结构类型,这些类型将进一步与 ORM 一起使用以从 SQL 数据库中获取数据。在自然界中被编译,我相信不会有一个开箱即用的解决方案,但是是否有任何 hack 可以做到这一点,而不需要创建一个单独的 go 进程。我通过反射(reflection)尝试过,但没有找到令人满意的方法。
目前,我正在使用 a-h generate确实生成结构的库,但我一直在研究如何在运行时加载这些新的结构类型。
编辑
示例 JSON 模式:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Address",
"id": "Address",
"type": "object",
"description": "address",
"properties": {
"houseName": {
"type": "string",
"description": "House Name",
"maxLength": 30
},
"houseNumber": {
"type": "string",
"description": "House Number",
"maxLength": 4
},
"flatNumber": {
"type": "string",
"description": "Flat",
"maxLength": 15
},
"street": {
"type": "string",
"description": "Address 1",
"maxLength": 40
},
"district": {
"type": "string",
"description": "Address 2",
"maxLength": 30
},
"town": {
"type": "string",
"description": "City",
"maxLength": 20
},
"county": {
"type": "string",
"description": "County",
"maxLength": 20
},
"postcode": {
"type": "string",
"description": "Postcode",
"maxLength": 8
}
}
}
现在,在上面提到的库中,有一个命令行工具,它为上面的 json 生成结构类型的文本,如下所示:
// Code generated by schema-generate. DO NOT EDIT.
package main
// Address address
type Address struct {
County string `json:"county,omitempty"`
District string `json:"district,omitempty"`
FlatNumber string `json:"flatNumber,omitempty"`
HouseName string `json:"houseName,omitempty"`
HouseNumber string `json:"houseNumber,omitempty"`
Postcode string `json:"postcode,omitempty"`
Street string `json:"street,omitempty"`
Town string `json:"town,omitempty"`
}
现在的问题是如何在程序中使用这个结构类型而不需要重新编译。有一个 hack,我可以在其中开始一个新的 go 进程,但这似乎不是一个好方法。另一种方法是编写我自己的解析器来解码 JSON 模式,例如:
b := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)
var f interface{}
json.Unmarshal(b, &f)
m := f.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string", vv)
case float64:
fmt.Println(k, "is float64", vv)
case int:
fmt.Println(k, "is int", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
default:
fmt.Println(k, "is of a type I don't know how to handle")
}
}
有人可以建议一些要查找的指针。谢谢。
最佳答案
看来您正在尝试实现自己的 json 编码。这没什么大不了的:标准的 json 包已经支持了。只需让您的类型实现 MarshalJSON
和 UnmarshalJSON
函数(参见 the docs 上的第一个示例)。假设某些字段将被共享(例如 schema、id、type),您可以像这样创建一个统一的类型:
// poor naming, but we need this level of wrapping here
type Data struct {
Metadata
}
type Metadata struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Description string `json:"description"`
Id string `json:"id"`
Properties json.RawMessage `json:"properties"`
Address *Address `json:"-"`
// other types go here, too
}
现在所有属性都将被解码到一个 json.RawMessage
字段(本质上这是一个 []byte
字段)。您现在可以在自定义解码函数中执行如下操作:
func (d *Data) UnmarshalJSON(b []byte) error {
meta := Metadata{}
// unmarshall common fields
if err := json.Unmarshal(b, &meta); err != nil {
return err
}
// Assuming the Type field contains the value that allows you to determine what data you're actually unmarshalling
switch meta.Type {
case "address":
meta.Address = &Address{} // initialise field
if err := json.Unmarshal([]byte(meta.Properties), meta.Address); err != nil {
return err
}
case "name":
meta.Name = &Name{}
if err := json.Unmarshal([]byte(meta.Properties), meta.Name); err != nil {
return err
}
default:
return errors.New("unknown message type")
}
// all done
d.Metadata = meta // assign to embedded
// optionally: clean up the Properties field, as it contains raw JSON, and is exported
d.Metadata.Properties = json.RawMessage{}
return nil
}
您可以为编码做几乎相同的事情。首先弄清楚您实际使用的是什么类型,然后将该对象编码到属性字段中,然后编码整个结构
func (d Data) MarshalJSON() ([]byte, error) {
var (
prop []byte
err error
)
switch {
case d.Metadata.Address != nil:
prop, err = json.Marshal(d.Address)
case d.Metadata.Name != nil:
prop, err = json.Marshal(d.Name) // will only work if field isn't masked, better to be explicit
default:
err = errors.New("No properties to marshal") // handle in whatever way is best
}
if err != nil {
return nil, err
}
d.Metadata.Properties = json.RawMessage(prop)
return json.Marshal(d.Metadata) // marshal the unified type here
}
关于go - 将 JSONSchema 解析为 golang 中的结构类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719384/