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

lightningnetwork / lnd / 15838907453

24 Jun 2025 01:26AM UTC coverage: 57.079% (-11.1%) from 68.172%
15838907453

Pull #9982

github

web-flow
Merge e42780be2 into 45c15646c
Pull Request #9982: lnwire+lnwallet: add LocalNonces field for splice nonce coordination w/ taproot channels

103 of 167 new or added lines in 5 files covered. (61.68%)

30191 existing lines in 463 files now uncovered.

96331 of 168768 relevant lines covered (57.08%)

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

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

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

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

68
                return true
1✔
69
        }
70

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

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

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

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

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