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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

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

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

31
// Delete removes an item from the map specified by the key.
32
func (m *SyncMap[K, V]) Delete(key K) {
98✔
33
        m.Map.Delete(key)
98✔
34
}
98✔
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) {
64✔
51
        m.Map.Range(func(k any, v any) bool {
136✔
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