move address implementation to separate source file

This commit is contained in:
Helmut Merz 2023-07-03 12:21:02 +02:00
parent e1bc2322f3
commit b1cd65ea05
3 changed files with 73 additions and 60 deletions

View file

@ -4,9 +4,12 @@ package lib
import ( import (
stdlib_context "context" stdlib_context "context"
"fmt"
"sync" "sync"
) )
// config
type ActionSpec interface { type ActionSpec interface {
Handler() ActionHandler Handler() ActionHandler
Receivers() []string Receivers() []string
@ -29,6 +32,8 @@ type Config interface {
Add(...Config) Config Add(...Config) Config
} }
// services - context
type Services map[string]Context type Services map[string]Context
type ContextState interface{} type ContextState interface{}
@ -46,7 +51,10 @@ type Context interface {
Stop() Stop()
} }
// message, address
type Address interface { type Address interface {
fmt.Stringer
Url() string Url() string
Service() string Service() string
Interaction() (string, string) Interaction() (string, string)
@ -59,6 +67,8 @@ type Message interface {
Sender() Address Sender() Address
} }
// action
type Action interface { type Action interface {
Context() Context Context() Context
Spec() ActionSpec Spec() ActionSpec
@ -66,6 +76,8 @@ type Action interface {
Handle() bool Handle() bool
} }
// procedures and handlers
type Proc = func(Context) type Proc = func(Context)
type StartProc = Proc type StartProc = Proc
type StepProc = func(Context) bool type StepProc = func(Context) bool

61
lib/message/address.go Normal file
View file

@ -0,0 +1,61 @@
package message
import (
"fmt"
"strings"
)
// Address implementation
type address struct {
url, srv, sid, iid string
}
func (addr *address) String() string {
return fmt.Sprintf("%s#%s/%s/%s", addr.url, addr.srv, addr.sid, addr.iid)
}
func (addr *address) Url() string {
return addr.url
}
func (addr *address) Service() string {
return addr.srv
}
func (addr *address) Interaction() (sid, iid string) {
return addr.sid, addr.iid
}
func (addr *address) SetUrl(url string) {
addr.url = url
}
func (addr *address) SetInteraction(sid, iid string) {
addr.sid = sid
addr.iid = iid
}
func SimpleAddress(srv string) *address {
return &address{
srv: srv,
}
}
func ParseAddress(s string) *address {
addr := address{}
p := strings.SplitN(s, "#", 2)
if len(p) > 1 {
addr.url = p[0]
s = p[1]
}
p = strings.Split(s, "/")
addr.srv = p[0]
if len(p) > 1 {
addr.sid = p[1]
if len(p) > 2 {
addr.iid = p[2]
}
}
return &addr
}

View file

@ -1,9 +1,6 @@
package message package message
import ( import (
"fmt"
"strings"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
) )
@ -24,60 +21,3 @@ func StrMessage(action string) strMessage {
} }
var Quit = StrMessage("quit") var Quit = StrMessage("quit")
// Address implementation
type address struct {
url, srv, sid, iid string
}
func (addr *address) String() string {
return fmt.Sprintf("%s#%s/%s/%s", addr.url, addr.srv, addr.sid, addr.iid)
}
func (addr *address) Url() string {
return addr.url
}
func (addr *address) Service() string {
return addr.srv
}
func (addr *address) Interaction() (sid, iid string) {
return addr.sid, addr.iid
}
func (addr *address) SetUrl(url string) {
addr.url = url
}
func (addr *address) SetInteraction(sid, iid string) {
addr.sid = sid
addr.iid = iid
}
func SimpleAddress(srv string) *address {
return &address{
srv: srv,
}
}
func ParseAddress(astr string) *address {
addr := address{}
p1 := strings.SplitN(astr, "#", 2)
s1 := p1[0]
s2 := s1
if len(p1) > 1 {
addr.url = s1
s2 = p1[1]
}
p2 := strings.Split(s2, "/")
addr.srv = p2[0]
if len(p2) > 1 {
addr.sid = p2[1]
if len(p2) > 2 {
addr.iid = p2[2]
}
}
return &addr
}