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

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 hits per line

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

95.92
/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) {
413✔
15
        m.Map.Store(key, value)
413✔
16
}
413✔
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) {
391✔
22
        result, ok := m.Map.Load(key)
391✔
23
        if !ok {
514✔
24
                return *new(V), false // nolint: gocritic
123✔
25
        }
123✔
26

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

31
// Delete removes an item from the map specified by the key.
32
func (m *SyncMap[K, V]) Delete(key K) {
102✔
33
        m.Map.Delete(key)
102✔
34
}
102✔
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) {
6✔
39
        result, loaded := m.Map.LoadAndDelete(key)
6✔
40
        if !loaded {
7✔
41
                return *new(V), loaded // nolint: gocritic
1✔
42
        }
1✔
43

44
        item, ok := result.(V)
5✔
45
        return item, ok
5✔
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) {
3,444✔
51
        m.Map.Range(func(k any, v any) bool {
3,516✔
52
                return visitor(k.(K), v.(V))
72✔
53
        })
72✔
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) {
16✔
60
        // rangeVisitor wraps the `visitor` function and returns false if
16✔
61
        // there's an error returned from the `visitor` function.
16✔
62
        rangeVisitor := func(k K, v V) bool {
34✔
63
                if err := visitor(k, v); err != nil {
19✔
64
                        // Break the iteration if there's an error.
1✔
65
                        return false
1✔
66
                }
1✔
67

68
                return true
17✔
69
        }
70

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

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

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

83
        return count
38✔
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) {
50✔
91
        result, loaded := m.Map.LoadOrStore(key, value)
50✔
92
        item, ok := result.(V)
50✔
93
        if !ok {
50✔
94
                return *new(V), false
×
95
        }
×
96

97
        return item, loaded
50✔
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