1
2
3
4
5
6
7 package impl
8
9 import "strings"
10
11 type implementation struct {
12 Package string
13 Name string
14 Available bool
15 Toggle *bool
16 }
17
18 var allImplementations []implementation
19
20
21
22
23
24
25
26
27
28
29 func Register(pkg, name string, available *bool) {
30 if strings.Contains(pkg, "/") {
31 panic("impl: package name must not contain slashes")
32 }
33 allImplementations = append(allImplementations, implementation{
34 Package: pkg,
35 Name: name,
36 Available: *available,
37 Toggle: available,
38 })
39 }
40
41
42
43
44 func List(pkg string) []string {
45 var names []string
46 for _, i := range allImplementations {
47 if i.Package == pkg {
48 names = append(names, i.Name)
49 }
50 }
51 return names
52 }
53
54 func available(pkg, name string) bool {
55 for _, i := range allImplementations {
56 if i.Package == pkg && i.Name == name {
57 return i.Available
58 }
59 }
60 panic("unknown implementation")
61 }
62
63
64
65
66 func Select(pkg, name string) bool {
67 if name == "" {
68 for _, i := range allImplementations {
69 if i.Package == pkg {
70 *i.Toggle = false
71 }
72 }
73 return true
74 }
75 if !available(pkg, name) {
76 return false
77 }
78 for _, i := range allImplementations {
79 if i.Package == pkg {
80 *i.Toggle = i.Name == name
81 }
82 }
83 return true
84 }
85
86 func Reset(pkg string) {
87 for _, i := range allImplementations {
88 if i.Package == pkg {
89 *i.Toggle = i.Available
90 return
91 }
92 }
93 }
94
View as plain text