| 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 |
IsFlow bool |
| 24 |
IsInline bool |
| 25 |
} |
| 26 |
|
| 27 |
func getTag(field reflect.StructField) string { |
| 28 |
// If struct tag `yaml` exist, use that. If no `yaml` |
| 29 |
// exists, but `json` does, use that and try the best to |
| 30 |
// adhere to its rules |
| 31 |
tag := field.Tag.Get(StructTagName) |
| 32 |
if tag == "" { |
| 33 |
tag = field.Tag.Get(`json`) |
| 34 |
} |
| 35 |
return tag |
| 36 |
} |
| 37 |
|
| 38 |
func structField(field reflect.StructField) *StructField { |
| 39 |
tag := getTag(field) |
| 40 |
fieldName := strings.ToLower(field.Name) |
| 41 |
options := strings.Split(tag, ",") |
| 42 |
if len(options) > 0 { |
| 43 |
if options[0] != "" { |
| 44 |
fieldName = options[0] |
| 45 |
} |
| 46 |
} |
| 47 |
structField := &StructField{ |
| 48 |
FieldName: field.Name, |
| 49 |
RenderName: fieldName, |
| 50 |
} |
| 51 |
if len(options) > 1 { |
| 52 |
for _, opt := range options[1:] { |
| 53 |
switch { |
| 54 |
case opt == "omitempty": |
| 55 |
structField.IsOmitEmpty = true |
| 56 |
case opt == "flow": |
| 57 |
structField.IsFlow = true |
| 58 |
case opt == "inline": |
| 59 |
structField.IsInline = true |
| 60 |
case strings.HasPrefix(opt, "anchor"): |
| 61 |
anchor := strings.Split(opt, "=") |
| 62 |
if len(anchor) > 1 { |
| 63 |
structField.AnchorName = anchor[1] |
| 64 |
} else { |
| 65 |
structField.IsAutoAnchor = true |
| 66 |
} |
| 67 |
case strings.HasPrefix(opt, "alias"): |
| 68 |
alias := strings.Split(opt, "=") |
| 69 |
if len(alias) > 1 { |
| 70 |
structField.AliasName = alias[1] |
| 71 |
} else { |
| 72 |
structField.IsAutoAlias = true |
| 73 |
} |
| 74 |
default: |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
return structField |
| 79 |
} |
| 80 |
|
| 81 |
func isIgnoredStructField(field reflect.StructField) bool { |
| 82 |
if field.PkgPath != "" && !field.Anonymous { |
| 83 |
// private field |
| 84 |
return true |
| 85 |
} |
| 86 |
return getTag(field) == "-" |
| 87 |
} |
| 88 |
|
| 89 |
type StructFieldMap map[string]*StructField |
| 90 |
|
| 91 |
func (m StructFieldMap) isIncludedRenderName(name string) bool { |
| 92 |
for _, v := range m { |
| 93 |
if !v.IsInline && v.RenderName == name { |
| 94 |
return true |
| 95 |
} |
| 96 |
} |
| 97 |
return false |
| 98 |
} |
| 99 |
|
| 100 |
func (m StructFieldMap) hasMergeProperty() bool { |
| 101 |
for _, v := range m { |
| 102 |
if v.IsOmitEmpty && v.IsInline && v.IsAutoAlias { |
| 103 |
return true |
| 104 |
} |
| 105 |
} |
| 106 |
return false |
| 107 |
} |
| 108 |
|
| 109 |
func structFieldMap(structType reflect.Type) (StructFieldMap, error) { |
| 110 |
structFieldMap := StructFieldMap{} |
| 111 |
renderNameMap := map[string]struct{}{} |
| 112 |
for i := 0; i < structType.NumField(); i++ { |
| 113 |
field := structType.Field(i) |
| 114 |
if isIgnoredStructField(field) { |
| 115 |
continue |
| 116 |
} |
| 117 |
structField := structField(field) |
| 118 |
if _, exists := renderNameMap[structField.RenderName]; exists { |
| 119 |
return nil, fmt.Errorf("duplicated struct field name %s", structField.RenderName) |
| 120 |
} |
| 121 |
structFieldMap[structField.FieldName] = structField |
| 122 |
renderNameMap[structField.RenderName] = struct{}{} |
| 123 |
} |
| 124 |
return structFieldMap, nil |
| 125 |
} |