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

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

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

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

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

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

68
                return true
4✔
69
        }
70

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

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

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

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

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