unifying data structures for message payload and server response

This commit is contained in:
Helmut Merz 2023-08-13 17:05:20 +02:00
parent d03b3ab11d
commit ec1ab12cef
4 changed files with 17 additions and 18 deletions

View file

@ -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
}

View file

@ -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 {

View file

@ -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"}
}

View file

@ -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"}`)
}