YAML MapSlice用于在编组或解组时保留序列。

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

YAML MapSlice to preserve the sequence when marshall or unmarshall

问题

我很困惑如何使用YAML的MapSlice数据结构。它来自于这里:https://sourcegraph.com/go/gopkg.in/yaml.v2/-/MapSlice。我成功地将其解组为MapSlice类型,但是如何将其映射到我自己的结构体呢?

var data = `
id:
  id-jakut:
    en:
      name: North Jakarta City
      label: North Jakarta
    id:
      name: Kota Jakarta Utara
      label: Jakarta Utara
  id-jaksel:
    en:
      name: South Jakarta City
      label: South Jakarta
    id:
      name: Kota Jakarta Selatan
      label: Jakarta Selatan
tw:
  tw-tp:
    en:
      name: Taipei City
      label: Taipei
    zh-TW:
      name: 台北
      label: 台北市
  tw-ntp:
    en:
      name: New Taipei City
      label: New Taipei City
    zh-TW:
      name: 新北市
      label: 新北市
`

type cityLocale struct {
    Name  string `yaml:"name,flow"`
    Label string `yaml:"label,flow"`
}

type cityLocales map[string]cityLocale
type cities map[string]cityLocales
type countryCities map[string]cities

func main() {
    m := yaml.MapSlice{}
    err := yaml.Unmarshal([]byte(data), &m)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("MAPSLICE==>%+v\n\n", m)

    t := countryCities{}
    err = yaml.Unmarshal([]byte(data), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("NORMAL==>%+v\n", t["tw"])
}

以上是你提供的代码。

英文:

I'm confused how to use YAML MapSlice data Structure. It's from here https://sourcegraph.com/go/gopkg.in/yaml.v2/-/MapSlice . I manage to unmarshall to a type of MapSlice but how to map it to my own struct

var data = `
id:
  id-jakut:
    en:
      name: North Jakarta City
      label: North Jakarta
    id:
      name: Kota Jakarta Utara
      label: Jakarta Utara
  id-jaksel:
    en:
      name: South Jakarta City
      label: South Jakarta
    id:
      name: Kota Jakarta Selatan
      label: Jakarta Selatan
tw:
  tw-tp:
    en:
      name: Taipei City
      label: Taipei
    zh-TW:
      name: 台北
      label: 台北市
  tw-ntp:
    en:
      name: New Taipei City
      label: New Taipei City
    zh-TW:
      name: 新北市
      label: 新北市
`

type cityLocale struct {
	Name  string `yaml:"name,flow"`
	Label string `yaml:"label,flow"`
}

type cityLocales map[string]cityLocale
type cities map[string]cityLocales
type countryCities map[string]cities

func main() {
	m := yaml.MapSlice{}
	err := yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("MAPSLICE==>%+v\n\n", m)    	

	t := countryCities{}
	err = yaml.Unmarshal([]byte(data), &t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("NORMAL==>%+v\n", t["tw"])

}

答案1

得分: 1

你需要更改你的城市类型,因为你错过了一个地图。如果你的城市是一个字符串地图的地图,你的代码就可以工作:

type cities map[string]map[string]cityLocales
英文:

You need to change your cities type, because you missed one map. If your cities are a map of map of strings your code works:

type cities map[string]map[string]cityLocales

答案2

得分: 0

你可以将MapSlice转换为你自己的类型。这涉及到类型转换,特别是对于像你示例中的嵌套类型。我在另一个答案中给出了一个非常基本的示例,你可以在这里找到。

英文:

You can convert the MapSlice to your own type. This involves type conversion. Especially for nested types like you have in your example.
I've given a very basic example in another answer you can find here.

huangapple
  • 本文由 发表于 2017年2月16日 13:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/42266028.html
匿名

发表评论

匿名网友

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

确定