From b1cd65ea05f25b31a2c57c655c074c9a7d1fc583 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Mon, 3 Jul 2023 12:21:02 +0200 Subject: [PATCH] move address implementation to separate source file --- lib/lib.go | 12 +++++++++ lib/message/address.go | 61 ++++++++++++++++++++++++++++++++++++++++++ lib/message/message.go | 60 ----------------------------------------- 3 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 lib/message/address.go diff --git a/lib/lib.go b/lib/lib.go index d1413eb..b92c297 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -4,9 +4,12 @@ package lib import ( stdlib_context "context" + "fmt" "sync" ) +// config + type ActionSpec interface { Handler() ActionHandler Receivers() []string @@ -29,6 +32,8 @@ type Config interface { Add(...Config) Config } +// services - context + type Services map[string]Context type ContextState interface{} @@ -46,7 +51,10 @@ type Context interface { Stop() } +// message, address + type Address interface { + fmt.Stringer Url() string Service() string Interaction() (string, string) @@ -59,6 +67,8 @@ type Message interface { Sender() Address } +// action + type Action interface { Context() Context Spec() ActionSpec @@ -66,6 +76,8 @@ type Action interface { Handle() bool } +// procedures and handlers + type Proc = func(Context) type StartProc = Proc type StepProc = func(Context) bool diff --git a/lib/message/address.go b/lib/message/address.go new file mode 100644 index 0000000..dbb71ae --- /dev/null +++ b/lib/message/address.go @@ -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 +} diff --git a/lib/message/message.go b/lib/message/message.go index a24593e..7a623cb 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -1,9 +1,6 @@ package message import ( - "fmt" - "strings" - "git.sr.ht/~cco/go-scopes/lib" ) @@ -24,60 +21,3 @@ func StrMessage(action string) strMessage { } 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 -}