Debian Codegraph @

typeinfo.go

raw symbols SCIP
1 package plist
2
3 import (
4 "reflect"
5 "strings"
6 "sync"
7 )
8
9 // typeInfo holds details for the plist representation of a type.
10 type typeInfo struct {
11 fields []fieldInfo
12 }
13
14 // fieldInfo holds details for the plist representation of a single field.
15 type fieldInfo struct {
16 idx []int
17 name string
18 omitEmpty bool
19 }
20
21 var tinfoMap = make(map[reflect.Type]*typeInfo)
22 var tinfoLock sync.RWMutex
23
24 // getTypeInfo returns the typeInfo structure with details necessary
25 // for marshalling and unmarshalling typ.
26 func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
27 tinfoLock.RLock()
28 tinfo, ok := tinfoMap[typ]
29 tinfoLock.RUnlock()
30 if ok {
31 return tinfo, nil
32 }
33 tinfo = &typeInfo{}
34 if typ.Kind() == reflect.Struct {
35 n := typ.NumField()
36 for i := 0; i < n; i++ {
37 f := typ.Field(i)
38 if f.PkgPath != "" || f.Tag.Get("plist") == "-" {
39 continue // Private field
40 }
41
42 // For embedded structs, embed its fields.
43 if f.Anonymous {
44 t := f.Type
45 if t.Kind() == reflect.Ptr {
46 t = t.Elem()
47 }
48 if t.Kind() == reflect.Struct {
49 inner, err := getTypeInfo(t)
50 if err != nil {
51 return nil, err
52 }
53 for _, finfo := range inner.fields {
54 finfo.idx = append([]int{i}, finfo.idx...)
55 if err := addFieldInfo(typ, tinfo, &finfo); err != nil {
56 return nil, err
57 }
58 }
59 continue
60 }
61 }
62
63 finfo, err := structFieldInfo(typ, &f)
64 if err != nil {
65 return nil, err
66 }
67
68 // Add the field if it doesn't conflict with other fields.
69 if err := addFieldInfo(typ, tinfo, finfo); err != nil {
70 return nil, err
71 }
72 }
73 }
74 tinfoLock.Lock()
75 tinfoMap[typ] = tinfo
76 tinfoLock.Unlock()
77 return tinfo, nil
78 }
79
80 // structFieldInfo builds and returns a fieldInfo for f.
81 func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, error) {
82 finfo := &fieldInfo{idx: f.Index}
83
84 // Split the tag from the xml namespace if necessary.
85 tag := f.Tag.Get("plist")
86
87 // Parse flags.
88 tokens := strings.Split(tag, ",")
89 tag = tokens[0]
90 if len(tokens) > 1 {
91 tag = tokens[0]
92 for _, flag := range tokens[1:] {
93 switch flag {
94 case "omitempty":
95 finfo.omitEmpty = true
96 }
97 }
98 }
99
100 if tag == "" {
101 // If the name part of the tag is completely empty,
102 // use the field name
103 finfo.name = f.Name
104 return finfo, nil
105 }
106
107 finfo.name = tag
108 return finfo, nil
109 }
110
111 // addFieldInfo adds finfo to tinfo.fields if there are no
112 // conflicts, or if conflicts arise from previous fields that were
113 // obtained from deeper embedded structures than finfo. In the latter
114 // case, the conflicting entries are dropped.
115 // A conflict occurs when the path (parent + name) to a field is
116 // itself a prefix of another path, or when two paths match exactly.
117 // It is okay for field paths to share a common, shorter prefix.
118 func addFieldInfo(typ reflect.Type, tinfo *typeInfo, newf *fieldInfo) error {
119 var conflicts []int
120 // First, figure all conflicts. Most working code will have none.
121 for i := range tinfo.fields {
122 oldf := &tinfo.fields[i]
123 if newf.name == oldf.name {
124 conflicts = append(conflicts, i)
125 }
126 }
127
128 // Without conflicts, add the new field and return.
129 if conflicts == nil {
130 tinfo.fields = append(tinfo.fields, *newf)
131 return nil
132 }
133
134 // If any conflict is shallower, ignore the new field.
135 // This matches the Go field resolution on embedding.
136 for _, i := range conflicts {
137 if len(tinfo.fields[i].idx) < len(newf.idx) {
138 return nil
139 }
140 }
141
142 // Otherwise, the new field is shallower, and thus takes precedence,
143 // so drop the conflicting fields from tinfo and append the new one.
144 for c := len(conflicts) - 1; c >= 0; c-- {
145 i := conflicts[c]
146 copy(tinfo.fields[i:], tinfo.fields[i+1:])
147 tinfo.fields = tinfo.fields[:len(tinfo.fields)-1]
148 }
149 tinfo.fields = append(tinfo.fields, *newf)
150 return nil
151 }
152
153 // value returns v's field value corresponding to finfo.
154 // It's equivalent to v.FieldByIndex(finfo.idx), but initializes
155 // and dereferences pointers as necessary.
156 func (finfo *fieldInfo) value(v reflect.Value) reflect.Value {
157 for i, x := range finfo.idx {
158 if i > 0 {
159 t := v.Type()
160 if t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct {
161 if v.IsNil() {
162 v.Set(reflect.New(v.Type().Elem()))
163 }
164 v = v.Elem()
165 }
166 }
167 v = v.Field(x)
168 }
169 return v
170 }