| 1 | // Package yaml can marshal and unmarshal cty values in YAML format. |
| 2 | package yamlscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/ |
| 3 | |
| 4 | import ( |
| 5 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 7 | "reflectscip-go gomod github.com/golang/go/src go1.16 reflect/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 9 | "syncscip-go gomod github.com/golang/go/src go1.16 sync/" |
| 10 | |
| 11 | "github.com/zclconf/go-cty/ctyscip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/" |
| 12 | ) |
| 13 | |
| 14 | // Unmarshal reads the document found within the given source buffer |
| 15 | // and attempts to convert it into a value conforming to the given type |
| 16 | // constraint. |
| 17 | // |
| 18 | // This is an alias for Unmarshal on the predefined Converter in "Standard". |
| 19 | // |
| 20 | // An error is returned if the given source contains any YAML document |
| 21 | // delimiters. |
| 22 | func Unmarshalscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Unmarshal().(src []byte, ty ctyscip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/Type#) (ctyscip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/Value#, error) { |
| 23 | return Standardscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Standard..Unmarshalscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Converter#Unmarshal().(src, ty) |
| 24 | } |
| 25 | |
| 26 | // Marshal serializes the given value into a YAML document, using a fixed |
| 27 | // mapping from cty types to YAML constructs. |
| 28 | // |
| 29 | // This is an alias for Marshal on the predefined Converter in "Standard". |
| 30 | // |
| 31 | // Note that unlike the function of the same name in the cty JSON package, |
| 32 | // this does not take a type constraint and therefore the YAML serialization |
| 33 | // cannot preserve late-bound type information in the serialization to be |
| 34 | // recovered from Unmarshal. Instead, any cty.DynamicPseudoType in the type |
| 35 | // constraint given to Unmarshal will be decoded as if the corresponding portion |
| 36 | // of the input were processed with ImpliedType to find a target type. |
| 37 | func Marshalscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Marshal().(v ctyscip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/Value#) ([]byte, error) { |
| 38 | return Standardscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Standard..Marshalscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Converter#Marshal().(v) |
| 39 | } |
| 40 | |
| 41 | // ImpliedType analyzes the given source code and returns a suitable type that |
| 42 | // it could be decoded into. |
| 43 | // |
| 44 | // For a converter that is using standard YAML rather than cty-specific custom |
| 45 | // tags, only a subset of cty types can be produced: strings, numbers, bools, |
| 46 | // tuple types, and object types. |
| 47 | // |
| 48 | // This is an alias for ImpliedType on the predefined Converter in "Standard". |
| 49 | func ImpliedTypescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/ImpliedType().(src []byte) (ctyscip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty v1.0.0 `github.com/zclconf/go-cty/cty`/Type#, error) { |
| 50 | return Standardscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Standard..ImpliedTypescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/Converter#ImpliedType().(src) |
| 51 | } |
| 52 | |
| 53 | func handleErrscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/handleErr().(err *error) { |
| 54 | if v := recover(); v != nil { |
| 55 | if e, ok := v.(yamlErrorscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/yamlError#); ok { |
| 56 | *err = e.errscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/yamlError#err. |
| 57 | } else { |
| 58 | panic(v) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | type yamlErrorscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/yamlError# struct { |
| 64 | errscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/yamlError#err. error |
| 65 | } |
| 66 | |
| 67 | func failscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fail().(err error) { |
| 68 | panic(yamlErrorscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/yamlError#{err}) |
| 69 | } |
| 70 | |
| 71 | func failfscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/failf().(format string, args ...interface{}) { |
| 72 | panic(yamlErrorscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/yamlError#{fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("yaml: "+format, args...)}) |
| 73 | } |
| 74 | |
| 75 | // -------------------------------------------------------------------------- |
| 76 | // Maintain a mapping of keys to structure field indexes |
| 77 | |
| 78 | // The code in this section was copied from mgo/bson. |
| 79 | |
| 80 | // structInfo holds details for the serialization of fields of |
| 81 | // a given struct. |
| 82 | type structInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo# struct { |
| 83 | FieldsMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#FieldsMap. map[string]fieldInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo# |
| 84 | FieldsListscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#FieldsList. []fieldInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo# |
| 85 | |
| 86 | // InlineMap is the number of the field in the struct that |
| 87 | // contains an ,inline map, or -1 if there's none. |
| 88 | InlineMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#InlineMap. int |
| 89 | } |
| 90 | |
| 91 | type fieldInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo# struct { |
| 92 | Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key. string |
| 93 | Numscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Num. int |
| 94 | OmitEmptyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#OmitEmpty. bool |
| 95 | Flowscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Flow. bool |
| 96 | // Id holds the unique field identifier, so we can cheaply |
| 97 | // check for field duplicates without maintaining an extra map. |
| 98 | Idscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Id. int |
| 99 | |
| 100 | // Inline holds the field index if the field is part of an inlined struct. |
| 101 | Inlinescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Inline. []int |
| 102 | } |
| 103 | |
| 104 | var structMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structMap. = make(map[reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.Typescip-go gomod github.com/golang/go/src go1.16 reflect/Type#]*structInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#) |
| 105 | var fieldMapMutexscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldMapMutex. syncscip-go gomod github.com/golang/go/src go1.16 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.16 sync/RWMutex# |
| 106 | |
| 107 | func getStructInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/getStructInfo().(st reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.Typescip-go gomod github.com/golang/go/src go1.16 reflect/Type#) (*structInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#, error) { |
| 108 | fieldMapMutexscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldMapMutex..RLockscip-go gomod github.com/golang/go/src go1.16 sync/RWMutex#RLock().() |
| 109 | sinfo, found := structMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structMap.[st] |
| 110 | fieldMapMutexscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldMapMutex..RUnlockscip-go gomod github.com/golang/go/src go1.16 sync/RWMutex#RUnlock().() |
| 111 | if found { |
| 112 | return sinfo, nil |
| 113 | } |
| 114 | |
| 115 | n := st.NumFieldscip-go gomod github.com/golang/go/src go1.16 reflect/Type#NumField().() |
| 116 | fieldsMap := make(map[string]fieldInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#) |
| 117 | fieldsList := make([]fieldInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#, 0, n) |
| 118 | inlineMap := -1 |
| 119 | for i := 0; i != n; i++ { |
| 120 | field := st.Fieldscip-go gomod github.com/golang/go/src go1.16 reflect/Type#Field().(i) |
| 121 | if field.PkgPathscip-go gomod github.com/golang/go/src go1.16 reflect/StructField#PkgPath. != "" && !field.Anonymousscip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Anonymous. { |
| 122 | continue // Private field |
| 123 | } |
| 124 | |
| 125 | info := fieldInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#{Numscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Num.: i} |
| 126 | |
| 127 | tag := field.Tagscip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Tag..Getscip-go gomod github.com/golang/go/src go1.16 reflect/StructTag#Get().("yaml") |
| 128 | if tag == "" && stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Indexscip-go gomod github.com/golang/go/src go1.16 strings/Index().(string(field.Tagscip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Tag.), ":") < 0 { |
| 129 | tag = string(field.Tagscip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Tag.) |
| 130 | } |
| 131 | if tag == "-" { |
| 132 | continue |
| 133 | } |
| 134 | |
| 135 | inline := false |
| 136 | fields := stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Splitscip-go gomod github.com/golang/go/src go1.16 strings/Split().(tag, ",") |
| 137 | if len(fields) > 1 { |
| 138 | for _, flag := range fields[1:] { |
| 139 | switch flag { |
| 140 | case "omitempty": |
| 141 | info.OmitEmptyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#OmitEmpty. = true |
| 142 | case "flow": |
| 143 | info.Flowscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Flow. = true |
| 144 | case "inline": |
| 145 | inline = true |
| 146 | default: |
| 147 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("Unsupported flag %q in tag %q of type %s", flag, tag, st)) |
| 148 | } |
| 149 | } |
| 150 | tag = fields[0] |
| 151 | } |
| 152 | |
| 153 | if inline { |
| 154 | switch field.Typescip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Type..Kindscip-go gomod github.com/golang/go/src go1.16 reflect/Type#Kind().() { |
| 155 | case reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.Mapscip-go gomod github.com/golang/go/src go1.16 reflect/Map.: |
| 156 | if inlineMap >= 0 { |
| 157 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("Multiple ,inline maps in struct " + st.Stringscip-go gomod github.com/golang/go/src go1.16 reflect/Type#String().()) |
| 158 | } |
| 159 | if field.Typescip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Type..Keyscip-go gomod github.com/golang/go/src go1.16 reflect/Type#Key().() != reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.16 reflect/TypeOf().("") { |
| 160 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("Option ,inline needs a map with string keys in struct " + st.Stringscip-go gomod github.com/golang/go/src go1.16 reflect/Type#String().()) |
| 161 | } |
| 162 | inlineMap = info.Numscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Num. |
| 163 | case reflectscip-go gomod github.com/golang/go/src go1.16 reflect/.Structscip-go gomod github.com/golang/go/src go1.16 reflect/Struct.: |
| 164 | sinfo, err := getStructInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/getStructInfo().(field.Typescip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Type.) |
| 165 | if err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | for _, finfo := range sinfo.FieldsListscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#FieldsList. { |
| 169 | if _, found := fieldsMap[finfo.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key.]; found { |
| 170 | msg := "Duplicated key '" + finfo.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key. + "' in struct " + st.Stringscip-go gomod github.com/golang/go/src go1.16 reflect/Type#String().() |
| 171 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().(msg) |
| 172 | } |
| 173 | if finfo.Inlinescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Inline. == nil { |
| 174 | finfo.Inlinescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Inline. = []int{i, finfo.Numscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Num.} |
| 175 | } else { |
| 176 | finfo.Inlinescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Inline. = append([]int{i}, finfo.Inlinescip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Inline....) |
| 177 | } |
| 178 | finfo.Idscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Id. = len(fieldsList) |
| 179 | fieldsMap[finfo.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key.] = finfo |
| 180 | fieldsList = append(fieldsList, finfo) |
| 181 | } |
| 182 | default: |
| 183 | //return nil, errors.New("Option ,inline needs a struct value or map field") |
| 184 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("Option ,inline needs a struct value field") |
| 185 | } |
| 186 | continue |
| 187 | } |
| 188 | |
| 189 | if tag != "" { |
| 190 | info.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key. = tag |
| 191 | } else { |
| 192 | info.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key. = stringsscip-go gomod github.com/golang/go/src go1.16 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.16 strings/ToLower().(field.Namescip-go gomod github.com/golang/go/src go1.16 reflect/StructField#Name.) |
| 193 | } |
| 194 | |
| 195 | if _, found = fieldsMap[info.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key.]; found { |
| 196 | msg := "Duplicated key '" + info.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key. + "' in struct " + st.Stringscip-go gomod github.com/golang/go/src go1.16 reflect/Type#String().() |
| 197 | return nil, errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().(msg) |
| 198 | } |
| 199 | |
| 200 | info.Idscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Id. = len(fieldsList) |
| 201 | fieldsList = append(fieldsList, info) |
| 202 | fieldsMap[info.Keyscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldInfo#Key.] = info |
| 203 | } |
| 204 | |
| 205 | sinfo = &structInfoscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#{ |
| 206 | FieldsMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#FieldsMap.: fieldsMap, |
| 207 | FieldsListscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#FieldsList.: fieldsList, |
| 208 | InlineMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structInfo#InlineMap.: inlineMap, |
| 209 | } |
| 210 | |
| 211 | fieldMapMutexscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldMapMutex..Lockscip-go gomod github.com/golang/go/src go1.16 sync/RWMutex#Lock().() |
| 212 | structMapscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/structMap.[st] = sinfo |
| 213 | fieldMapMutexscip-go gomod github.com/zclconf/go-cty-yaml . `github.com/zclconf/go-cty-yaml`/fieldMapMutex..Unlockscip-go gomod github.com/golang/go/src go1.16 sync/RWMutex#Unlock().() |
| 214 | return sinfo, nil |
| 215 | } |