Debian Codegraph @

api/v1/lib/extras/store/singleton.go

raw symbols SCIP
1 package store
2
3 import (
4 "sync/atomic"
5 )
6
7 type (
8 Getter interface {
9 Get() (string, error)
10 }
11
12 GetFunc func() (string, error)
13
14 Setter interface {
15 Set(string) error
16 }
17
18 SetFunc func(string) error
19
20 // Singleton is a thread-safe abstraction to load and store a string
21 Singleton interface {
22 Getter
23 Setter
24 }
25
26 SingletonAdapter struct {
27 GetFunc
28 SetFunc
29 }
30
31 SingletonDecorator interface {
32 Decorate(Singleton) Singleton
33 }
34
35 Error string
36 )
37
38 func (err Error) Error() string { return string(err) }
39
40 const ErrNotFound = Error("value not found in store")
41
42 func (f GetFunc) Get() (string, error) { return f() }
43 func (f SetFunc) Set(s string) error { return f(s) }
44
45 func NewInMemorySingleton() Singleton {
46 var value atomic.Value
47 return &SingletonAdapter{
48 func() (string, error) {
49 x := value.Load()
50 if x == nil {
51 return "", ErrNotFound
52 }
53 return x.(string), nil
54 },
55 func(s string) error {
56 value.Store(s)
57 return nil
58 },
59 }
60 }
61
62 type (
63 GetFuncDecorator func(Getter, string, error) (string, error)
64 SetFuncDecorator func(Setter, string, error) error
65 )
66
67 func DoSet() SetFuncDecorator {
68 return func(s Setter, v string, _ error) error {
69 return s.Set(v)
70 }
71 }
72
73 func (f SetFuncDecorator) AndThen(f2 SetFuncDecorator) SetFuncDecorator {
74 return func(s Setter, v string, err error) error {
75 err = f(s, v, err)
76 if err != nil {
77 return err
78 }
79 return f2(s, v, nil)
80 }
81 }
82
83 func (f SetFuncDecorator) Decorate(s Singleton) Singleton {
84 if f == nil {
85 return s
86 }
87 return &SingletonAdapter{
88 s.Get,
89 SetFunc(func(v string) error {
90 return f(s, v, nil)
91 }),
92 }
93 }
94
95 func DoGet() GetFuncDecorator {
96 return func(s Getter, _ string, _ error) (string, error) {
97 return s.Get()
98 }
99 }
100
101 func (f GetFuncDecorator) AndThen(f2 GetFuncDecorator) GetFuncDecorator {
102 return func(s Getter, v string, err error) (string, error) {
103 v, err = f(s, v, err)
104 if err != nil {
105 return v, err
106 }
107 return f2(s, v, nil)
108 }
109 }
110
111 func (f GetFuncDecorator) Decorate(s Singleton) Singleton {
112 if f == nil {
113 return s
114 }
115 return &SingletonAdapter{
116 GetFunc(func() (string, error) {
117 return f(s, "", nil)
118 }),
119 s.Set,
120 }
121 }
122
123 func DecorateSingleton(s Singleton, ds ...SingletonDecorator) Singleton {
124 for _, d := range ds {
125 if d != nil {
126 s = d.Decorate(s)
127 }
128 }
129 return s
130 }
131
132 // GetOrPanic curries the result of a Getter invocation: the returned func only ever returns the string component when
133 // the error component of the underlying Get() call is nil. If Get() generates an error then the curried func panics.
134 func GetOrPanic(g Getter) func() string {
135 return func() string {
136 v, err := g.Get()
137 if err != nil {
138 panic(err)
139 }
140 return v
141 }
142 }
143
144 func GetIgnoreErrors(g Getter) func() string {
145 return func() string {
146 v, _ := g.Get()
147 return v
148 }
149 }
150
151 // SetOrPanic curries the result of a Setter invocation: the returned func only ever returns normally when the error
152 // component of the underlying Set() call is nil. If Set() generates an error then the curried func panics.
153 func SetOrPanic(s Setter) func(v string) {
154 return func(v string) {
155 err := s.Set(v)
156 if err != nil {
157 panic(err)
158 }
159 }
160 }