| 1 | package yaml |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | // StructTagName tag keyword for Marshal/Unmarshal |
| 11 | StructTagName = "yaml" |
| 12 | ) |
| 13 | |
| 14 | // StructField information for each the field in structure |
| 15 | type StructField struct { |
| 16 | FieldName string |
| 17 | RenderName string |
| 18 | AnchorName string |
| 19 | AliasName string |
| 20 | IsAutoAnchor bool |
| 21 | IsAutoAlias bool |
| 22 | IsOmitEmpty bool |
| 23 | IsOmitZero bool |
| 24 | IsFlow bool |
| 25 | IsInline bool |
| 26 | } |
| 27 | |
| 28 | func getTag(field 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.Tag.Get(StructTagName) |
| 33 | if tag == "" { |
| 34 | tag = field.Tag.Get(`json`) |
| 35 | } |
| 36 | return tag |
| 37 | } |
| 38 | |
| 39 | func structField(field reflect.StructField) *StructField { |
| 40 | tag := getTag(field) |
| 41 | fieldName := strings.ToLower(field.Name) |
| 42 | options := strings.Split(tag, ",") |
| 43 | if len(options) > 0 { |
| 44 | if options[0] != "" { |
| 45 | fieldName = options[0] |
| 46 | } |
| 47 | } |
| 48 | sf := &StructField{ |
| 49 | FieldName: field.Name, |
| 50 | RenderName: fieldName, |
| 51 | } |
| 52 | if len(options) > 1 { |
| 53 | for _, opt := range options[1:] { |
| 54 | switch { |
| 55 | case opt == "omitempty": |
| 56 | sf.IsOmitEmpty = true |
| 57 | case opt == "omitzero": |
| 58 | sf.IsOmitZero = true |
| 59 | case opt == "flow": |
| 60 | sf.IsFlow = true |
| 61 | case opt == "inline": |
| 62 | sf.IsInline = true |
| 63 | case strings.HasPrefix(opt, "anchor"): |
| 64 | anchor := strings.Split(opt, "=") |
| 65 | if len(anchor) > 1 { |
| 66 | sf.AnchorName = anchor[1] |
| 67 | } else { |
| 68 | sf.IsAutoAnchor = true |
| 69 | } |
| 70 | case strings.HasPrefix(opt, "alias"): |
| 71 | alias := strings.Split(opt, "=") |
| 72 | if len(alias) > 1 { |
| 73 | sf.AliasName = alias[1] |
| 74 | } else { |
| 75 | sf.IsAutoAlias = true |
| 76 | } |
| 77 | default: |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return sf |
| 82 | } |
| 83 | |
| 84 | func isIgnoredStructField(field reflect.StructField) bool { |
| 85 | if field.PkgPath != "" && !field.Anonymous { |
| 86 | // private field |
| 87 | return true |
| 88 | } |
| 89 | return getTag(field) == "-" |
| 90 | } |
| 91 | |
| 92 | type StructFieldMap map[string]*StructField |
| 93 | |
| 94 | func (m StructFieldMap) isIncludedRenderName(name string) bool { |
| 95 | for _, v := range m { |
| 96 | if !v.IsInline && v.RenderName == name { |
| 97 | return true |
| 98 | } |
| 99 | } |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | func (m StructFieldMap) hasMergeProperty() bool { |
| 104 | for _, v := range m { |
| 105 | if v.IsOmitEmpty && v.IsInline && v.IsAutoAlias { |
| 106 | return true |
| 107 | } |
| 108 | } |
| 109 | return false |
| 110 | } |
| 111 | |
| 112 | func structFieldMap(structType reflect.Type) (StructFieldMap, error) { |
| 113 | fieldMap := StructFieldMap{} |
| 114 | renderNameMap := map[string]struct{}{} |
| 115 | for i := 0; i < structType.NumField(); i++ { |
| 116 | field := structType.Field(i) |
| 117 | if isIgnoredStructField(field) { |
| 118 | continue |
| 119 | } |
| 120 | sf := structField(field) |
| 121 | if _, exists := renderNameMap[sf.RenderName]; exists { |
| 122 | return nil, fmt.Errorf("duplicated struct field name %s", sf.RenderName) |
| 123 | } |
| 124 | fieldMap[sf.FieldName] = sf |
| 125 | renderNameMap[sf.RenderName] = struct{}{} |
| 126 | } |
| 127 | return fieldMap, nil |
| 128 | } |