{"id":43090,"date":"2024-12-01T11:31:25","date_gmt":"2024-12-01T03:31:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43090\/"},"modified":"2024-12-01T11:31:25","modified_gmt":"2024-12-01T03:31:25","slug":"%e4%bd%bf%e7%94%a8%e7%bb%93%e6%9e%84%e8%a7%a3%e6%9e%90-yaml","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43090\/","title":{"rendered":"\u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML<\/span><\/p>\n<h1>\u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 13:15:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u521b\u5efa\u4e86\u4ee5\u4e0b yaml \u6587\u4ef6\u6765\u63d0\u4f9b\u7528\u6237\u9700\u8981\u63d0\u4f9b\u7684\u4e00\u4e9b\u914d\u7f6e\uff1a<\/p>\n<pre>environments:\n sys1:\n    models:\n    - app-type: app1\n      service-type: \u201cfds\"\n\n    - app-type: app2\n      service-type: \u201cera\u201d\n sys2:\n    models:\n    - app-type: app1\n      service-type: \u201cfds\"\n\n    - app-type: app2\n      service-type: \u201cera\"<\/pre>\n<p>https:\/\/codebeautify.org\/yaml-validator\/cbb349ec<\/p>\n<p>\u8fd9\u91cc\u6211\u6709\uff1a<\/p>\n<ol>\n<li>\u4e00\u4e2a\u73af\u5883\uff08\u6839\uff09<\/li>\n<li>\u73af\u5883\u5305\u542b 1..n <code>sys<\/code><\/li>\n<li>\u6bcf\u4e2a <code>sys<\/code> \u5305\u542b 1..n \u4e2a\u5177\u6709\u5173\u952e\u5e94\u7528\u7c7b\u578b\u7684\u6a21\u578b\u5b9e\u4f8b<\/li>\n<\/ol>\n<p>\u73b0\u5728\u6211\u9700\u8981\u89e3\u6790\u8fd9\u4e2a yaml \u6587\u4ef6\uff0c\u56e0\u6b64\u6211\u5c1d\u8bd5\u6784\u5efa\u4e00\u4e2a\u7ed3\u6784\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>type environment struct {\n    environment [] sys\n}\n\ntype sys struct{\n    models    []properties\n}\n\ntype models struct{\n    app-type     string      `yaml:\"app-type\"`\n    service-type string      `yaml:\"service-type\"`\n}<\/pre>\n<p>\u73b0\u5728\u6211\u5c1d\u8bd5\u89e3\u6790\u6b64 yaml\uff0c\u4f46\u6536\u5230\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u7684\u9519\u8bef\u3002<\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\uff1a<\/p>\n<pre>1. do i model the yaml correctly?\n2. do i model the struct correctly?<\/pre>\n<p>\u8fd9\u662f\u4ee3\u7801\uff1a<\/p>\n<pre>func main() {\n    y := Environments{}\n\n    err := yaml.Unmarshal([]byte(data), &amp;y)\n    if err != nil {\n        log.Fatalf(\"error: %v\", err)\n    }\n    fmt.Printf(\"%+v\\n\", y)\n}<\/pre>\n<p>\u6570\u636e\u662f<code>yaml.file<\/code>\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bd5\u8bd5\u8fd9\u4e2a\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n\n    \"gopkg.in\/yaml.v2\"\n)\n\ntype message struct {\n    environments map[string]models `yaml:\"environments\"`\n}\ntype models map[string][]model\ntype model struct {\n    apptype     string `yaml:\"app-type\"`\n    servicetype string `yaml:\"service-type\"`\n}\n\nfunc main() {\n    data := []byte(`\nenvironments:\n sys1:\n    models:\n    - app-type: app1\n      service-type: fds\n    - app-type: app2\n      service-type: era\n sys2:\n    models:\n    - app-type: app1\n      service-type: fds\n    - app-type: app2\n      service-type: era\n`)\n    y := message{}\n\n    err := yaml.unmarshal([]byte(data), &amp;y)\n    if err != nil {\n        log.fatalf(\"error: %v\", err)\n    }\n    fmt.printf(\"%+v\\n\", y)\n\n}<\/pre>\n<p>\u5982\u679c\u60a8\u5728 kubernetes \u73af\u5883 (k8s) \u4e2d\u5de5\u4f5c\uff0c\u6211\u5efa\u8bae\u60a8\u67e5\u770b <code>github.com\/kubernetes-sigs\/yaml<\/code> \u5305\uff0c\u800c\u4e0d\u662f <code>gopkg.in\/yaml.v2<\/code>\u3002\u7528\u6cd5\u662f\u76f8\u540c\u7684\uff0c\u4f46\u5b83\u67e5\u770b\u7ed3\u6784\u7684 json \u6807\u8bb0\uff08\u6bd4 yaml \u6807\u8bb0\u66f4\u5e7f\u6cdb\uff09\u3002\u8fd9\u4f7f\u5f97\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6 yaml\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u539f\u751f go \u7ed3\u6784\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613<\/p>\n<pre>package something\n\nimport (\n  \"io\/ioutil\"\n\n   grafana \"github.com\/grafana-operator\/grafana-operator\/v4\/api\/integreatly\/v1alpha1\"\n\n   \"k8s.io\/apimachinery\/pkg\/types\"\n   \"sigs.k8s.io\/yaml\"\n)\n\nfunc getDefaultGrafanaForNS(ns string) (grafana.Grafana, error) {\n    yfile, err := ioutil.ReadFile(\"grafana.yml\")\n\n    if err != nil {\n\n        return grafana.Grafana{}, err\n    }\n    result := grafana.Grafana{}\n    err = yaml.Unmarshal([]byte(yfile), &amp;result)\n    if err != nil {\n        return grafana.Grafana{}, err\n    }\n    return result, nil\n\n}<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML \u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML \u6765\u6e90\uff1astackoverflow 2024-04-29 13:15:34 0\u6d4f\u89c8 \u6536\u85cf \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300a\u4f7f\u7528\u7ed3\u6784\u89e3\u6790 YAML\u300b\uff0c\u8986\u76d6\u4e86Golang\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 \u95ee\u9898\u5185\u5bb9 \u6211\u521b\u5efa\u4e86\u4ee5\u4e0b yaml \u6587\u4ef6\u6765\u63d0\u4f9b\u7528\u6237\u9700\u8981\u63d0\u4f9b\u7684\u4e00\u4e9b\u914d\u7f6e\uff1a environments: sys1: models: &#8211; app-type: app1 service-type: \u201cfds&#8221; &#8211; app-type: app2 service-type: \u201cera\u201d sys2: models: &#8211; app-type: app1 service-type: \u201cfds&#8221; &#8211; app-type: app2 service-type: \u201cera&#8221; https:\/\/codebeautify.org\/yaml-validator\/cbb349ec \u8fd9\u91cc\u6211\u6709\uff1a \u4e00\u4e2a\u73af\u5883\uff08\u6839\uff09 \u73af\u5883\u5305\u542b 1..n sys \u6bcf\u4e2a sys \u5305\u542b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43090","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43090"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43090\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}