-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(redlock): apply interface #2
Conversation
@vvanglro This one also |
Can you give me a short example, I don't understand how the abstracted |
I think I understand, I went to check the package main
import (
"context"
"fmt"
"github.com/vvanglro/gredlock/redlock"
)
type mockRedLock struct {}
func (rl *mockRedLock) SetLock(ctx context.Context, key string, value string, ttl int) (float64, error) {
return 1.1, nil
}
func (rl *mockRedLock) UnSetLock(ctx context.Context, key string, value string) (float64, error) {
return 1.2, nil
}
func (rl *mockRedLock) GetLockTtl(ctx context.Context, key string, value string) (int64, error) {
return 2, nil
}
func (rl *mockRedLock) IsLocked(ctx context.Context, key string) bool {
return true
}
func main() {
ctx := context.Background()
key := "my-key"
value := "my-value"
ttl := 60
var client redlock.RedLocker
client = &mockRedLock{}
resp, _ := client.SetLock(ctx, key, value, ttl)
fmt.Println(resp)
resp2, _ := client.GetLockTtl(ctx, key, value)
fmt.Println(resp2)
resp3 := client.IsLocked(ctx, key)
fmt.Println(resp3)
resp4, _ := client.UnSetLock(ctx, key, value)
fmt.Println(resp4)
} |
@vvanglro Yeah you basically got it Users can now reference Here is a real example:
|
I'm thinking about a problem. Can the return type of the method remain as is? In this way, when I use it, I can directly click on the corresponding method and jump directly to the source code instead of an abstract interface. What do you think? func NewRedisLock(ctx context.Context, options ...*red.Options) *RedLock {} |
@vvanglro It's obviously up to you 😄 but I would recommend keeping the abstraction as what your users are provided back Most IDEs have a "Go to Implementation" Feature. I'm not sure what you are using but VSCode has this. If you right click on the method you should see it |
Okay, I see. I'll keep it now. Thank you again.🙇♂️ I use goland. |
Context
Users of this library may want to mock
RedLock
when creating unit tests in their own application logic. I contemplated just creating the interface in my own project but I think it would be more useful for the community if the library just ships one itself. This should also provide a little more flexibily in the future as the underlyingRedLock
struct is abstracted away from users and you can switch out the implementation if ever needed.Changes
RedLocker
and swap out return forNewRedisLock
TestNewRedisLock
to verify underlying instance