英文:
How to avoid repetition when defining this nested map
问题
你可以使用结构体来定义map[string]string
,然后在properties
中引用该结构体。这样你就不需要每次都定义map[string]string
了。以下是修改后的代码示例:
type StringMap map[string]string
var deviceSchemaJson = map[string]interface{}{
"additionalProperties": false,
"properties": map[string]interface{}{
"application": StringMap{
"type": "string",
},
"hostname": StringMap{
"type": "string",
},
"ipaddress": map[string]interface{}{
"oneOf": []map[string]string{{"format": "ipv4"}, {"format": "ipv6"}},
"type": "string",
},
"kernel_version": StringMap{
"type": "string",
},
},
"type": "object",
}
通过定义StringMap
结构体,你可以在properties
中直接使用StringMap
类型,避免了每次都定义map[string]string
的麻烦。
英文:
In one of my json schema, I have a map like this
var deviceSchemaJson = map[string]interface{}{
"additionalProperties": false,
"properties": map[string]interface{}{
"application": map[string]string{
"type": "string",
},
"hostname": map[string]string{
"type": "string",
},
"ipaddress": map[string]interface{}{
"oneOf": []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
"type": "string",
},
"kernel_version": map[string]string{
"type": "string",
},
},
"type": "object",
}
How can I avoid defining map[string]string
every time?
答案1
得分: 2
如果你更喜欢这个版本,可以尝试一下:
package main
import (
"fmt"
)
func main() {
fmt.Printf("%#v\n", deviceSchemaJson)
}
var deviceSchemaJson = value{
"additionalProperties": false,
"properties": value{
"application": value{
"type": "string",
},
"hostname": value{
"type": "string",
},
"ipaddress": value{
"oneOf": []valuestring{{"format": "ipv4"}, {"format": "ipv6"}},
"type": "string",
},
"kernel_version": valuestring{
"type": "string",
},
},
"type": "object",
}
type value map[string]interface{}
type valuestring map[string]string
链接:https://play.golang.org/p/6Kq5pvXYvNm
英文:
try this if that suites you better
package main
import (
"fmt"
)
func main() {
fmt.Printf("%#v\n",deviceSchemaJson)
}
var deviceSchemaJson = value{
"additionalProperties": false,
"properties": value{
"application": value{
"type": "string",
},
"hostname": value{
"type": "string",
},
"ipaddress": value{
"oneOf": []valuestring{{"format": "ipv4"}, {"format": "ipv6"}},
"type": "string",
},
"kernel_version": valuestring{
"type": "string",
},
},
"type": "object",
}
type value map[string]interface{}
type valuestring map[string]string
答案2
得分: 0
如何避免每次都定义map[string]string
?
通过定义表示数据结构的类型。
type DeviceSchema struct {
AdditionalProperties bool
Properties Properties
Type string
}
type Properties struct {
Application string
Hostname string
IpAddress []map[string]string
KernelVersion string
}
func main() {
deviceSchemaData := DeviceSchema{
AdditionalProperties: false,
Properties: Properties{
IpAddress: []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
},
Type: "object",
}
fmt.Println(deviceSchemaData)
}
链接:https://play.golang.org/p/iHfoft1zyAn
英文:
> How can I avoid defining map[string]string
every time?
By defining types which represent your data structure.
type DeviceSchema struct {
AdditionalProperties bool
Properties Properties
Type string
}
type Properties struct {
Application string
Hostname string
IpAddress []map[string]string
KernelVersion string
}
func main() {
deviceSchemaData := DeviceSchema{
AdditionalProperties: false,
Properties: Properties{
IpAddress: []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
},
Type: "object",
}
fmt.Println(deviceSchemaData)
}
答案3
得分: 0
简化定义的一种方法是:
func stringMap(s...string) map[string]string {
ret:=map[string]string{}
for i:=0; i<len(s);i+=2 {
ret[s[i]]=s[i+1]
}
return ret
}
var deviceSchemaJson = map[string]interface{}{
"additionalProperties": false,
"properties": map[string]interface{}{
"application": stringMap("type","string"),
"hostname": stringMap("type","string"),
"ipaddress": map[string]interface{}{
"oneOf": []map[string]string{stringMap("format","ipv4"),stringMap("format","ipv6")},
"type": "string",
},
"kernel_version": stringMap("type","string"),
},
"type": "object",
}
英文:
One way to simplify the definition would be:
func stringMap(s...string) map[string]string {
ret:=map[string]string{}
for i:=0; i<len(s);i+=2 {
ret展开收缩]=s[i+1]
}
return ret
}
var deviceSchemaJson = map[string]interface{}{
"additionalProperties": false,
"properties": map[string]interface{}{
"application": stringMap("type","string"),
"hostname": stringMap("type","string"),
"ipaddress": map[string]interface{}{
"oneOf": []map[string]string{stringMap("format","ipv4"),stringMap("format","ipv6")},
"type": "string",
},
"kernel_version": stringMap("type","string"),
},
"type": "object",
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论