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

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

How to avoid repetition when defining this nested map

问题

你可以使用结构体来定义map[string]string,然后在properties中引用该结构体。这样你就不需要每次都定义map[string]string了。以下是修改后的代码示例:

  1. type StringMap map[string]string
  2. var deviceSchemaJson = map[string]interface{}{
  3. "additionalProperties": false,
  4. "properties": map[string]interface{}{
  5. "application": StringMap{
  6. "type": "string",
  7. },
  8. "hostname": StringMap{
  9. "type": "string",
  10. },
  11. "ipaddress": map[string]interface{}{
  12. "oneOf": []map[string]string{{"format": "ipv4"}, {"format": "ipv6"}},
  13. "type": "string",
  14. },
  15. "kernel_version": StringMap{
  16. "type": "string",
  17. },
  18. },
  19. "type": "object",
  20. }

通过定义StringMap结构体,你可以在properties中直接使用StringMap类型,避免了每次都定义map[string]string的麻烦。

英文:

In one of my json schema, I have a map like this

  1. var deviceSchemaJson = map[string]interface{}{
  2. "additionalProperties": false,
  3. "properties": map[string]interface{}{
  4. "application": map[string]string{
  5. "type": "string",
  6. },
  7. "hostname": map[string]string{
  8. "type": "string",
  9. },
  10. "ipaddress": map[string]interface{}{
  11. "oneOf": []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
  12. "type": "string",
  13. },
  14. "kernel_version": map[string]string{
  15. "type": "string",
  16. },
  17. },
  18. "type": "object",
  19. }

How can I avoid defining map[string]string every time?

答案1

得分: 2

如果你更喜欢这个版本,可以尝试一下:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. fmt.Printf("%#v\n", deviceSchemaJson)
  7. }
  8. var deviceSchemaJson = value{
  9. "additionalProperties": false,
  10. "properties": value{
  11. "application": value{
  12. "type": "string",
  13. },
  14. "hostname": value{
  15. "type": "string",
  16. },
  17. "ipaddress": value{
  18. "oneOf": []valuestring{{"format": "ipv4"}, {"format": "ipv6"}},
  19. "type": "string",
  20. },
  21. "kernel_version": valuestring{
  22. "type": "string",
  23. },
  24. },
  25. "type": "object",
  26. }
  27. type value map[string]interface{}
  28. type valuestring map[string]string

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

英文:

try this if that suites you better

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. fmt.Printf("%#v\n",deviceSchemaJson)
  7. }
  8. var deviceSchemaJson = value{
  9. "additionalProperties": false,
  10. "properties": value{
  11. "application": value{
  12. "type": "string",
  13. },
  14. "hostname": value{
  15. "type": "string",
  16. },
  17. "ipaddress": value{
  18. "oneOf": []valuestring{{"format": "ipv4"}, {"format": "ipv6"}},
  19. "type": "string",
  20. },
  21. "kernel_version": valuestring{
  22. "type": "string",
  23. },
  24. },
  25. "type": "object",
  26. }
  27. type value map[string]interface{}
  28. type valuestring map[string]string

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

答案2

得分: 0

如何避免每次都定义map[string]string

通过定义表示数据结构的类型。

  1. type DeviceSchema struct {
  2. AdditionalProperties bool
  3. Properties Properties
  4. Type string
  5. }
  6. type Properties struct {
  7. Application string
  8. Hostname string
  9. IpAddress []map[string]string
  10. KernelVersion string
  11. }
  12. func main() {
  13. deviceSchemaData := DeviceSchema{
  14. AdditionalProperties: false,
  15. Properties: Properties{
  16. IpAddress: []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
  17. },
  18. Type: "object",
  19. }
  20. fmt.Println(deviceSchemaData)
  21. }

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

英文:

> How can I avoid defining map[string]string every time?

By defining types which represent your data structure.

  1. type DeviceSchema struct {
  2. AdditionalProperties bool
  3. Properties Properties
  4. Type string
  5. }
  6. type Properties struct {
  7. Application string
  8. Hostname string
  9. IpAddress []map[string]string
  10. KernelVersion string
  11. }
  12. func main() {
  13. deviceSchemaData := DeviceSchema{
  14. AdditionalProperties: false,
  15. Properties: Properties{
  16. IpAddress: []map[string]string{{"format": "ipv4"},{"format": "ipv6"}},
  17. },
  18. Type: "object",
  19. }
  20. fmt.Println(deviceSchemaData)
  21. }

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

答案3

得分: 0

简化定义的一种方法是:

  1. func stringMap(s...string) map[string]string {
  2. ret:=map[string]string{}
  3. for i:=0; i<len(s);i+=2 {
  4. ret[s[i]]=s[i+1]
  5. }
  6. return ret
  7. }
  8. var deviceSchemaJson = map[string]interface{}{
  9. "additionalProperties": false,
  10. "properties": map[string]interface{}{
  11. "application": stringMap("type","string"),
  12. "hostname": stringMap("type","string"),
  13. "ipaddress": map[string]interface{}{
  14. "oneOf": []map[string]string{stringMap("format","ipv4"),stringMap("format","ipv6")},
  15. "type": "string",
  16. },
  17. "kernel_version": stringMap("type","string"),
  18. },
  19. "type": "object",
  20. }
英文:

One way to simplify the definition would be:

  1. func stringMap(s...string) map[string]string {
  2. ret:=map[string]string{}
  3. for i:=0; i&lt;len(s);i+=2 {
  4. ret
    展开收缩
    ]=s[i+1]
  5. }
  6. return ret
  7. }
  8. var deviceSchemaJson = map[string]interface{}{
  9. &quot;additionalProperties&quot;: false,
  10. &quot;properties&quot;: map[string]interface{}{
  11. &quot;application&quot;: stringMap(&quot;type&quot;,&quot;string&quot;),
  12. &quot;hostname&quot;: stringMap(&quot;type&quot;,&quot;string&quot;),
  13. &quot;ipaddress&quot;: map[string]interface{}{
  14. &quot;oneOf&quot;: []map[string]string{stringMap(&quot;format&quot;,&quot;ipv4&quot;),stringMap(&quot;format&quot;,&quot;ipv6&quot;)},
  15. &quot;type&quot;: &quot;string&quot;,
  16. },
  17. &quot;kernel_version&quot;: stringMap(&quot;type&quot;,&quot;string&quot;),
  18. },
  19. &quot;type&quot;: &quot;object&quot;,
  20. }
  21. </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:

确定