| 1 | package yamlscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/" |
| 5 | "reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | // StructTagName tag keyword for Marshal/Unmarshal |
| 11 | StructTagNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructTagName. = "yaml" |
| 12 | ) |
| 13 | |
| 14 | // StructField information for each the field in structure |
| 15 | type StructFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField# struct { |
| 16 | FieldNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#FieldName. string |
| 17 | RenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#RenderName. string |
| 18 | AnchorNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#AnchorName. string |
| 19 | AliasNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#AliasName. string |
| 20 | IsAutoAnchorscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsAutoAnchor. bool |
| 21 | IsAutoAliasscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsAutoAlias. bool |
| 22 | IsOmitEmptyscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsOmitEmpty. bool |
| 23 | IsOmitZeroscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsOmitZero. bool |
| 24 | IsFlowscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsFlow. bool |
| 25 | IsInlinescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsInline. bool |
| 26 | } |
| 27 | |
| 28 | func getTagscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/getTag().(field reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#) string { |
| 29 | // If struct tag `yaml` exist, use that. If no `yaml` |
| 30 | // exists, but `json` does, use that and try the best to |
| 31 | // adhere to its rules |
| 32 | tag := field.Tagscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#Tag..Getscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructTag#Get().(StructTagNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructTagName.) |
| 33 | if tag == "" { |
| 34 | tag = field.Tagscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#Tag..Getscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructTag#Get().(`json`) |
| 35 | } |
| 36 | return tag |
| 37 | } |
| 38 | |
| 39 | func structFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/structField().(field reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#) *StructFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField# { |
| 40 | tag := getTagscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/getTag().(field) |
| 41 | fieldName := stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.21.0 strings/ToLower().(field.Namescip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#Name.) |
| 42 | options := stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.21.0 strings/Split().(tag, ",") |
| 43 | if len(options) > 0 { |
| 44 | if options[0] != "" { |
| 45 | fieldName = options[0] |
| 46 | } |
| 47 | } |
| 48 | sf := &StructFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#{ |
| 49 | FieldNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#FieldName.: field.Namescip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#Name., |
| 50 | RenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#RenderName.: fieldName, |
| 51 | } |
| 52 | if len(options) > 1 { |
| 53 | for _, opt := range options[1:] { |
| 54 | switch { |
| 55 | case opt == "omitempty": |
| 56 | sf.IsOmitEmptyscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsOmitEmpty. = true |
| 57 | case opt == "omitzero": |
| 58 | sf.IsOmitZeroscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsOmitZero. = true |
| 59 | case opt == "flow": |
| 60 | sf.IsFlowscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsFlow. = true |
| 61 | case opt == "inline": |
| 62 | sf.IsInlinescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsInline. = true |
| 63 | case stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21.0 strings/HasPrefix().(opt, "anchor"): |
| 64 | anchor := stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.21.0 strings/Split().(opt, "=") |
| 65 | if len(anchor) > 1 { |
| 66 | sf.AnchorNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#AnchorName. = anchor[1] |
| 67 | } else { |
| 68 | sf.IsAutoAnchorscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsAutoAnchor. = true |
| 69 | } |
| 70 | case stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.21.0 strings/HasPrefix().(opt, "alias"): |
| 71 | alias := stringsscip-go gomod github.com/golang/go/src go1.21.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.21.0 strings/Split().(opt, "=") |
| 72 | if len(alias) > 1 { |
| 73 | sf.AliasNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#AliasName. = alias[1] |
| 74 | } else { |
| 75 | sf.IsAutoAliasscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsAutoAlias. = true |
| 76 | } |
| 77 | default: |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return sf |
| 82 | } |
| 83 | |
| 84 | func isIgnoredStructFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/isIgnoredStructField().(field reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/.StructFieldscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#) bool { |
| 85 | if field.PkgPathscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#PkgPath. != "" && !field.Anonymousscip-go gomod github.com/golang/go/src go1.21.0 reflect/StructField#Anonymous. { |
| 86 | // private field |
| 87 | return true |
| 88 | } |
| 89 | return getTagscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/getTag().(field) == "-" |
| 90 | } |
| 91 | |
| 92 | type StructFieldMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap# map[string]*StructFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField# |
| 93 | |
| 94 | func (m StructFieldMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap#) isIncludedRenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap#isIncludedRenderName().(name string) bool { |
| 95 | for _, v := range m { |
| 96 | if !v.IsInlinescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsInline. && v.RenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#RenderName. == name { |
| 97 | return true |
| 98 | } |
| 99 | } |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | func (m StructFieldMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap#) hasMergePropertyscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap#hasMergeProperty().() bool { |
| 104 | for _, v := range m { |
| 105 | if v.IsOmitEmptyscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsOmitEmpty. && v.IsInlinescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsInline. && v.IsAutoAliasscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#IsAutoAlias. { |
| 106 | return true |
| 107 | } |
| 108 | } |
| 109 | return false |
| 110 | } |
| 111 | |
| 112 | func structFieldMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/structFieldMap().(structType reflectscip-go gomod github.com/golang/go/src go1.21.0 reflect/.Typescip-go gomod github.com/golang/go/src go1.21.0 reflect/Type#) (StructFieldMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap#, error) { |
| 113 | fieldMap := StructFieldMapscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructFieldMap#{} |
| 114 | renderNameMap := map[string]struct{}{} |
| 115 | for i := 0; i < structType.NumFieldscip-go gomod github.com/golang/go/src go1.21.0 reflect/Type#NumField().(); i++ { |
| 116 | field := structType.Fieldscip-go gomod github.com/golang/go/src go1.21.0 reflect/Type#Field().(i) |
| 117 | if isIgnoredStructFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/isIgnoredStructField().(field) { |
| 118 | continue |
| 119 | } |
| 120 | sf := structFieldscip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/structField().(field) |
| 121 | if _, exists := renderNameMap[sf.RenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#RenderName.]; exists { |
| 122 | return nil, fmtscip-go gomod github.com/golang/go/src go1.21.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21.0 fmt/Errorf().("duplicated struct field name %s", sf.RenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#RenderName.) |
| 123 | } |
| 124 | fieldMap[sf.FieldNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#FieldName.] = sf |
| 125 | renderNameMap[sf.RenderNamescip-go gomod github.com/goccy/go-yaml . `github.com/goccy/go-yaml`/StructField#RenderName.] = struct{}{} |
| 126 | } |
| 127 | return fieldMap, nil |
| 128 | } |