rename lib to core; move lib code (mainly interfaces) to top-level scopes package
This commit is contained in:
parent
d7e96f4f3d
commit
b6325392a8
20 changed files with 169 additions and 170 deletions
|
@ -5,10 +5,10 @@ import (
|
|||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/context"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
"git.sr.ht/~cco/go-scopes/core/context"
|
||||
"git.sr.ht/~cco/go-scopes/core/message"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@ import (
|
|||
"net/url"
|
||||
"strings"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
|
|
@ -10,10 +10,10 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/app"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/context"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
"git.sr.ht/~cco/go-scopes/core/context"
|
||||
"git.sr.ht/~cco/go-scopes/core/message"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
)
|
||||
|
||||
type Cfg struct {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/core/message"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
|
@ -3,7 +3,7 @@ package context
|
|||
import (
|
||||
"sync"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
)
|
||||
|
||||
type Context = lib.Context
|
|
@ -1,9 +1,9 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/action"
|
||||
"git.sr.ht/~cco/go-scopes/core/action"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
|
@ -4,7 +4,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
)
|
||||
|
||||
// Address implementation
|
|
@ -4,7 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
)
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
package etc
|
||||
|
||||
import (
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/app"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
||||
|
|
137
lib/lib.go
137
lib/lib.go
|
@ -1,137 +0,0 @@
|
|||
// Package `lib` provides a set of common types (mostly interfaces
|
||||
// and function declarations) and some basic functions.
|
||||
package lib
|
||||
|
||||
import (
|
||||
stdlib_context "context"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// config
|
||||
|
||||
type ActionSpec interface {
|
||||
Handler() ActionHandler
|
||||
Receivers() []string
|
||||
}
|
||||
|
||||
type Pattern interface {
|
||||
fmt.Stringer
|
||||
Slice() []string
|
||||
}
|
||||
|
||||
type ActionConfig interface {
|
||||
Pattern() Pattern
|
||||
Specs() []ActionSpec
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
Name() string
|
||||
Starter() StartProc
|
||||
Step() StepProc
|
||||
MessageHandler() MessageHandler
|
||||
DoneHandler() StepProc
|
||||
Actions() []ActionConfig
|
||||
AddAction(string, ...ActionSpec) Config
|
||||
Children() []Config
|
||||
Add(...Config) Config
|
||||
}
|
||||
|
||||
// services - context
|
||||
|
||||
type Services map[string]Context
|
||||
|
||||
type ContextState interface{}
|
||||
|
||||
type Context interface {
|
||||
stdlib_context.Context
|
||||
Config() Config
|
||||
Parent() Context
|
||||
Services() Services
|
||||
ChildContext(Config) Context
|
||||
State() ContextState
|
||||
WithState(ContextState) Context
|
||||
Mailbox() chan Message
|
||||
WaitGroup() *sync.WaitGroup
|
||||
Stop()
|
||||
}
|
||||
|
||||
// message, address
|
||||
|
||||
type MsgHead interface {
|
||||
fmt.Stringer
|
||||
Slice() []string
|
||||
Domain() string
|
||||
Action() string
|
||||
Class() string
|
||||
Item() string
|
||||
}
|
||||
|
||||
type Message interface {
|
||||
MsgHead
|
||||
Head() MsgHead
|
||||
Sender() Address
|
||||
Payload() Payload
|
||||
WithSender(Address) Message
|
||||
WithPayload(Payload) Message
|
||||
}
|
||||
|
||||
type Address interface {
|
||||
fmt.Stringer
|
||||
Url() string
|
||||
Service() string
|
||||
Interaction() (string, string)
|
||||
SetUrl(string)
|
||||
SetInteraction(string, string)
|
||||
}
|
||||
|
||||
type Payload interface {
|
||||
fmt.Stringer
|
||||
Data() interface{}
|
||||
FromJson(string) Payload
|
||||
}
|
||||
|
||||
// action
|
||||
|
||||
type Action interface {
|
||||
Context() Context
|
||||
Spec() ActionSpec
|
||||
Message() Message
|
||||
Handle() bool
|
||||
}
|
||||
|
||||
// procedures and handlers
|
||||
|
||||
type Proc = func(Context)
|
||||
type StartProc = Proc
|
||||
type StepProc = func(Context) bool
|
||||
type MessageHandler = func(Context, Message) bool
|
||||
type ActionHandler = func(Action) bool
|
||||
|
||||
// library functions
|
||||
|
||||
func RunCtx(ctx Context, fct Proc) {
|
||||
ctx.WaitGroup().Add(1)
|
||||
go func() {
|
||||
defer ctx.WaitGroup().Done()
|
||||
fct(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
func HandleMsg(ctx Context, msg Message) bool {
|
||||
return ctx.Config().MessageHandler()(ctx, msg)
|
||||
}
|
||||
|
||||
func Send(ctx Context, addr Address, msg Message) {
|
||||
if srv, ok := ctx.Services()[addr.Service()]; ok {
|
||||
srv.Mailbox() <- msg
|
||||
}
|
||||
}
|
||||
|
||||
func GetState[St ContextState](ctx Context) St {
|
||||
return ctx.State().(St)
|
||||
}
|
||||
|
||||
func GetCfg[C Config](ctx Context) C {
|
||||
return ctx.Config().(C)
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
package logging
|
||||
|
||||
import (
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
|
136
scopes.go
136
scopes.go
|
@ -1 +1,137 @@
|
|||
// Package `scopes` provides a set of common types (mostly interfaces
|
||||
// and function declarations) and some basic functions.
|
||||
package scopes
|
||||
|
||||
import (
|
||||
stdlib_context "context"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// config
|
||||
|
||||
type ActionSpec interface {
|
||||
Handler() ActionHandler
|
||||
Receivers() []string
|
||||
}
|
||||
|
||||
type Pattern interface {
|
||||
fmt.Stringer
|
||||
Slice() []string
|
||||
}
|
||||
|
||||
type ActionConfig interface {
|
||||
Pattern() Pattern
|
||||
Specs() []ActionSpec
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
Name() string
|
||||
Starter() StartProc
|
||||
Step() StepProc
|
||||
MessageHandler() MessageHandler
|
||||
DoneHandler() StepProc
|
||||
Actions() []ActionConfig
|
||||
AddAction(string, ...ActionSpec) Config
|
||||
Children() []Config
|
||||
Add(...Config) Config
|
||||
}
|
||||
|
||||
// services - context
|
||||
|
||||
type Services map[string]Context
|
||||
|
||||
type ContextState interface{}
|
||||
|
||||
type Context interface {
|
||||
stdlib_context.Context
|
||||
Config() Config
|
||||
Parent() Context
|
||||
Services() Services
|
||||
ChildContext(Config) Context
|
||||
State() ContextState
|
||||
WithState(ContextState) Context
|
||||
Mailbox() chan Message
|
||||
WaitGroup() *sync.WaitGroup
|
||||
Stop()
|
||||
}
|
||||
|
||||
// message, address
|
||||
|
||||
type MsgHead interface {
|
||||
fmt.Stringer
|
||||
Slice() []string
|
||||
Domain() string
|
||||
Action() string
|
||||
Class() string
|
||||
Item() string
|
||||
}
|
||||
|
||||
type Message interface {
|
||||
MsgHead
|
||||
Head() MsgHead
|
||||
Sender() Address
|
||||
Payload() Payload
|
||||
WithSender(Address) Message
|
||||
WithPayload(Payload) Message
|
||||
}
|
||||
|
||||
type Address interface {
|
||||
fmt.Stringer
|
||||
Url() string
|
||||
Service() string
|
||||
Interaction() (string, string)
|
||||
SetUrl(string)
|
||||
SetInteraction(string, string)
|
||||
}
|
||||
|
||||
type Payload interface {
|
||||
fmt.Stringer
|
||||
Data() interface{}
|
||||
FromJson(string) Payload
|
||||
}
|
||||
|
||||
// action
|
||||
|
||||
type Action interface {
|
||||
Context() Context
|
||||
Spec() ActionSpec
|
||||
Message() Message
|
||||
Handle() bool
|
||||
}
|
||||
|
||||
// procedures and handlers
|
||||
|
||||
type Proc = func(Context)
|
||||
type StartProc = Proc
|
||||
type StepProc = func(Context) bool
|
||||
type MessageHandler = func(Context, Message) bool
|
||||
type ActionHandler = func(Action) bool
|
||||
|
||||
// library functions
|
||||
|
||||
func RunCtx(ctx Context, fct Proc) {
|
||||
ctx.WaitGroup().Add(1)
|
||||
go func() {
|
||||
defer ctx.WaitGroup().Done()
|
||||
fct(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
func HandleMsg(ctx Context, msg Message) bool {
|
||||
return ctx.Config().MessageHandler()(ctx, msg)
|
||||
}
|
||||
|
||||
func Send(ctx Context, addr Address, msg Message) {
|
||||
if srv, ok := ctx.Services()[addr.Service()]; ok {
|
||||
srv.Mailbox() <- msg
|
||||
}
|
||||
}
|
||||
|
||||
func GetState[St ContextState](ctx Context) St {
|
||||
return ctx.State().(St)
|
||||
}
|
||||
|
||||
func GetCfg[C Config](ctx Context) C {
|
||||
return ctx.Config().(C)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package server
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog"
|
||||
|
|
|
@ -3,9 +3,9 @@ package server
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/core"
|
||||
"git.sr.ht/~cco/go-scopes/core"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
|
|
@ -4,16 +4,16 @@ import (
|
|||
"fmt"
|
||||
tbase "testing"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/common/testing"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/action"
|
||||
"git.sr.ht/~cco/go-scopes/lib/context"
|
||||
"git.sr.ht/~cco/go-scopes/lib/core"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
"git.sr.ht/~cco/go-scopes/core"
|
||||
"git.sr.ht/~cco/go-scopes/core/action"
|
||||
"git.sr.ht/~cco/go-scopes/core/context"
|
||||
"git.sr.ht/~cco/go-scopes/core/message"
|
||||
)
|
||||
|
||||
func TestLib(tb *tbase.T) {
|
||||
func TestCore(tb *tbase.T) {
|
||||
t := testing.SetUp(tb)
|
||||
t.Run("address", AddressTest)
|
||||
t.Run("message", MessageTest)
|
|
@ -1,13 +1,13 @@
|
|||
package etc
|
||||
|
||||
import (
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/app"
|
||||
"git.sr.ht/~cco/go-scopes/client"
|
||||
"git.sr.ht/~cco/go-scopes/common/testing"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/action"
|
||||
"git.sr.ht/~cco/go-scopes/lib/core"
|
||||
"git.sr.ht/~cco/go-scopes/core"
|
||||
"git.sr.ht/~cco/go-scopes/core/action"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
"git.sr.ht/~cco/go-scopes/server"
|
||||
)
|
||||
|
|
|
@ -4,10 +4,10 @@ import (
|
|||
"os"
|
||||
tbase "testing"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/app"
|
||||
"git.sr.ht/~cco/go-scopes/common/testing"
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
"git.sr.ht/~cco/go-scopes/core/message"
|
||||
"git.sr.ht/~cco/go-scopes/tests/etc"
|
||||
)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue