src to pkg, impl lobby.Closed
This commit is contained in:
19
pkg/lib/db/ctx.go
Normal file
19
pkg/lib/db/ctx.go
Normal 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
22
pkg/lib/db/ctx_test.go
Normal 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
21
pkg/lib/db/new.go
Normal 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
|
||||
}
|
||||
34
pkg/lib/event/serialize.go
Normal file
34
pkg/lib/event/serialize.go
Normal 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
23
pkg/lib/test.go
Normal 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
26
pkg/lib/test_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user