-
Notifications
You must be signed in to change notification settings - Fork 14
/
binary_registry.go
48 lines (39 loc) · 1.36 KB
/
binary_registry.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
// Copyright 2021 Converter Systems LLC. All rights reserved.
package ua
import (
"fmt"
"reflect"
"sync"
)
var (
binaryEncodingTypes sync.Map // map[ExpandedNodeID]reflect.Type
binaryEncodingIDs sync.Map // map[reflect.Type]ExpandedNodeID
)
// RegisterBinaryEncodingID registers the type and id with the BinaryEncoder.
func RegisterBinaryEncodingID(typ reflect.Type, id ExpandedNodeID) {
if t, dup := binaryEncodingTypes.LoadOrStore(id, typ); dup && t != typ {
panic(fmt.Sprintf("RegisterBinaryEncodingID: registering duplicate types for %q: %s != %s", id, t, typ))
}
if n, dup := binaryEncodingIDs.LoadOrStore(typ, id); dup && n != id {
binaryEncodingTypes.Delete(id)
panic(fmt.Sprintf("RegisterBinaryEncodingID: registering duplicate ids for %s: %q != %q", typ, n, id))
}
}
// FindBinaryEncodingIDForType finds the BinaryEncodingID given the type.
func FindBinaryEncodingIDForType(typ reflect.Type) (ExpandedNodeID, bool) {
if val, ok := binaryEncodingIDs.Load(typ); ok {
if id, ok := val.(ExpandedNodeID); ok {
return id, ok
}
}
return NilExpandedNodeID, false
}
// FindTypeForBinaryEncodingID finds the Type given the BinaryEncodingID.
func FindTypeForBinaryEncodingID(id ExpandedNodeID) (reflect.Type, bool) {
if val, ok := binaryEncodingTypes.Load(id); ok {
if typ, ok := val.(reflect.Type); ok {
return typ, ok
}
}
return nil, false
}