src to pkg, impl lobby.Closed

This commit is contained in:
Bel LaPointe
2025-02-12 16:48:58 -07:00
parent 1f6b79aa3b
commit e35ddef4b7
18 changed files with 117 additions and 21 deletions

19
pkg/lib/db/ctx.go Normal file
View File

@@ -0,0 +1,19 @@
package db
import (
"context"
"database/sql"
)
func Inject(ctx context.Context, db *sql.DB) context.Context {
return context.WithValue(ctx, "__db__", db)
}
func From(ctx context.Context) *sql.DB {
return Extract(ctx)
}
func Extract(ctx context.Context) *sql.DB {
v, _ := ctx.Value("__db__").(*sql.DB)
return v
}

22
pkg/lib/db/ctx_test.go Normal file
View File

@@ -0,0 +1,22 @@
package db_test
import (
"context"
"database/sql"
"gitea/price-is-wrong/pkg/lib/db"
"testing"
)
func TestInjectDB(t *testing.T) {
ctx := context.Background()
d := &sql.DB{}
injected := db.Inject(ctx, d)
extracted := db.Extract(injected)
if d != extracted {
t.Fatal("couldnt extract injected db")
} else if extracted != db.From(injected) {
t.Fatal("couldnt from extracted db")
}
}

21
pkg/lib/db/new.go Normal file
View File

@@ -0,0 +1,21 @@
package db
import (
"context"
"database/sql"
_ "github.com/glebarez/sqlite"
)
func New(ctx context.Context, driver, conn string) (*sql.DB, error) {
db, err := sql.Open(driver, conn)
if err != nil {
return nil, err
}
if err := db.PingContext(ctx); err != nil {
return nil, err
}
return db, nil
}

View File

@@ -0,0 +1,34 @@
package event
import (
"encoding/json"
"fmt"
)
func Serialize(t string, v any) ([]byte, error) {
var wrapper struct {
Type string `json:"type"`
Payload any `json:"payload"`
}
wrapper.Type = t
wrapper.Payload = v
return json.Marshal(wrapper)
}
func Parse(b []byte, typesToPointers map[string]any) (string, error) {
var wrapper struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}
if err := json.Unmarshal(b, &wrapper); err != nil {
return "", err
}
if ptr, ok := typesToPointers[wrapper.Type]; !ok {
return "", fmt.Errorf("cannot parse unknown type %s", wrapper.Type)
} else if err := json.Unmarshal(wrapper.Payload, ptr); err != nil {
return "", fmt.Errorf("failed parsing type %s: %w", wrapper.Type, err)
}
return wrapper.Type, nil
}

23
pkg/lib/test.go Normal file
View File

@@ -0,0 +1,23 @@
package lib
import (
"context"
"path"
"testing"
"gitea/price-is-wrong/pkg/lib/db"
_ "github.com/glebarez/sqlite"
)
func NewTestCtx(t *testing.T) context.Context {
d := t.TempDir()
p := path.Join(d, "db.db")
t.Logf("test db at %s", p)
b, err := db.New(context.Background(), "sqlite", p)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { b.Close() })
return db.Inject(context.Background(), b)
}

26
pkg/lib/test_test.go Normal file
View File

@@ -0,0 +1,26 @@
package lib_test
import (
"context"
"gitea/price-is-wrong/pkg/lib"
"gitea/price-is-wrong/pkg/lib/db"
"sync"
"testing"
)
func TestTestCtx(t *testing.T) {
var ctx context.Context
wg := &sync.WaitGroup{}
wg.Add(1)
t.Run("subtest", func(t *testing.T) {
defer wg.Done()
ctx = lib.NewTestCtx(t)
if _, err := db.Extract(ctx).Exec(`SELECT 1`); err != nil {
t.Fatal(err)
}
})
wg.Wait()
if _, err := db.Extract(ctx).Exec(`SELECT 1`); err == nil {
t.Fatal(err)
}
}