我使用JSON模式,并使用gojsonschema自动生成它。不幸的是,它有几个字段出错,我想修复它们。
以下是模式:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/filecoin-project/bacalhau/pkg/model/job",
"$ref": "#/$defs/Job",
"$defs": {
"Deal": {
"properties": {
"Concurrency": {
"type": "integer"
},
"Confidence": {
"type": "integer"
},
"MinBids": {
"type": "integer"
}
},
"additionalProperties": false,
"type": "object"
},
"Spec": {
"properties": {
"Engine": {
"type": "integer"
},
"Verifier": {
"type": "integer"
},
},
}
}
}
我想做的是用程序找到一个给定字段(在本例中为“引擎”)的路径。这样我就可以列出所有需要更改的对象(它们都需要以相同的方式更改),并在数组中循环遍历它们。所以像这样的东西(今天有效)。
func FixJSONSchema() ([]byte, error) {
s := jsonschema.Reflect(&model.Job{})
jsonSchemaData, err := json.MarshalIndent(s, "", " ")
if err != nil {
return nil, fmt.Errorf("error indenting %s", err)
}
// JSON String
jsonString := string(jsonSchemaData)
enumTypes := []struct {
Name string
Path string
}{
{Name: "Engine", Path: "$defs.Spec.properties.Engine.type"},
{Name: "Verifier", Path: "$defs.Spec.properties.Verifier.type"},
}
for _, enumType := range enumTypes {
// Use sjson to find the enum type path in the JSON
jsonString, _ = sjson.Set(jsonString, enumType.Path, "string")
}
return []byte(jsonString), nil
}
我正在使用优秀的sjson库来实现这一点。
https://github.com/tidwall/sjson
总之,我真正想做的是,只需要一个数组
["Engine", "Verifier", ...]
并使用工具搜索整个结构并返回路径(如果多个内容匹配,则返回路径)。