• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/lnutils/sync_map.go
1
package lnutils
2

3
import "sync"
4

5
// SyncMap wraps a sync.Map with type parameters such that it's easier to
6
// access the items stored in the map since no type assertion is needed. It
7
// also requires explicit type definition when declaring and initiating the
8
// variables, which helps us understanding what's stored in a given map.
9
type SyncMap[K comparable, V any] struct {
10
        sync.Map
11
}
12

13
// Store puts an item in the map.
14
func (m *SyncMap[K, V]) Store(key K, value V) {
2✔
15
        m.Map.Store(key, value)
2✔
16
}
2✔
17

18
// Load queries an item from the map using the specified key. If the item
19
// cannot be found, an empty value and false will be returned. If the stored
20
// item fails the type assertion, a nil value and false will be returned.
21
func (m *SyncMap[K, V]) Load(key K) (V, bool) {
2✔
22
        result, ok := m.Map.Load(key)
2✔
23
        if !ok {
4✔
24
                return *new(V), false // nolint: gocritic
2✔
25
        }
2✔
26

27
        item, ok := result.(V)
2✔
28
        return item, ok
2✔
29
}
30

31
// Delete removes an item from the map specified by the key.
32
func (m *SyncMap[K, V]) Delete(key K) {
2✔
33
        m.Map.Delete(key)
2✔
34
}
2✔
35

36
// LoadAndDelete queries an item and deletes it from the map using the
37
// specified key.
38
func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool) {
2✔
39
        result, loaded := m.Map.LoadAndDelete(key)
2✔
40
        if !loaded {
2✔
UNCOV
41
                return *new(V), loaded // nolint: gocritic
×
UNCOV
42
        }
×
43

44
        item, ok := result.(V)
2✔
45
        return item, ok
2✔
46
}
47

48
// Range iterates the map and applies the `visitor` function. If the `visitor`
49
// returns false, the iteration will be stopped.
50
func (m *SyncMap[K, V]) Range(visitor func(K, V) bool) {
2✔
51
        m.Map.Range(func(k any, v any) bool {
4✔
52
                return visitor(k.(K), v.(V))
2✔
53
        })
2✔
54
}
55

56
// ForEach iterates the map and applies the `visitor` function. Unlike the
57
// `Range` method, the `visitor` function will be applied to all the items
58
// unless there's an error.
59
func (m *SyncMap[K, V]) ForEach(visitor func(K, V) error) {
2✔
60
        // rangeVisitor wraps the `visitor` function and returns false if
2✔
61
        // there's an error returned from the `visitor` function.
2✔
62
        rangeVisitor := func(k K, v V) bool {
4✔
63
                if err := visitor(k, v); err != nil {
2✔
UNCOV
64
                        // Break the iteration if there's an error.
×
UNCOV
65
                        return false
×
UNCOV
66
                }
×
67

68
                return true
2✔
69
        }
70

71
        m.Range(rangeVisitor)
2✔
72
}
73

74
// Len returns the number of items in the map.
75
func (m *SyncMap[K, V]) Len() int {
2✔
76
        var count int
2✔
77
        m.Range(func(_ K, _ V) bool {
4✔
78
                count++
2✔
79

2✔
80
                return true
2✔
81
        })
2✔
82

83
        return count
2✔
84
}
85

86
// LoadOrStore queries an item from the map using the specified key. If the
87
// item cannot be found, the `value` will be stored in the map and returned.
88
// If the stored item fails the type assertion, a nil value and false will be
89
// returned.
90
func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) {
2✔
91
        result, loaded := m.Map.LoadOrStore(key, value)
2✔
92
        item, ok := result.(V)
2✔
93
        if !ok {
2✔
94
                return *new(V), false
×
95
        }
×
96

97
        return item, loaded
2✔
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc