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 func Packages() []string {
44 var pkgs []string
45 seen := make(map[string]bool)
46 for _, i := range allImplementations {
47 if !seen[i.Package] {
48 pkgs = append(pkgs, i.Package)
49 seen[i.Package] = true
50 }
51 }
52 return pkgs
53 }
54
55
56
57
58 func List(pkg string) []string {
59 var names []string
60 for _, i := range allImplementations {
61 if i.Package == pkg {
62 names = append(names, i.Name)
63 }
64 }
65 return names
66 }
67
68 func available(pkg, name string) bool {
69 for _, i := range allImplementations {
70 if i.Package == pkg && i.Name == name {
71 return i.Available
72 }
73 }
74 panic("unknown implementation")
75 }
76
77
78
79
80 func Select(pkg, name string) bool {
81 if name == "" {
82 for _, i := range allImplementations {
83 if i.Package == pkg {
84 *i.Toggle = false
85 }
86 }
87 return true
88 }
89 if !available(pkg, name) {
90 return false
91 }
92 for _, i := range allImplementations {
93 if i.Package == pkg {
94 *i.Toggle = i.Name == name
95 }
96 }
97 return true
98 }
99
100 func Reset(pkg string) {
101 for _, i := range allImplementations {
102 if i.Package == pkg {
103 *i.Toggle = i.Available
104 return
105 }
106 }
107 }
108
View as plain text