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

azrod / kivigo / 17445027409

03 Sep 2025 08:16PM UTC coverage: 84.027%. First build
17445027409

Pull #62

github

web-flow
Merge 052c36313 into 52a88381f
Pull Request #62: feat: add haskey & has keys func

60 of 78 new or added lines in 11 files covered. (76.92%)

1594 of 1897 relevant lines covered (84.03%)

0.97 hits per line

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

90.0
/pkg/client/backend.go
1
package client
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7

8
        "github.com/azrod/kivigo/pkg/errs"
9
)
10

11
// HasKey checks if the specified key exists in the store.
12
// Returns an error if the key is empty.
13
func (c Client) HasKey(ctx context.Context, key string) (bool, error) {
1✔
14
        if key == "" {
2✔
15
                return false, errs.ErrEmptyKey
1✔
16
        }
1✔
17

18
        _, err := c.GetRaw(ctx, key)
1✔
19
        if err != nil {
2✔
20
                if errors.Is(err, errs.ErrNotFound) {
2✔
21
                        return false, nil
1✔
22
                }
1✔
NEW
23
                return false, fmt.Errorf("failed to check key existence: %w", err)
×
24
        }
25

26
        return true, nil
1✔
27
}
28

29
// HasKeys checks if the specified keys exists in the store.
30
// Returns an error if the keys is empty.
31
func (c Client) HasKeys(ctx context.Context, keys []string) (bool, error) {
1✔
32
        if len(keys) == 0 {
2✔
33
                return false, errs.ErrEmptyKey
1✔
34
        }
1✔
35

36
        for _, key := range keys {
2✔
37
                exists, err := c.HasKey(ctx, key)
1✔
38
                if err != nil {
1✔
NEW
39
                        return false, fmt.Errorf("failed to check key existence: %w", err)
×
NEW
40
                }
×
41
                if !exists {
2✔
42
                        return false, nil
1✔
43
                }
1✔
44
        }
45

46
        return true, nil
1✔
47
}
48

49
type MatchKeysFunc func(keys []string) (bool, error)
50

51
// MatchKeys lists all keys with the given prefix and applies the provided MatchKeysFunc to determine if they match a custom condition.
52
// Returns false and an error if the function is nil or listing keys fails.
53
//
54
// Example:
55
//
56
//        ok, err := client.MatchKeys(ctx, "prefix", func(keys []string) (bool, error) {
57
//            // Custom matching logic
58
//            return len(keys) > 0, nil
59
//        })
60
func (c Client) MatchKeys(ctx context.Context, prefix string, f MatchKeysFunc) (bool, error) {
1✔
61
        if f == nil {
2✔
62
                return false, errs.ErrEmptyFunc
1✔
63
        }
1✔
64

65
        // List all keys in the store
66
        keys, err := c.List(ctx, prefix)
1✔
67
        if err != nil {
1✔
NEW
68
                return false, fmt.Errorf("failed to list keys: %w", err)
×
NEW
69
        }
×
70

71
        // Check if any key matches the criteria
72
        return f(keys)
1✔
73
}
74

75
// Get retrieves the value stored under the specified key and decodes it into dest.
76
// Returns an error if the key does not exist or decoding fails.
77
//
78
// Example:
79
//
80
//        var value string
81
//        err := client.Get(ctx, "myKey", &value)
82
func (c Client) Get(ctx context.Context, key string, value any) error {
1✔
83
        if key == "" {
2✔
84
                return errs.ErrEmptyKey
1✔
85
        }
1✔
86

87
        vV, err := c.GetRaw(ctx, key)
1✔
88
        if err != nil {
2✔
89
                return err
1✔
90
        }
1✔
91

92
        return c.opts.Encoder.Decode(vV, value)
1✔
93
}
94

95
// Set stores the given value under the specified key.
96
// Returns an error if the operation fails.
97
//
98
// Example:
99
//
100
//        err := client.Set(ctx, "myKey", "myValue")
101
func (c Client) Set(ctx context.Context, key string, value any) error {
1✔
102
        if key == "" {
2✔
103
                return errs.ErrEmptyKey
1✔
104
        }
1✔
105

106
        vV, err := c.opts.Encoder.Encode(value)
1✔
107
        if err != nil {
2✔
108
                return err
1✔
109
        }
1✔
110

111
        err = c.SetRaw(ctx, key, vV)
1✔
112
        if err != nil {
1✔
113
                return err
×
114
        }
×
115

116
        // Trigger hooks after successful operation
117
        if c.hooks != nil {
2✔
118
                c.hooks.Run(ctx, EventSet, key, vV)
1✔
119
        }
1✔
120

121
        return nil
1✔
122
}
123

124
// Delete removes the value associated with the specified key.
125
// Returns an error if the operation fails.
126
//
127
// Example:
128
//
129
//        err := client.Delete(ctx, "myKey")
130
func (c Client) Delete(ctx context.Context, key string) error {
1✔
131
        if key == "" {
2✔
132
                return errs.ErrEmptyKey
1✔
133
        }
1✔
134

135
        err := c.KV.Delete(ctx, key)
1✔
136
        if err != nil {
2✔
137
                return err
1✔
138
        }
1✔
139

140
        // Trigger hooks after successful operation
141
        if c.hooks != nil {
2✔
142
                c.hooks.Run(ctx, EventDelete, key, nil)
1✔
143
        }
1✔
144

145
        return nil
1✔
146
}
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

© 2026 Coveralls, Inc