Skip to content

Commit 8802a9d

Browse files
committed
Replace all occurences of "interface{}" with "any"
1 parent 63ef5bd commit 8802a9d

File tree

12 files changed

+58
-58
lines changed

12 files changed

+58
-58
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- DONE: save current state of resources before terminating (or even periodically)
1414
- DONE: load last state of resources at startup
1515
- DONE: LINK operation to link a resource to another resource
16+
- DONE: replace all occurences of "interface{}" with "any"
1617

1718
- IMPORTANT/DIFFICULT: websocket timeout after some time of no or only invalid, or unauthorized request
1819
- IMPORTANT/EASY-MEDIUM: change STREAM behavior: allow multiple streams of same resource but with different REID
@@ -21,7 +22,6 @@
2122

2223
- UNIMPORTANT/MEDIUM-DIFFICULT: overhaul interface to heimdall (redis seems unergonomic and clunky), maybe use http endpoint in heimdall to check authentication and cache the result
2324
- UNIMPORTANT/MEDIUM: overhaul snapshotting (make it more robust, maybe change "gob" AND "msgpack" to just "msgpack" -> cannot differentiate between directory and resource content)
24-
- UNIMPORTANT/EASY: replace all occurences of "interface{}" with "any"
2525
- UNIMPORTANT/MEDIUM: export prometheus metrics
2626
- UNIMPORTANT/DIFFICULT: fine grained access control
2727

auth/jwt/authentication.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var (
1616
// --- JWT Authentication ---
1717

1818
// ValidateJWT parses and validates an HMAC signed JWT and returns its claims or an error if invalid
19-
func ValidateJWT(tokenStr string) (map[string]interface{}, error) {
20-
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
19+
func ValidateJWT(tokenStr string) (map[string]any, error) {
20+
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (any, error) {
2121
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
2222
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
2323
}

directory/directory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Directory interface {
3333
ForEach(f func(resource.Resource) (bool, error)) error
3434

3535
// Returns the directory structure as a nested map
36-
List(path []string) (map[string]interface{}, error)
36+
List(path []string) (map[string]any, error)
3737

3838
// Takes a snapshot of a directory
3939
Snapshot(path []string, writer io.Writer) error

directory/tree/tree.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func forEach(t tree, f func(resource.Resource) (bool, error)) (err error) {
253253

254254
// List lists the contents of a directory by returning a recursively nested map of subdirectories.
255255
// A resource is indicated by a nil value.
256-
func (d *directory) List(path []string) (map[string]interface{}, error) {
256+
func (d *directory) List(path []string) (map[string]any, error) {
257257
d.lock.RLock()
258258
defer d.lock.RUnlock()
259259

@@ -268,8 +268,8 @@ func (d *directory) List(path []string) (map[string]interface{}, error) {
268268
return m, nil
269269
}
270270

271-
func list(n *node, includeContent bool) (map[string]interface{}, error) {
272-
result := make(map[string]interface{})
271+
func list(n *node, includeContent bool) (map[string]any, error) {
272+
result := make(map[string]any)
273273
for k, v := range n.entries {
274274
switch x := v.(type) {
275275
case *leaf:
@@ -296,7 +296,7 @@ func list(n *node, includeContent bool) (map[string]interface{}, error) {
296296
// We therefore use MsgPack to marshal the resource contents (in order to keep full MsgPack compatibility)
297297
// and then gob (Go's binary encoding) to marshal the directory tree.
298298
// Restoring from a snapshot does the same thing in reverse: First decode with gob and then decode the resources with msgpack.
299-
// Note: shamaton/msgpack library decodes map[string]interface{} as map[interface{}]interface{} -> switched to vmihailenco/msgpack
299+
// Note: shamaton/msgpack library decodes map[string]any as map[any]any -> switched to vmihailenco/msgpack
300300

301301
// Snapshot takes a snapshot of a directory (including the resource contents), serializes and writes it to the io.Writer.
302302
func (d *directory) Snapshot(path []string, writer io.Writer) error {
@@ -306,7 +306,7 @@ func (d *directory) Snapshot(path []string, writer io.Writer) error {
306306
if err != nil {
307307
return err
308308
}
309-
gob.Register(map[string]interface{}{})
309+
gob.Register(map[string]any{})
310310
enc := gob.NewEncoder(writer)
311311
m, err := list(n, true)
312312
if err != nil {
@@ -322,8 +322,8 @@ func (d *directory) Restore(path []string, reader io.Reader) error {
322322
d.lock.Lock()
323323
defer d.lock.Unlock()
324324

325-
var m map[string]interface{}
326-
gob.Register(map[string]interface{}{})
325+
var m map[string]any
326+
gob.Register(map[string]any{})
327327
dec := gob.NewDecoder(reader)
328328
err := dec.Decode(&m)
329329
if err != nil {
@@ -332,10 +332,10 @@ func (d *directory) Restore(path []string, reader io.Reader) error {
332332
return restore(d, path, m)
333333
}
334334

335-
func restore(d *directory, path []string, m map[string]interface{}) error {
335+
func restore(d *directory, path []string, m map[string]any) error {
336336
for k, v := range m {
337337
switch x := v.(type) {
338-
case map[string]interface{}:
338+
case map[string]any:
339339
p := append(path, k)
340340
_, err := d.getDirectory(p, true)
341341
if err != nil {

handler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (handler *Handler) Close() {
4242
}
4343

4444
func (handler *Handler) Disconnect(client *types.Client) {
45-
client.ForEachStream(func(path []string, ch chan interface{}) {
45+
client.ForEachStream(func(path []string, ch chan any) {
4646
resource, err := handler.directory.GetResource(path)
4747
if err != nil {
4848
return

resource/broker/broker.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,33 @@ var (
3232
type broker struct {
3333
path []string // resource path
3434

35-
input chan inputMsg // input channel (only for PUT)
36-
control chan controlMsg // control channel (for everything else than PUT)
37-
streams map[chan interface{}]bool // keeps track of active subscriber streams (value indicates whether the channel is infinite->blocking-send or finite->non-blocking-send)
38-
links map[*broker]chan interface{} // keeps track of active links from other resources
35+
input chan inputMsg // input channel (only for PUT)
36+
control chan controlMsg // control channel (for everything else than PUT)
37+
streams map[chan any]bool // keeps track of active subscriber streams (value indicates whether the channel is infinite->blocking-send or finite->non-blocking-send)
38+
links map[*broker]chan any // keeps track of active links from other resources
3939

40-
value interface{} // latest input value
40+
value any // latest input value
4141
valueLock sync.RWMutex
4242
}
4343

4444
var _ resource.Resource = (*broker)(nil) // ensure resource implements Resource
4545

4646
// Message sent through input channel
4747
type inputMsg struct { // PUT
48-
Content interface{}
48+
Content any
4949
ResponseChan chan resource.Response
5050
}
5151

5252
// Message sent through control channel
5353
type controlMsg struct {
5454
Type controlMsgType // enum - see const
55-
Content interface{}
55+
Content any
5656
ResponseChan chan resource.Response
5757
}
5858

5959
// Content for STREAM controlMsg
6060
type streamContent struct {
61-
channel chan interface{}
61+
channel chan any
6262
infinite bool
6363
}
6464

@@ -72,8 +72,8 @@ func Create(path []string) resource.Resource {
7272
input: make(chan inputMsg, inputChanSize),
7373
control: make(chan controlMsg, controlChanSize),
7474

75-
streams: make(map[chan interface{}]bool),
76-
links: make(map[*broker]chan interface{}),
75+
streams: make(map[chan any]bool),
76+
links: make(map[*broker]chan any),
7777

7878
value: msgp.Raw{},
7979
valueLock: sync.RWMutex{},
@@ -99,7 +99,7 @@ func (r *broker) monitor() {
9999
}
100100
}
101101

102-
func nonBlockingSend(c chan interface{}, v interface{}) bool {
102+
func nonBlockingSend(c chan any, v any) bool {
103103
select {
104104
case c <- v:
105105
return true
@@ -157,7 +157,7 @@ func (r *broker) broker() {
157157
controlMsg.ResponseChan <- resource.Response{Code: 200, Err: nil}
158158

159159
case STOP:
160-
stream := controlMsg.Content.(chan interface{})
160+
stream := controlMsg.Content.(chan any)
161161
_, ok := r.streams[stream]
162162
if !ok {
163163
controlMsg.ResponseChan <- resource.Response{Code: 404, Err: errors.New("the stream does not exist and therefore cannot be closed")}
@@ -211,16 +211,16 @@ func (r *broker) Close() resource.Response {
211211

212212
// Stream subscribes to this resource.
213213
// This returns a new channel where all updates to this resource will be sent to.
214-
func (r *broker) Stream() (chan interface{}, resource.Response) {
215-
stream := make(chan interface{}, streamChanSize)
214+
func (r *broker) Stream() (chan any, resource.Response) {
215+
stream := make(chan any, streamChanSize)
216216
respChan := make(chan resource.Response)
217217
defer close(respChan)
218218
r.control <- controlMsg{Type: STREAM, Content: streamContent{stream, false}, ResponseChan: respChan}
219219
return stream, <-respChan
220220
}
221221

222222
// TODO: change interface sucht that this method can be used
223-
func (r *broker) StreamWithGuaranteedDelivery() (chan interface{}, resource.Response) {
223+
func (r *broker) StreamWithGuaranteedDelivery() (chan any, resource.Response) {
224224
streamIn, streamOut := makeInfinite()
225225
respChan := make(chan resource.Response)
226226
defer close(respChan)
@@ -230,23 +230,23 @@ func (r *broker) StreamWithGuaranteedDelivery() (chan interface{}, resource.Resp
230230

231231
// StopStream unsubscribes from this resource.
232232
// The channel created by Stream() needs to be passed
233-
func (r *broker) StopStream(stream chan interface{}) resource.Response {
233+
func (r *broker) StopStream(stream chan any) resource.Response {
234234
respChan := make(chan resource.Response)
235235
defer close(respChan)
236236
r.control <- controlMsg{Type: STOP, Content: stream, ResponseChan: respChan}
237237
return <-respChan
238238
}
239239

240240
// Put updates the value of this resource.
241-
func (r *broker) Put(payload interface{}) resource.Response {
241+
func (r *broker) Put(payload any) resource.Response {
242242
respChan := make(chan resource.Response)
243243
defer close(respChan)
244244
r.input <- inputMsg{Content: payload, ResponseChan: respChan}
245245
return <-respChan
246246
}
247247

248248
// Get returns the current (latest written) value of this resource
249-
func (r *broker) Get() (interface{}, resource.Response) {
249+
func (r *broker) Get() (any, resource.Response) {
250250
r.valueLock.RLock()
251251
defer r.valueLock.RUnlock()
252252
return r.value, resource.Response{Code: 200, Err: nil}
@@ -289,18 +289,18 @@ func (r *broker) isLinkedBy(other *broker) bool {
289289
}
290290

291291
// Makes an infinite channel by using a slice and a goroutine
292-
func makeInfinite() (in chan interface{}, out chan interface{}) {
293-
in = make(chan interface{}, streamChanSize)
294-
out = make(chan interface{}, streamChanSize)
292+
func makeInfinite() (in chan any, out chan any) {
293+
in = make(chan any, streamChanSize)
294+
out = make(chan any, streamChanSize)
295295
go func() {
296-
var inQ []interface{}
297-
outC := func() chan interface{} {
296+
var inQ []any
297+
outC := func() chan any {
298298
if len(inQ) == 0 {
299299
return nil
300300
}
301301
return out
302302
}
303-
curVal := func() interface{} {
303+
curVal := func() any {
304304
if len(inQ) == 0 {
305305
return nil
306306
}

resource/brokerless/brokerless.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ func (r *brokerless) Close() resource.Response {
6363
}
6464

6565
// Get implements resource.Resource.
66-
func (r *brokerless) Get() (interface{}, resource.Response) {
66+
func (r *brokerless) Get() (any, resource.Response) {
6767
r.valueLock.RLock()
6868
defer r.valueLock.RUnlock()
6969
return r.value, resource.Response{Code: 200, Err: nil}
7070
}
7171

7272
// Put implements resource.Resource.
73-
func (r *brokerless) Put(value interface{}) resource.Response {
73+
func (r *brokerless) Put(value any) resource.Response {
7474
r.valueLock.Lock()
7575
r.value = value
7676
r.valueLock.Unlock()
@@ -92,7 +92,7 @@ func (r *brokerless) Put(value interface{}) resource.Response {
9292
}
9393

9494
// Stream implements resource.Resource.
95-
func (r *brokerless) Stream() (chan interface{}, resource.Response) {
95+
func (r *brokerless) Stream() (chan any, resource.Response) {
9696
r.streamsLock.Lock()
9797
defer r.streamsLock.Unlock()
9898

@@ -102,7 +102,7 @@ func (r *brokerless) Stream() (chan interface{}, resource.Response) {
102102
}
103103

104104
// StopStream implements resource.Resource.
105-
func (r *brokerless) StopStream(stream chan interface{}) resource.Response {
105+
func (r *brokerless) StopStream(stream chan any) resource.Response {
106106
r.streamsLock.Lock()
107107
defer r.streamsLock.Unlock()
108108

resource/resource.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package resource
22

33
type Resource interface {
4-
Stream() (chan interface{}, Response)
5-
StopStream(chan interface{}) Response
6-
Put(interface{}) Response
7-
Get() (interface{}, Response)
4+
Stream() (chan any, Response)
5+
StopStream(chan any) Response
6+
Put(any) Response
7+
Get() (any, Response)
88
Link(Resource) Response
99
UnLink(Resource) Response
1010
Close() Response
1111
}
1212

13-
// type Channel chan interface{}
13+
// type Channel chan any
1414

1515
// type Channel interface {
16-
// Write(interface{})
17-
// Read() (interface{}, bool)
16+
// Write(any)
17+
// Read() (any, bool)
1818
// Close()
1919
// }
2020

resource/resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func TestLinkUnLinkStreamPut(t *testing.T) {
313313

314314
func TestStopStreamInvalid(t *testing.T) {
315315
testResource := resource.Create([]string{})
316-
stream := make(chan interface{})
316+
stream := make(chan any)
317317
resp := testResource.StopStream(stream)
318318
if resp.Err == nil {
319319
t.Fatalf("Expected error, got nil")

types/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Client struct {
99

1010
type stream struct {
1111
path []string
12-
channel chan interface{}
12+
channel chan any
1313
}
1414

1515
func NewClient(send func(*Response) error) *Client {
@@ -19,14 +19,14 @@ func NewClient(send func(*Response) error) *Client {
1919
}
2020
}
2121

22-
func (c *Client) AddStream(path []string, ch chan interface{}) {
22+
func (c *Client) AddStream(path []string, ch chan any) {
2323
c.streams = append(c.streams, stream{
2424
path: path,
2525
channel: ch,
2626
})
2727
}
2828

29-
func (c *Client) GetStream(path []string) chan interface{} {
29+
func (c *Client) GetStream(path []string) chan any {
3030
for _, s := range c.streams {
3131
if equals(s.path, path) {
3232
return s.channel
@@ -52,7 +52,7 @@ func (c *Client) RemoveStream(path []string) {
5252
c.streams = c.streams[:len(c.streams)-1]
5353
}
5454

55-
func (c *Client) ForEachStream(f func(path []string, ch chan interface{})) {
55+
func (c *Client) ForEachStream(f func(path []string, ch chan any)) {
5656
for _, s := range c.streams {
5757
f(s.path, s.channel)
5858
}

0 commit comments

Comments
 (0)