如何在定义嵌套映射时避免重复?

huangapple go评论98阅读模式
英文:

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

https://play.golang.org/p/6Kq5pvXYvNm

答案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)
}

https://play.golang.org/p/iHfoft1zyAn

答案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&lt;len(s);i+=2 {
     ret
展开收缩
]=s[i+1] } return ret } var deviceSchemaJson = map[string]interface{}{ &quot;additionalProperties&quot;: false, &quot;properties&quot;: map[string]interface{}{ &quot;application&quot;: stringMap(&quot;type&quot;,&quot;string&quot;), &quot;hostname&quot;: stringMap(&quot;type&quot;,&quot;string&quot;), &quot;ipaddress&quot;: map[string]interface{}{ &quot;oneOf&quot;: []map[string]string{stringMap(&quot;format&quot;,&quot;ipv4&quot;),stringMap(&quot;format&quot;,&quot;ipv6&quot;)}, &quot;type&quot;: &quot;string&quot;, }, &quot;kernel_version&quot;: stringMap(&quot;type&quot;,&quot;string&quot;), }, &quot;type&quot;: &quot;object&quot;, } </details>

huangapple
  • 本文由 发表于 2021年8月23日 03:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/68884721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定