From 510f173427f892528ac8955aef761d7f6494bd5b Mon Sep 17 00:00:00 2001 From: Brahim Berkasse Date: Thu, 18 Jan 2024 20:28:12 +0100 Subject: [PATCH] fix: golint warning --- Makefile | 3 +++ src/aof/aof.go | 4 ++++ src/handler/handler.go | 4 ++++ src/handler/hash.go | 3 +++ src/main.go | 14 +++++++------- src/resp/resp.go | 8 ++++++-- src/resp/writer.go | 3 +++ 7 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 0534004..1bac4ca 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ SRC=src/main.go all: clean go build -o $(NAME) $(SRC) +scheck: + staticcheck ./... + clean: rm -Rf $(NAME) diff --git a/src/aof/aof.go b/src/aof/aof.go index 1409b86..763d2b6 100644 --- a/src/aof/aof.go +++ b/src/aof/aof.go @@ -10,12 +10,15 @@ import ( "github.com/brkss/redis/src/resp" ) +// AOF is struct that hold file / reader and mutex related to data sync type AOF struct { file *os.File rd *bufio.Reader mu sync.Mutex } +// NewAOF create a new AOF object create or open file provided in params +// alse lunch a parallel thread to keep the file synced func NewAOF(path string) (*AOF, error) { f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666) if err != nil { @@ -40,6 +43,7 @@ func NewAOF(path string) (*AOF, error) { return aof, nil } +// Close closes the file after the problem exited func (aof *AOF) Close() error { aof.mu.Lock() defer aof.mu.Unlock() diff --git a/src/handler/handler.go b/src/handler/handler.go index 3c83aa4..6f887a6 100644 --- a/src/handler/handler.go +++ b/src/handler/handler.go @@ -6,9 +6,13 @@ import ( "github.com/brkss/redis/src/resp" ) +// SETs is a global variable to hold database data var SETs = map[string]string{} + +// SETsMutex is a global variable that hold mutexed to handle editing SETs's data var SETsMutex = sync.RWMutex{} +// Handlers is global variable that hold function for each command ! var Handlers = map[string]func([]resp.Value) resp.Value{ "PING": ping, "GET": get, diff --git a/src/handler/hash.go b/src/handler/hash.go index d37b26e..98780d2 100644 --- a/src/handler/hash.go +++ b/src/handler/hash.go @@ -6,7 +6,10 @@ import ( "github.com/brkss/redis/src/resp" ) +// HSETs is a global variable to hold database hashed data var HSETs = map[string]map[string]string{} + +// HSETsMutext is a global variable that hold mutexed to handle editing HSETs's data var HSETsMutext = sync.RWMutex{} func hset(args []resp.Value) resp.Value { diff --git a/src/main.go b/src/main.go index b229ce9..cf1541f 100644 --- a/src/main.go +++ b/src/main.go @@ -11,18 +11,19 @@ import ( "github.com/brkss/redis/src/resp" ) +// Port is a global const representing the server port const ( - PORT = ":6379" - BUFFER_SIZE = 1024 - AOF_FILE = "./database.aof" + Port = ":6379" + BufferSize = 1024 + AOFFile = "./database.aof" ) func main() { - fmt.Println("Start listening for connection on 127.0.0.1" + PORT) + fmt.Println("Start listening for connection on 127.0.0.1" + Port) // create new server ! - l, err := net.Listen("tcp", PORT) + l, err := net.Listen("tcp", Port) if err != nil { log.Fatal("Can't run redos server :", err) } @@ -34,7 +35,7 @@ func main() { } // init aof - aof, err := aof.NewAOF(AOF_FILE) + aof, err := aof.NewAOF(AOFFile) if err != nil { log.Fatal("Something went wrong opening aof : ", err) } @@ -50,7 +51,6 @@ func main() { return } handler(args) - return }) fmt.Println("read error : ", err) diff --git a/src/resp/resp.go b/src/resp/resp.go index a9e5807..610fd75 100644 --- a/src/resp/resp.go +++ b/src/resp/resp.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// STRING, ... holds pre defined const ( STRING = '+' ERROR = '-' @@ -15,6 +16,7 @@ const ( ARRAY = '*' ) +// Value hold request / response value later will be reformed to RESP type Value struct { Typ string Str string @@ -23,10 +25,12 @@ type Value struct { Arr []Value } +// Resp holds pointer to bufio reader type Resp struct { reader *bufio.Reader } +// NewReader create a pointer to Resp func NewReader(rd io.Reader) *Resp { return &Resp{bufio.NewReader(rd)} } @@ -38,7 +42,7 @@ func (r *Resp) readLine() (line []byte, n int, err error) { if err != nil { return nil, 0, err } - n += 1 + n++ line = append(line, b) if len(line) >= 2 && line[len(line)-2] == '\r' { break @@ -73,7 +77,7 @@ func (r *Resp) Read() (Value, error) { case BULK: return r.readBulk() default: - err := fmt.Errorf("Unkown type %v\n", string(_type)) + err := fmt.Errorf("unkown type %v", string(_type)) return Value{}, err } } diff --git a/src/resp/writer.go b/src/resp/writer.go index 66ad305..66274f6 100644 --- a/src/resp/writer.go +++ b/src/resp/writer.go @@ -5,10 +5,12 @@ import ( "strconv" ) +// Writer struct that holds writer value will be used to write resp response to the connection type Writer struct { writer io.Writer } +// NewWriter create pointer to Writer struct func NewWriter(w io.Writer) *Writer { return &Writer{writer: w} } @@ -24,6 +26,7 @@ func (w *Writer) Write(v Value) error { return nil } +// Marshal convert from strut Value to Resp in bytes func (v Value) Marshal() []byte { switch v.Typ { case "array":