define core types in lib, use in storage
This commit is contained in:
parent
662868cae1
commit
a7771672be
4 changed files with 43 additions and 26 deletions
19
scopes.go
19
scopes.go
|
@ -8,6 +8,18 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// elementary types
|
||||||
|
|
||||||
|
type Ident uint
|
||||||
|
|
||||||
|
type Data interface{}
|
||||||
|
|
||||||
|
type Map map[string]Data
|
||||||
|
type StrMap map[string]string
|
||||||
|
|
||||||
|
type Slice []Data
|
||||||
|
type StrSlice []string
|
||||||
|
|
||||||
// services - context
|
// services - context
|
||||||
|
|
||||||
type Services map[string]Context
|
type Services map[string]Context
|
||||||
|
@ -30,12 +42,7 @@ type Context interface {
|
||||||
Send(Message)
|
Send(Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// data (payload), message, address
|
// payload, message, address types
|
||||||
|
|
||||||
type Data interface{}
|
|
||||||
|
|
||||||
type Map map[string]Data
|
|
||||||
type Slice []Data
|
|
||||||
|
|
||||||
type Payload interface {
|
type Payload interface {
|
||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
lib "git.sr.ht/~cco/go-scopes"
|
||||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ func Template(name, src string) *template.Template {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildSql(t *template.Template, data map[string]any) string {
|
func BuildSql(t *template.Template, data lib.Map) string {
|
||||||
var out strings.Builder
|
var out strings.Builder
|
||||||
err := t.Execute(&out, data)
|
err := t.Execute(&out, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -11,18 +11,36 @@ import (
|
||||||
sql "git.sr.ht/~cco/go-scopes/storage"
|
sql "git.sr.ht/~cco/go-scopes/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Track interface {
|
||||||
|
TrackId() lib.Ident
|
||||||
|
}
|
||||||
|
|
||||||
|
type BaseTrack = track
|
||||||
|
type BaseContainer = container
|
||||||
|
|
||||||
|
// basic track implementation
|
||||||
|
|
||||||
type track struct {
|
type track struct {
|
||||||
trackId uint
|
trackId lib.Ident
|
||||||
head map[string]string
|
head lib.StrMap
|
||||||
timeStamp *time.Time
|
timeStamp *time.Time
|
||||||
data lib.Data
|
data lib.Map
|
||||||
container *container
|
container *container
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tr *track) TrackId() uint {
|
func SimpleTrack(taskId, userName string, data lib.Map) track {
|
||||||
|
return track{
|
||||||
|
head: lib.StrMap{"taskId": taskId, "userName": userName},
|
||||||
|
data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tr *track) TrackId() lib.Ident {
|
||||||
return tr.trackId
|
return tr.trackId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// basic container implementation
|
||||||
|
|
||||||
type container struct {
|
type container struct {
|
||||||
tableName string
|
tableName string
|
||||||
headFields []string
|
headFields []string
|
||||||
|
@ -30,13 +48,6 @@ type container struct {
|
||||||
storage *sql.Storage
|
storage *sql.Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
func SimpleTrack(taskId, userName string, data lib.Map) track {
|
|
||||||
return track{
|
|
||||||
head: map[string]string{"taskId": taskId, "userName": userName},
|
|
||||||
data: data,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SimpleContainer(db *sql.Storage) *container {
|
func SimpleContainer(db *sql.Storage) *container {
|
||||||
return &container{
|
return &container{
|
||||||
tableName: "tracks",
|
tableName: "tracks",
|
||||||
|
@ -46,7 +57,7 @@ func SimpleContainer(db *sql.Storage) *container {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cont *container) Insert(tr *track) uint {
|
func (cont *container) Insert(tr *track) lib.Ident {
|
||||||
var columns []string
|
var columns []string
|
||||||
var values []any
|
var values []any
|
||||||
for _, k := range cont.headFields {
|
for _, k := range cont.headFields {
|
||||||
|
@ -57,14 +68,14 @@ func (cont *container) Insert(tr *track) uint {
|
||||||
b, _ := json.Marshal(tr.data)
|
b, _ := json.Marshal(tr.data)
|
||||||
values = append(values, b)
|
values = append(values, b)
|
||||||
db := cont.storage
|
db := cont.storage
|
||||||
data := map[string]interface{}{
|
data := lib.Map{
|
||||||
"schema": db.Schema,
|
"schema": db.Schema,
|
||||||
"tablename": cont.tableName,
|
"tablename": cont.tableName,
|
||||||
"columns": columns,
|
"columns": columns,
|
||||||
}
|
}
|
||||||
sql := storage.BuildSql(SqlInsert, data)
|
sql := storage.BuildSql(SqlInsert, data)
|
||||||
if res := storage.QueryCol[uint](db, sql, values...); res != nil {
|
if res := storage.QueryCol[uint](db, sql, values...); res != nil {
|
||||||
trid := res[0]
|
trid := lib.Ident(res[0])
|
||||||
tr.trackId = trid
|
tr.trackId = trid
|
||||||
tr.container = cont
|
tr.container = cont
|
||||||
return trid
|
return trid
|
||||||
|
@ -74,7 +85,7 @@ func (cont *container) Insert(tr *track) uint {
|
||||||
|
|
||||||
func (cont *container) CreateTable() *container {
|
func (cont *container) CreateTable() *container {
|
||||||
db := cont.storage
|
db := cont.storage
|
||||||
data := map[string]interface{}{
|
data := lib.Map{
|
||||||
"schema": db.Schema,
|
"schema": db.Schema,
|
||||||
"tablename": cont.tableName,
|
"tablename": cont.tableName,
|
||||||
"headFields": cont.headFields,
|
"headFields": cont.headFields,
|
||||||
|
@ -82,9 +93,7 @@ func (cont *container) CreateTable() *container {
|
||||||
"params": db.Params,
|
"params": db.Params,
|
||||||
}
|
}
|
||||||
sql := storage.BuildSql(SqlCreate, data)
|
sql := storage.BuildSql(SqlCreate, data)
|
||||||
//println(sql)
|
if _, err := db.Exec(sql); err == nil {
|
||||||
_, err := db.Exec(sql)
|
|
||||||
if err == nil {
|
|
||||||
return cont
|
return cont
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -70,7 +70,7 @@ func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||||
cont.CreateTable()
|
cont.CreateTable()
|
||||||
track := tracking.SimpleTrack("t01", "john", lib.Map{})
|
track := tracking.SimpleTrack("t01", "john", lib.Map{})
|
||||||
cont.Insert(&track)
|
cont.Insert(&track)
|
||||||
t.AssertEqual(track.TrackId(), uint(1))
|
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue