forked from go-restruct/restruct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayof_14.go
46 lines (38 loc) · 1.11 KB
/
arrayof_14.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
// +build !go1.5
package restruct
import (
"fmt"
"reflect"
"sync"
)
type arrayTypeKey struct {
count int
elem reflect.Type
}
var arrayTypeMap = map[arrayTypeKey]reflect.Type{}
var arrayTypeMutex = sync.RWMutex{}
// RegisterArrayType registers an array type for use with struct tags. This
// function is only necessary on Go 1.4 and below, which do not provide
// reflect.ArrayOf. This function is goroutine safe and idempotent (i.e.,
// calling it multiple times with the same value is perfectly safe.) If you
// require Go 1.5 or above, you can safely use any array type in your struct
// tags without using this function.
func RegisterArrayType(array interface{}) {
typ := reflect.TypeOf(array)
key := arrayTypeKey{
count: typ.Len(),
elem: typ.Elem(),
}
arrayTypeMutex.Lock()
arrayTypeMap[key] = typ
arrayTypeMutex.Unlock()
}
func arrayOf(count int, elem reflect.Type) reflect.Type {
key := arrayTypeKey{count, elem}
arrayTypeMutex.RLock()
defer arrayTypeMutex.RUnlock()
if typ, ok := arrayTypeMap[key]; ok {
return typ
}
panic(fmt.Errorf("unregistered array type [%d]%s", count, elem.String()))
}