如何解析复杂嵌套的 YAML 数据结构?

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

How do I Unmarshal complex nesting of yaml

问题

如何解组这个数据?我试图提取名称、描述、版本和其他字段。但是当我打印出解组后的内容时,我得到以下结果:

Index: {ApiVersion:v1 Entry:map[]
Generated:2016-10-06T16:23:20.499029981-06:00}

以下是我到目前为止尝试过的方法。

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. const index_yaml = `apiVersion: v1
  8. entries:
  9. alpine:
  10. - created: 2016-10-06T16:23:20.499814565-06:00
  11. description: Deploy a basic Alpine Linux pod
  12. digest: 99c76e403d752c84ead610644d4b1c2f2b453a74b921f422b9dcb8a7c8b559cd
  13. home: https://helm.sh/helm
  14. name: alpine
  15. sources:
  16. - https://github.com/helm/helm
  17. urls:
  18. - https://technosophos.github.io/tscharts/alpine-0.2.0.tgz
  19. version: 0.2.0
  20. - created: 2016-10-06T16:23:20.499543808-06:00
  21. description: Deploy a basic Alpine Linux pod
  22. digest: 515c58e5f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cd78727
  23. home: https://helm.sh/helm
  24. name: alpine
  25. sources:
  26. - https://github.com/helm/helm
  27. urls:
  28. - https://technosophos.github.io/tscharts/alpine-0.1.0.tgz
  29. version: 0.1.0
  30. nginx:
  31. - created: 2016-10-06T16:23:20.499543808-06:00
  32. description: Create a basic nginx HTTP server
  33. digest: aaff4545f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cdffffff
  34. home: https://helm.sh/helm
  35. name: nginx
  36. sources:
  37. - https://github.com/helm/charts
  38. urls:
  39. - https://technosophos.github.io/tscharts/nginx-1.1.0.tgz
  40. version: 1.1.0
  41. generated: 2016-10-06T16:23:20.499029981-06:00`
  42. type Entry struct {
  43. Created string `yaml:"created"`
  44. Description string `yaml:"description"`
  45. Home string `yaml:"home"`
  46. }
  47. type Entries map[string][]Entry
  48. type Index struct {
  49. ApiVersion string `yaml:"apiVersion"`
  50. Entry Entries `yaml:"entries"`
  51. Generated string `yaml:"generated"`
  52. }
  53. func main() {
  54. // data, err := ioutil.ReadFile("index.yaml")
  55. // if err != nil {
  56. // log.Fatal("Error reading index", err)
  57. // return
  58. // }
  59. var index Index
  60. err := yaml.Unmarshal([]byte(index_yaml), &index)
  61. if err != nil {
  62. log.Fatal("File reading error", err)
  63. return
  64. }
  65. fmt.Printf("Index: %+v", index)
  66. }
英文:

How do I unmarshal this data? I'm trying to extract the Name, Description, Version and other fields. But when I print out the unmarshaled content I get the following:

> Index: {ApiVersion:v1 Entry:map[]
> Generated:2016-10-06T16:23:20.499029981-06:00}

Here is what I've tried so far.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. const index_yaml = `apiVersion: v1
  8. entries:
  9. alpine:
  10. - created: 2016-10-06T16:23:20.499814565-06:00
  11. description: Deploy a basic Alpine Linux pod
  12. digest: 99c76e403d752c84ead610644d4b1c2f2b453a74b921f422b9dcb8a7c8b559cd
  13. home: https://helm.sh/helm
  14. name: alpine
  15. sources:
  16. - https://github.com/helm/helm
  17. urls:
  18. - https://technosophos.github.io/tscharts/alpine-0.2.0.tgz
  19. version: 0.2.0
  20. - created: 2016-10-06T16:23:20.499543808-06:00
  21. description: Deploy a basic Alpine Linux pod
  22. digest: 515c58e5f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cd78727
  23. home: https://helm.sh/helm
  24. name: alpine
  25. sources:
  26. - https://github.com/helm/helm
  27. urls:
  28. - https://technosophos.github.io/tscharts/alpine-0.1.0.tgz
  29. version: 0.1.0
  30. nginx:
  31. - created: 2016-10-06T16:23:20.499543808-06:00
  32. description: Create a basic nginx HTTP server
  33. digest: aaff4545f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cdffffff
  34. home: https://helm.sh/helm
  35. name: nginx
  36. sources:
  37. - https://github.com/helm/charts
  38. urls:
  39. - https://technosophos.github.io/tscharts/nginx-1.1.0.tgz
  40. version: 1.1.0
  41. generated: 2016-10-06T16:23:20.499029981-06:00`
  42. type Entry struct {
  43. Created string `yaml:"created"`
  44. Description string `yaml:"created"`
  45. Home string `yaml:"home"`
  46. }
  47. type Entries map[string][]Entry
  48. type Index struct {
  49. ApiVersion string `yaml:"apiVersion"`
  50. Entry Entries `yaml:"entry"`
  51. Generated string `yaml:"generated"`
  52. }
  53. func main() {
  54. // data, err := ioutil.ReadFile("index.yaml")
  55. // if err != nil {
  56. // log.Fatal("Error reading index", err)
  57. // return
  58. // }
  59. var index Index
  60. err := yaml.Unmarshal([]byte(index_yaml), &index)
  61. if err != nil {
  62. log.Fatal("File reading error", err)
  63. return
  64. }
  65. fmt.Printf("Index: %+v", index)
  66. }

答案1

得分: 1

这是一个很棒的服务,你可以使用它来为你的YAML文件生成结构体。以下是它的工作结果。

  1. type AutoGenerated struct {
  2. APIVersion string `yaml:"apiVersion"`
  3. Entries struct {
  4. Alpine []struct {
  5. Created struct{} `yaml:"created"`
  6. Description string `yaml:"description"`
  7. Digest string `yaml:"digest"`
  8. Home string `yaml:"home"`
  9. Name string `yaml:"name"`
  10. Sources []string `yaml:"sources"`
  11. Urls []string `yaml:"urls"`
  12. Version string `yaml:"version"`
  13. } `yaml:"alpine"`
  14. Nginx []struct {
  15. Created struct{} `yaml:"created"`
  16. Description string `yaml:"description"`
  17. Digest string `yaml:"digest"`
  18. Home string `yaml:"home"`
  19. Name string `yaml:"name"`
  20. Sources []string `yaml:"sources"`
  21. Urls []string `yaml:"urls"`
  22. Version string `yaml:"version"`
  23. } `yaml:"nginx"`
  24. } `yaml:"entries"`
  25. Generated struct{} `yaml:"generated"`
  26. }
  27. var doc AutoGenerated
  28. err := yaml.Unmarshal([]byte(index_yaml), &doc)
  29. if err != nil {
  30. log.Fatalf("error: %v", err)
  31. return
  32. }
  33. fmt.Printf("Doc: %+v", doc)

你可以在需要的地方添加omitempty,或者根据需要更改结构。

更新:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Entry struct {
  8. Created string `yaml:"created"`
  9. Description string `yaml:"description"`
  10. Digest string `yaml:"digest"`
  11. Home string `yaml:"home"`
  12. Name string `yaml:"name"`
  13. Sources []string `yaml:"sources"`
  14. Urls []string `yaml:"urls"`
  15. Version string `yaml:"version"`
  16. }
  17. type AutoGenerated struct {
  18. APIVersion string `yaml:"apiVersion"`
  19. Entries map[string][]Entry `yaml:"entries"`
  20. Generated string `yaml:"generated"`
  21. }
  22. func main() {
  23. var doc AutoGenerated
  24. err := yaml.Unmarshal([]byte(index_yaml), &doc)
  25. if err != nil {
  26. log.Fatalf("error: %v", err)
  27. return
  28. }
  29. fmt.Printf("Doc: %+v", doc)
  30. }
  31. const index_yaml = `apiVersion: v1
  32. entries:
  33. alpine:
  34. - created: 2016-10-06T16:23:20.499814565-06:00
  35. description: Deploy a basic Alpine Linux pod
  36. digest: 99c76e403d752c84ead610644d4b1c2f2b453a74b921f422b9dcb8a7c8b559cd
  37. home: https://helm.sh/helm
  38. name: alpine
  39. sources:
  40. - https://github.com/helm/helm
  41. urls:
  42. - https://technosophos.github.io/tscharts/alpine-0.2.0.tgz
  43. version: 0.2.0
  44. - created: 2016-10-06T16:23:20.499543808-06:00
  45. description: Deploy a basic Alpine Linux pod
  46. digest: 515c58e5f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cd78727
  47. home: https://helm.sh/helm
  48. name: alpine
  49. sources:
  50. - https://github.com/helm/helm
  51. urls:
  52. - https://technosophos.github.io/tscharts/alpine-0.1.0.tgz
  53. version: 0.1.0
  54. nginx:
  55. - created: 2016-10-06T16:23:20.499543808-06:00
  56. description: Create a basic nginx HTTP server
  57. digest: aaff4545f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cdffffff
  58. home: https://helm.sh/helm
  59. name: nginx
  60. sources:
  61. - https://github.com/helm/charts
  62. urls:
  63. - https://technosophos.github.io/tscharts/nginx-1.1.0.tgz
  64. version: 1.1.0
  65. generated: 2016-10-06T16:23:20.499029981-06:00`
英文:

Here is wonderfull service you can use to make struct for your yamls. This is result of its work.

  1. type AutoGenerated struct {
  2. APIVersion string `yaml:"apiVersion"`
  3. Entries struct {
  4. Alpine []struct {
  5. Created struct {
  6. } `yaml:"created"`
  7. Description string `yaml:"description"`
  8. Digest string `yaml:"digest"`
  9. Home string `yaml:"home"`
  10. Name string `yaml:"name"`
  11. Sources []string `yaml:"sources"`
  12. Urls []string `yaml:"urls"`
  13. Version string `yaml:"version"`
  14. } `yaml:"alpine"`
  15. Nginx []struct {
  16. Created struct {
  17. } `yaml:"created"`
  18. Description string `yaml:"description"`
  19. Digest string `yaml:"digest"`
  20. Home string `yaml:"home"`
  21. Name string `yaml:"name"`
  22. Sources []string `yaml:"sources"`
  23. Urls []string `yaml:"urls"`
  24. Version string `yaml:"version"`
  25. } `yaml:"nginx"`
  26. } `yaml:"entries"`
  27. Generated struct {
  28. } `yaml:"generated"`
  29. }
  30. var doc AutoGenerated
  31. err := yaml.Unmarshal([]byte(index_yaml), &doc)
  32. if err != nil {
  33. log.Fatalf("error: %v", err)
  34. return
  35. }
  36. fmt.Printf("Doc: %+v", doc)

you can add omitempty where you need it, or change structure as you want it.

update:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. type Entry struct {
  8. Created string `yaml:"created"`
  9. Description string `yaml:"description"`
  10. Digest string `yaml:"digest"`
  11. Home string `yaml:"home"`
  12. Name string `yaml:"name"`
  13. Sources []string `yaml:"sources"`
  14. Urls []string `yaml:"urls"`
  15. Version string `yaml:"version"`
  16. }
  17. type AutoGenerated struct {
  18. APIVersion string `yaml:"apiVersion"`
  19. Entries map[string][]Entry `yaml:"entries"`
  20. Generated string `yaml:"generated"`
  21. }
  22. func main() {
  23. var doc AutoGenerated
  24. err := yaml.Unmarshal([]byte(index_yaml), &doc)
  25. if err != nil {
  26. log.Fatalf("error: %v", err)
  27. return
  28. }
  29. fmt.Printf("Doc: %+v", doc)
  30. }
  31. const index_yaml = `apiVersion: v1
  32. entries:
  33. alpine:
  34. - created: 2016-10-06T16:23:20.499814565-06:00
  35. description: Deploy a basic Alpine Linux pod
  36. digest: 99c76e403d752c84ead610644d4b1c2f2b453a74b921f422b9dcb8a7c8b559cd
  37. home: https://helm.sh/helm
  38. name: alpine
  39. sources:
  40. - https://github.com/helm/helm
  41. urls:
  42. - https://technosophos.github.io/tscharts/alpine-0.2.0.tgz
  43. version: 0.2.0
  44. - created: 2016-10-06T16:23:20.499543808-06:00
  45. description: Deploy a basic Alpine Linux pod
  46. digest: 515c58e5f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cd78727
  47. home: https://helm.sh/helm
  48. name: alpine
  49. sources:
  50. - https://github.com/helm/helm
  51. urls:
  52. - https://technosophos.github.io/tscharts/alpine-0.1.0.tgz
  53. version: 0.1.0
  54. nginx:
  55. - created: 2016-10-06T16:23:20.499543808-06:00
  56. description: Create a basic nginx HTTP server
  57. digest: aaff4545f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cdffffff
  58. home: https://helm.sh/helm
  59. name: nginx
  60. sources:
  61. - https://github.com/helm/charts
  62. urls:
  63. - https://technosophos.github.io/tscharts/nginx-1.1.0.tgz
  64. version: 1.1.0
  65. generated: 2016-10-06T16:23:20.499029981-06:00`

答案2

得分: 0

首先,您需要修改您的yaml输入的缩进,然后根据建议将Entry Entriesyaml:"entry"修改为yaml:"entries"

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. const index_yaml = `apiVersion: v1
  8. entries:
  9. alpine:
  10. - created: 2016-10-06T16:23:20.499814565-06:00
  11. description: Deploy a basic Alpine Linux pod
  12. digest: 99c76e403d752c84ead610644d4b1c2f2b453a74b921f422b9dcb8a7c8b559cd
  13. home: https://helm.sh/helm
  14. name: alpine
  15. sources:
  16. - https://github.com/helm/helm
  17. urls:
  18. - https://technosophos.github.io/tscharts/alpine-0.2.0.tgz
  19. version: 0.2.0
  20. - created: 2016-10-06T16:23:20.499543808-06:00
  21. description: Deploy a basic Alpine Linux pod
  22. digest: 515c58e5f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cd78727
  23. home: https://helm.sh/helm
  24. name: alpine
  25. sources:
  26. - https://github.com/helm/helm
  27. urls:
  28. - https://technosophos.github.io/tscharts/alpine-0.1.0.tgz
  29. version: 0.1.0
  30. nginx:
  31. - created: 2016-10-06T16:23:20.499543808-06:00
  32. description: Create a basic nginx HTTP server
  33. digest: aaff4545f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cdffffff
  34. home: https://helm.sh/helm
  35. name: nginx
  36. sources:
  37. - https://github.com/helm/charts
  38. urls:
  39. - https://technosophos.github.io/tscharts/nginx-1.1.0.tgz
  40. version: 1.1.0
  41. generated: 2016-10-06T16:23:20.499029981-06:00`
  42. type Entry struct {
  43. Created string `yaml:"created"`
  44. Description string `yaml:"description"`
  45. Home string `yaml:"home"`
  46. }
  47. type Entries map[string][]Entry
  48. type Index struct {
  49. ApiVersion string `yaml:"apiVersion"`
  50. Entry Entries `yaml:"entries"`
  51. Generated string `yaml:"generated"`
  52. }
  53. func main() {
  54. var index Index
  55. err := yaml.Unmarshal([]byte(index_yaml), &index)
  56. if err != nil {
  57. log.Fatal("File reading error", err)
  58. return
  59. }
  60. fmt.Printf("Index: %+v", index)
  61. }

输出:

  1. Index: {ApiVersion:v1 Entry:map[alpine:[{Created:2016-10-06T16:23:20.499814565-06:00 Description:Deploy a basic Alpine Linux pod Home:https://helm.sh/helm} {Created:2016-10-06T16:23:20.499543808-06:00 Description:Deploy a basic Alpine Linux pod Home:https://helm.sh/helm}] nginx:[{Created:2016-10-06T16:23:20.499543808-06:00 Description:Create a basic nginx HTTP server Home:https://helm.sh/helm}]] Generated:2016-10-06T16:23:20.499029981-06:00}
英文:

Firstly you have to modify the indentation of your yaml input,then modify the Entry Entries from yaml:"entry" to yaml:"entries" as per suggestion:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/yaml.v2"
  6. )
  7. const index_yaml = `apiVersion: v1
  8. entries:
  9. alpine:
  10. - created: 2016-10-06T16:23:20.499814565-06:00
  11. description: Deploy a basic Alpine Linux pod
  12. digest: 99c76e403d752c84ead610644d4b1c2f2b453a74b921f422b9dcb8a7c8b559cd
  13. home: https://helm.sh/helm
  14. name: alpine
  15. sources:
  16. - https://github.com/helm/helm
  17. urls:
  18. - https://technosophos.github.io/tscharts/alpine-0.2.0.tgz
  19. version: 0.2.0
  20. - created: 2016-10-06T16:23:20.499543808-06:00
  21. description: Deploy a basic Alpine Linux pod
  22. digest: 515c58e5f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cd78727
  23. home: https://helm.sh/helm
  24. name: alpine
  25. sources:
  26. - https://github.com/helm/helm
  27. urls:
  28. - https://technosophos.github.io/tscharts/alpine-0.1.0.tgz
  29. version: 0.1.0
  30. nginx:
  31. - created: 2016-10-06T16:23:20.499543808-06:00
  32. description: Create a basic nginx HTTP server
  33. digest: aaff4545f79d8b2913a10cb400ebb6fa9c77fe813287afbacf1a0b897cdffffff
  34. home: https://helm.sh/helm
  35. name: nginx
  36. sources:
  37. - https://github.com/helm/charts
  38. urls:
  39. - https://technosophos.github.io/tscharts/nginx-1.1.0.tgz
  40. version: 1.1.0
  41. generated: 2016-10-06T16:23:20.499029981-06:00`
  42. type Entry struct {
  43. Created string `yaml:"created"`
  44. Description string `yaml:"description"`
  45. Home string `yaml:"home"`
  46. }
  47. type Entries map[string][]Entry
  48. type Index struct {
  49. ApiVersion string `yaml:"apiVersion"`
  50. Entry Entries `yaml:"entries"`
  51. Generated string `yaml:"generated"`
  52. }
  53. func main() {
  54. var index Index
  55. err := yaml.Unmarshal([]byte(index_yaml), &index)
  56. if err != nil {
  57. log.Fatal("File reading error", err)
  58. return
  59. }
  60. fmt.Printf("Index: %+v", index)
  61. }

Output:

  1. Index: {ApiVersion:v1 Entry:map[alpine:[{Created:2016-10-06T16:23:20.499814565-06:00 Description:Deploy a basic Alpine Linux pod Home:https://helm.sh/helm} {Created:2016-10-06T16:23:20.499543808-06:00 Description:Deploy a basic Alpine Linux pod Home:https://helm.sh/helm}] nginx:[{Created:2016-10-06T16:23:20.499543808-06:00 Description:Create a basic nginx HTTP server Home:https://helm.sh/helm}]] Generated:2016-10-06T16:23:20.499029981-06:00}

huangapple
  • 本文由 发表于 2021年8月12日 17:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/68755123.html
匿名

发表评论

匿名网友

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

确定