-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement.go
84 lines (76 loc) · 2.17 KB
/
implement.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package jwt
//unused
type implementation struct {
Package string
Name string
Available bool
Toggle *bool
}
var allImplementations []implementation
// Register records an alternative implementation of a cryptographic primitive.
// The implementation might be available or not based on CPU support. If
// available is false, the implementation is unavailable and can't be tested on
// this machine. If available is true, it can be set to false to disable the
// implementation. If all alternative implementations but one are disabled, the
// remaining one must be used (i.e. disabling one implementation must not
// implicitly disable any other). Each package has an implicit base
// implementation that is selected when all alternatives are unavailable or
// disabled.
func Register(pkg, name string, available *bool) {
allImplementations = append(allImplementations, implementation{
Package: pkg,
Name: name,
Available: *available,
Toggle: available,
})
}
// List returns the names of all alternative implementations registered for the
// given package, whether available or not. The implicit base implementation is
// not included.
func List(pkg string) []string {
var names []string
for _, i := range allImplementations {
if i.Package == pkg {
names = append(names, i.Name)
}
}
return names
}
func available(pkg, name string) bool {
for _, i := range allImplementations {
if i.Package == pkg && i.Name == name {
return i.Available
}
}
panic("unknown implementation")
}
// Select disables all implementations for the given package except the one
// with the given name. If name is empty, the base implementation is selected.
// It returns whether the selected implementation is available.
func Select(pkg, name string) bool {
if name == "" {
for _, i := range allImplementations {
if i.Package == pkg {
*i.Toggle = false
}
}
return true
}
if !available(pkg, name) {
return false
}
for _, i := range allImplementations {
if i.Package == pkg {
*i.Toggle = i.Name == name
}
}
return true
}
func Reset(pkg string) {
for _, i := range allImplementations {
if i.Package == pkg {
*i.Toggle = i.Available
return
}
}
}