Skip to content

Commit

Permalink
fix logger zero value
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodallaqua committed Nov 17, 2023
1 parent ce2d9ae commit 5c64d30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
19 changes: 13 additions & 6 deletions demo/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -68,17 +69,23 @@ type newReq struct {

type newRes struct{}

func newEntrypoint(addr, path string, shouldLog bool, opts ...vectoria.Options) (*entrypoint, error) {
func newEntrypoint(logger *slog.Logger, addr, path string, shouldLogDB bool, opts ...vectoria.Options) (*entrypoint, error) {
if logger == nil {
return nil, errors.New("logger cannot be nil")
}

db, err := vectoria.New(path, opts...)
if err != nil {
logger.Error("unable to create database", "function", "newEntrypoint", "error", err)
return nil, err
}

return &entrypoint{
addr: addr,
path: path,
app: newApp(shouldLog),
db: db,
addr: addr,
path: path,
app: newApp(shouldLogDB),
db: db,
logger: logger,
}, nil
}

Expand Down Expand Up @@ -306,7 +313,7 @@ func main() {
)

logger.Info("launching vector database")
entry, err := newEntrypoint(addr, path, true,
entry, err := newEntrypoint(logger, addr, path, true,
vectoria.WithIndexLSH(&vectoria.LSHConfig{
IndexName: "demo",
NumRounds: 10,
Expand Down
13 changes: 10 additions & 3 deletions demo/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
_ "embed"
"io"
"log/slog"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -66,7 +67,9 @@ func TestRegisterRoutes(t *testing.T) {
},
}

entry, err := newEntrypoint(gofakeit.URL(), "", false)
logger := slog.New(slog.NewTextHandler(nil, nil))

entry, err := newEntrypoint(logger, gofakeit.URL(), "", false)
assert.NoError(t, err)

entry.registerRoutes()
Expand All @@ -83,7 +86,9 @@ func TestRegisterRoutes(t *testing.T) {
}

func TestAdd(t *testing.T) {
entry, err := newEntrypoint(gofakeit.URL(), "", false,
logger := slog.New(slog.NewTextHandler(nil, nil))

entry, err := newEntrypoint(logger, gofakeit.URL(), "", false,
vectoria.WithIndexLSH(&vectoria.LSHConfig{
IndexName: "demo",
NumRounds: 10,
Expand All @@ -108,7 +113,9 @@ func TestAdd(t *testing.T) {
}

func TestGet(t *testing.T) {
entry, err := newEntrypoint(gofakeit.URL(), "", false,
logger := slog.New(slog.NewTextHandler(nil, nil))

entry, err := newEntrypoint(logger, gofakeit.URL(), "", false,
vectoria.WithIndexLSH(&vectoria.LSHConfig{
IndexName: "demo",
NumRounds: 10,
Expand Down

0 comments on commit 5c64d30

Please sign in to comment.