Skip to content

Commit

Permalink
Merge pull request #41 from EchoVault/chore/fix-minor-issues
Browse files Browse the repository at this point in the history
Chore/fix minor issues
  • Loading branch information
kelvinmwinuka authored May 18, 2024
2 parents aac4349 + c634b1b commit e8fe6b1
Show file tree
Hide file tree
Showing 21 changed files with 2,003 additions and 1,833 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ build:
env CGO_ENABLED=1 CC=x86_64-linux-musl-gcc GOOS=linux GOARCH=amd64 DEST=bin/linux/x86_64 make build-server

run:
make build && docker-compose up --build
make build && \
docker-compose up --build

test-unit:
env RACE=false OUT=internal/modules/admin/testdata make build-modules-test && \
Expand Down
3,237 changes: 1,627 additions & 1,610 deletions coverage/coverage.out

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions echovault/api_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"github.com/echovault/echovault/internal"
"github.com/tidwall/resp"
"strings"
)

// ACLLoadOptions modifies the behaviour of the ACLLoad function.
Expand Down Expand Up @@ -150,8 +151,8 @@ func (server *EchoVault) ACLUsers() ([]string, error) {
//
// `user` - User - The user object to add/update.
//
// Returns: "OK" if the user is successfully created/updated.
func (server *EchoVault) ACLSetUser(user User) (string, error) {
// Returns: true if the user is successfully created/updated.
func (server *EchoVault) ACLSetUser(user User) (bool, error) {
cmd := []string{"ACL", "SETUSER", user.Username}

if user.Enabled {
Expand Down Expand Up @@ -238,10 +239,11 @@ func (server *EchoVault) ACLSetUser(user User) (string, error) {

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err
}

return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err
}

// ACLGetUser gets the ACL configuration of the name with the given username.
Expand Down Expand Up @@ -323,14 +325,15 @@ func (server *EchoVault) ACLGetUser(username string) (map[string][]string, error
//
// `usernames` - ...string - A string of usernames to delete from the ACL module.
//
// Returns: "OK" if the deletion is successful.
func (server *EchoVault) ACLDelUser(usernames ...string) (string, error) {
// Returns: true if the deletion is successful.
func (server *EchoVault) ACLDelUser(usernames ...string) (bool, error) {
cmd := append([]string{"ACL", "DELUSER"}, usernames...)
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err
}
return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err
}

// ACLList lists all the currently loaded ACL users and their rules.
Expand All @@ -349,8 +352,8 @@ func (server *EchoVault) ACLList() ([]string, error) {
//
// `options` - ACLLoadOptions - modifies the load behaviour.
//
// Returns: "OK" if the load is successful.
func (server *EchoVault) ACLLoad(options ACLLoadOptions) (string, error) {
// Returns: true if the load is successful.
func (server *EchoVault) ACLLoad(options ACLLoadOptions) (bool, error) {
cmd := []string{"ACL", "LOAD"}
switch {
case options.Merge:
Expand All @@ -363,19 +366,21 @@ func (server *EchoVault) ACLLoad(options ACLLoadOptions) (string, error) {

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err
}

return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err
}

// ACLSave saves the current ACL configuration to the configured ACL file.
//
// Returns: "OK" if the save is successful.
func (server *EchoVault) ACLSave() (string, error) {
// Returns: true if the save is successful.
func (server *EchoVault) ACLSave() (bool, error) {
b, err := server.handleCommand(server.context, internal.EncodeCommand([]string{"ACL", "SAVE"}), nil, false, true)
if err != nil {
return "", err
return false, err
}
return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
return strings.EqualFold(s, "ok"), err
}
2 changes: 1 addition & 1 deletion echovault/api_admin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2024 Kelvin Clement Mwinuka
//
// Licensed under the Apache License, Version 2.0 (the "License");s
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
Expand Down
44 changes: 29 additions & 15 deletions echovault/api_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package echovault
import (
"github.com/echovault/echovault/internal"
"strconv"
"strings"
)

// SetOptions modifies the behaviour for the Set command
Expand Down Expand Up @@ -77,14 +78,14 @@ type PExpireAtOptions ExpireOptions
//
// `options` - SetOptions.
//
// Returns: "OK" if the set is successful, If the "Get" flag in SetOptions is set to true, the previous value is returned.
// Returns: true if the set is successful, If the "Get" flag in SetOptions is set to true, the previous value is returned.
//
// Errors:
//
// "key <key> does not exist"" - when the XX flag is set to true and the key does not exist.
//
// "key <key> does already exists" - when the NX flag is set to true and the key already exists.
func (server *EchoVault) Set(key, value string, options SetOptions) (string, error) {
func (server *EchoVault) Set(key, value string, options SetOptions) (string, bool, error) {
cmd := []string{"SET", key, value}

switch {
Expand All @@ -111,10 +112,18 @@ func (server *EchoVault) Set(key, value string, options SetOptions) (string, err

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return "", false, err
}

return internal.ParseStringResponse(b)
previousValue, err := internal.ParseStringResponse(b)
if err != nil {
return "", false, err
}
if !options.GET {
previousValue = ""
}

return previousValue, true, nil
}

// MSet set multiple values at multiple keys with one command. Existing keys are overwritten and non-existent
Expand All @@ -124,12 +133,12 @@ func (server *EchoVault) Set(key, value string, options SetOptions) (string, err
//
// `kvPairs` - map[string]string - a map representing all the keys and values to be set.
//
// Returns: "OK" if the set is successful.
// Returns: true if the set is successful.
//
// Errors:
//
// "key <key> does already exists" - when the NX flag is set to true and the key already exists.
func (server *EchoVault) MSet(kvPairs map[string]string) (string, error) {
// "key <key> already exists" - when the NX flag is set to true and the key already exists.
func (server *EchoVault) MSet(kvPairs map[string]string) (bool, error) {
cmd := []string{"MSET"}

for k, v := range kvPairs {
Expand All @@ -138,10 +147,15 @@ func (server *EchoVault) MSet(kvPairs map[string]string) (string, error) {

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return "", err
return false, err
}

return internal.ParseStringResponse(b)
s, err := internal.ParseStringResponse(b)
if err != nil {
return false, err
}

return strings.EqualFold(s, "ok"), nil
}

// Get retrieves the value at the provided key.
Expand Down Expand Up @@ -279,7 +293,7 @@ func (server *EchoVault) PTTL(key string) (int, error) {
// `options` - ExpireOptions
//
// Returns: true if the key's expiry was successfully updated.
func (server *EchoVault) Expire(key string, seconds int, options ExpireOptions) (int, error) {
func (server *EchoVault) Expire(key string, seconds int, options ExpireOptions) (bool, error) {
cmd := []string{"EXPIRE", key, strconv.Itoa(seconds)}

switch {
Expand All @@ -295,10 +309,10 @@ func (server *EchoVault) Expire(key string, seconds int, options ExpireOptions)

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return 0, err
return false, err
}

return internal.ParseIntegerResponse(b)
return internal.ParseBooleanResponse(b)
}

// PExpire set the given key's expiry in milliseconds from now.
Expand All @@ -313,7 +327,7 @@ func (server *EchoVault) Expire(key string, seconds int, options ExpireOptions)
// `options` - PExpireOptions
//
// Returns: true if the key's expiry was successfully updated.
func (server *EchoVault) PExpire(key string, milliseconds int, options PExpireOptions) (int, error) {
func (server *EchoVault) PExpire(key string, milliseconds int, options PExpireOptions) (bool, error) {
cmd := []string{"PEXPIRE", key, strconv.Itoa(milliseconds)}

switch {
Expand All @@ -329,10 +343,10 @@ func (server *EchoVault) PExpire(key string, milliseconds int, options PExpireOp

b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return 0, err
return false, err
}

return internal.ParseIntegerResponse(b)
return internal.ParseBooleanResponse(b)
}

// ExpireAt set the given key's expiry in unix epoch seconds.
Expand Down
Loading

0 comments on commit e8fe6b1

Please sign in to comment.