-
-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathtype_info.go
54 lines (47 loc) · 985 Bytes
/
type_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package openapi3gen
import (
"reflect"
"sort"
"sync"
)
var (
typeInfos = map[reflect.Type]*theTypeInfo{}
typeInfosMutex sync.RWMutex
)
// theTypeInfo contains information about JSON serialization of a type
type theTypeInfo struct {
Type reflect.Type
Fields []theFieldInfo
}
// getTypeInfo returns theTypeInfo for the given type.
func getTypeInfo(t reflect.Type) *theTypeInfo {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
typeInfosMutex.RLock()
typeInfo, exists := typeInfos[t]
typeInfosMutex.RUnlock()
if exists {
return typeInfo
}
if t.Kind() != reflect.Struct {
typeInfo = &theTypeInfo{
Type: t,
}
} else {
// Allocate
typeInfo = &theTypeInfo{
Type: t,
Fields: make([]theFieldInfo, 0, 16),
}
// Add fields
typeInfo.Fields = appendFields(nil, nil, t)
// Sort fields
sort.Sort(sortableFieldInfos(typeInfo.Fields))
}
// Publish
typeInfosMutex.Lock()
typeInfos[t] = typeInfo
typeInfosMutex.Unlock()
return typeInfo
}