| 1 | # go-yaml fork |
| 2 | |
| 3 | This package is a fork of the go-yaml library and is intended solely for consumption |
| 4 | by kubernetes projects. In this fork, we plan to support only critical changes required for |
| 5 | kubernetes, such as small bug fixes and regressions. Larger, general-purpose feature requests |
| 6 | should be made in the upstream go-yaml library, and we will reject such changes in this fork |
| 7 | unless we are pulling them from upstream. |
| 8 | |
| 9 | This fork is based on v3.0.1: https://github.com/go-yaml/yaml/releases/tag/v3.0.1. |
| 10 | |
| 11 | # YAML support for the Go language |
| 12 | |
| 13 | Introduction |
| 14 | ------------ |
| 15 | |
| 16 | The yaml package enables Go programs to comfortably encode and decode YAML |
| 17 | values. It was developed within [Canonical](https://www.canonical.com) as |
| 18 | part of the [juju](https://juju.ubuntu.com) project, and is based on a |
| 19 | pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML) |
| 20 | C library to parse and generate YAML data quickly and reliably. |
| 21 | |
| 22 | Compatibility |
| 23 | ------------- |
| 24 | |
| 25 | The yaml package supports most of YAML 1.2, but preserves some behavior |
| 26 | from 1.1 for backwards compatibility. |
| 27 | |
| 28 | Specifically, as of v3 of the yaml package: |
| 29 | |
| 30 | - YAML 1.1 bools (_yes/no, on/off_) are supported as long as they are being |
| 31 | decoded into a typed bool value. Otherwise they behave as a string. Booleans |
| 32 | in YAML 1.2 are _true/false_ only. |
| 33 | - Octals encode and decode as _0777_ per YAML 1.1, rather than _0o777_ |
| 34 | as specified in YAML 1.2, because most parsers still use the old format. |
| 35 | Octals in the _0o777_ format are supported though, so new files work. |
| 36 | - Does not support base-60 floats. These are gone from YAML 1.2, and were |
| 37 | actually never supported by this package as it's clearly a poor choice. |
| 38 | |
| 39 | and offers backwards |
| 40 | compatibility with YAML 1.1 in some cases. |
| 41 | 1.2, including support for |
| 42 | anchors, tags, map merging, etc. Multi-document unmarshalling is not yet |
| 43 | implemented, and base-60 floats from YAML 1.1 are purposefully not |
| 44 | supported since they're a poor design and are gone in YAML 1.2. |
| 45 | |
| 46 | Installation and usage |
| 47 | ---------------------- |
| 48 | |
| 49 | The import path for the package is *gopkg.in/yaml.v3*. |
| 50 | |
| 51 | To install it, run: |
| 52 | |
| 53 | go get gopkg.in/yaml.v3 |
| 54 | |
| 55 | API documentation |
| 56 | ----------------- |
| 57 | |
| 58 | If opened in a browser, the import path itself leads to the API documentation: |
| 59 | |
| 60 | - [https://gopkg.in/yaml.v3](https://gopkg.in/yaml.v3) |
| 61 | |
| 62 | API stability |
| 63 | ------------- |
| 64 | |
| 65 | The package API for yaml v3 will remain stable as described in [gopkg.in](https://gopkg.in). |
| 66 | |
| 67 | |
| 68 | License |
| 69 | ------- |
| 70 | |
| 71 | The yaml package is licensed under the MIT and Apache License 2.0 licenses. |
| 72 | Please see the LICENSE file for details. |
| 73 | |
| 74 | |
| 75 | Example |
| 76 | ------- |
| 77 | |
| 78 | ```Go |
| 79 | package main |
| 80 | |
| 81 | import ( |
| 82 | "fmt" |
| 83 | "log" |
| 84 | |
| 85 | "gopkg.in/yaml.v3" |
| 86 | ) |
| 87 | |
| 88 | var data = ` |
| 89 | a: Easy! |
| 90 | b: |
| 91 | c: 2 |
| 92 | d: [3, 4] |
| 93 | ` |
| 94 | |
| 95 | // Note: struct fields must be public in order for unmarshal to |
| 96 | // correctly populate the data. |
| 97 | type T struct { |
| 98 | A string |
| 99 | B struct { |
| 100 | RenamedC int `yaml:"c"` |
| 101 | D []int `yaml:",flow"` |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func main() { |
| 106 | t := T{} |
| 107 | |
| 108 | err := yaml.Unmarshal([]byte(data), &t) |
| 109 | if err != nil { |
| 110 | log.Fatalf("error: %v", err) |
| 111 | } |
| 112 | fmt.Printf("--- t:\n%v\n\n", t) |
| 113 | |
| 114 | d, err := yaml.Marshal(&t) |
| 115 | if err != nil { |
| 116 | log.Fatalf("error: %v", err) |
| 117 | } |
| 118 | fmt.Printf("--- t dump:\n%s\n\n", string(d)) |
| 119 | |
| 120 | m := make(map[interface{}]interface{}) |
| 121 | |
| 122 | err = yaml.Unmarshal([]byte(data), &m) |
| 123 | if err != nil { |
| 124 | log.Fatalf("error: %v", err) |
| 125 | } |
| 126 | fmt.Printf("--- m:\n%v\n\n", m) |
| 127 | |
| 128 | d, err = yaml.Marshal(&m) |
| 129 | if err != nil { |
| 130 | log.Fatalf("error: %v", err) |
| 131 | } |
| 132 | fmt.Printf("--- m dump:\n%s\n\n", string(d)) |
| 133 | } |
| 134 | ``` |
| 135 | |
| 136 | This example will generate the following output: |
| 137 | |
| 138 | ``` |
| 139 | --- t: |
| 140 | {Easy! {2 [3 4]}} |
| 141 | |
| 142 | --- t dump: |
| 143 | a: Easy! |
| 144 | b: |
| 145 | c: 2 |
| 146 | d: [3, 4] |
| 147 | |
| 148 | |
| 149 | --- m: |
| 150 | map[a:Easy! b:map[c:2 d:[3 4]]] |
| 151 | |
| 152 | --- m dump: |
| 153 | a: Easy! |
| 154 | b: |
| 155 | c: 2 |
| 156 | d: |
| 157 | - 3 |
| 158 | - 4 |
| 159 | ``` |
| 160 |