From ec1ab12cef3646d198a61b1ddc01781a4a8fd07d Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 13 Aug 2023 17:05:20 +0200 Subject: [PATCH] unifying data structures for message payload and server response --- core/message/message.go | 6 ++---- scopes.go | 17 ++++++++++------- server/server.go | 8 +++----- tests/core_test.go | 4 ++-- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/core/message/message.go b/core/message/message.go index 537a7b1..3673976 100644 --- a/core/message/message.go +++ b/core/message/message.go @@ -94,9 +94,7 @@ var Quit = SimpleMessage("quit") // payload -type payload struct { - data interface{} -} +type payload struct{ data lib.Data } func (pl *payload) String() string { b, err := json.Marshal(pl.data) @@ -106,7 +104,7 @@ func (pl *payload) String() string { return string(b) } -func (pl *payload) Data() interface{} { +func (pl *payload) Data() lib.Data { return pl.data } diff --git a/scopes.go b/scopes.go index 2abc017..45e9c61 100644 --- a/scopes.go +++ b/scopes.go @@ -30,10 +30,19 @@ type Context interface { Send(Message) } -// message, address +// data (payload), message, address type Data interface{} +type Map map[string]Data +type Slice []Data + +type Payload interface { + fmt.Stringer + Data() Data + FromJson(string) Payload +} + type MsgHead interface { fmt.Stringer Slice() []string @@ -67,12 +76,6 @@ type Address interface { SetInteraction(string, string) } -type Payload interface { - fmt.Stringer - Data() interface{} - FromJson(string) Payload -} - // action type Action interface { diff --git a/server/server.go b/server/server.go index 358422d..cdf65a8 100644 --- a/server/server.go +++ b/server/server.go @@ -12,8 +12,6 @@ import ( "github.com/gin-gonic/gin" ) -type data = map[string]interface{} - type ServerState struct { server *http.Server } @@ -77,13 +75,13 @@ func handleMsg(ctx lib.Context, cfg *mhSpec, gc *gin.Context) { } func Async(ctx lib.Context, msg lib.Message) (int, lib.Data) { - return http.StatusOK, data{"status": "OK"} + return http.StatusOK, lib.Map{"status": "OK"} } func Sync(ctx lib.Context, msg lib.Message) (int, lib.Data) { select { case msg := <-ctx.Mailbox(): - return http.StatusOK, data(msg.Payload().Data().(map[string]interface{})) + return http.StatusOK, msg.Payload().Data() } - return http.StatusOK, data{"status": "OK"} + return http.StatusOK, lib.Map{"status": "OK"} } diff --git a/tests/core_test.go b/tests/core_test.go index a25d2e1..fbbf451 100644 --- a/tests/core_test.go +++ b/tests/core_test.go @@ -56,10 +56,10 @@ func PayloadTest(t *testing.T) { //pl := struct{ text, work string }{"scopes messaging", "development"} pl1 := message.Payload(`development`) t.AssertEqual(fmt.Sprint(pl1), `"development"`) - pl2 := message.Payload(map[string]string{"activity": "development"}) + pl2 := message.Payload(lib.Map{"activity": "development"}) t.AssertEqual(fmt.Sprint(pl2), `{"activity":"development"}`) msg.WithPayload(pl2) - pl3 := message.PayloadFromJson[map[string]any](`{"activity": "development"}`) + pl3 := message.PayloadFromJson[lib.Map](`{"activity": "development"}`) t.AssertEqual(fmt.Sprint(pl3), `{"activity":"development"}`) }