unifying data structures for message payload and server response
This commit is contained in:
		
							parent
							
								
									d03b3ab11d
								
							
						
					
					
						commit
						ec1ab12cef
					
				
					 4 changed files with 17 additions and 18 deletions
				
			
		|  | @ -94,9 +94,7 @@ var Quit = SimpleMessage("quit") | ||||||
| 
 | 
 | ||||||
| // payload | // payload | ||||||
| 
 | 
 | ||||||
| type payload struct { | type payload struct{ data lib.Data } | ||||||
| 	data interface{} |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| func (pl *payload) String() string { | func (pl *payload) String() string { | ||||||
| 	b, err := json.Marshal(pl.data) | 	b, err := json.Marshal(pl.data) | ||||||
|  | @ -106,7 +104,7 @@ func (pl *payload) String() string { | ||||||
| 	return string(b) | 	return string(b) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (pl *payload) Data() interface{} { | func (pl *payload) Data() lib.Data { | ||||||
| 	return pl.data | 	return pl.data | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										17
									
								
								scopes.go
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								scopes.go
									
										
									
									
									
								
							|  | @ -30,10 +30,19 @@ type Context interface { | ||||||
| 	Send(Message) | 	Send(Message) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // message, address | // data (payload), message, address | ||||||
| 
 | 
 | ||||||
| type Data interface{} | type Data interface{} | ||||||
| 
 | 
 | ||||||
|  | type Map map[string]Data | ||||||
|  | type Slice []Data | ||||||
|  | 
 | ||||||
|  | type Payload interface { | ||||||
|  | 	fmt.Stringer | ||||||
|  | 	Data() Data | ||||||
|  | 	FromJson(string) Payload | ||||||
|  | } | ||||||
|  | 
 | ||||||
| type MsgHead interface { | type MsgHead interface { | ||||||
| 	fmt.Stringer | 	fmt.Stringer | ||||||
| 	Slice() []string | 	Slice() []string | ||||||
|  | @ -67,12 +76,6 @@ type Address interface { | ||||||
| 	SetInteraction(string, string) | 	SetInteraction(string, string) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type Payload interface { |  | ||||||
| 	fmt.Stringer |  | ||||||
| 	Data() interface{} |  | ||||||
| 	FromJson(string) Payload |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // action | // action | ||||||
| 
 | 
 | ||||||
| type Action interface { | type Action interface { | ||||||
|  |  | ||||||
|  | @ -12,8 +12,6 @@ import ( | ||||||
| 	"github.com/gin-gonic/gin" | 	"github.com/gin-gonic/gin" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type data = map[string]interface{} |  | ||||||
| 
 |  | ||||||
| type ServerState struct { | type ServerState struct { | ||||||
| 	server *http.Server | 	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) { | 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) { | func Sync(ctx lib.Context, msg lib.Message) (int, lib.Data) { | ||||||
| 	select { | 	select { | ||||||
| 	case msg := <-ctx.Mailbox(): | 	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"} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -56,10 +56,10 @@ func PayloadTest(t *testing.T) { | ||||||
| 	//pl := struct{ text, work string }{"scopes messaging", "development"} | 	//pl := struct{ text, work string }{"scopes messaging", "development"} | ||||||
| 	pl1 := message.Payload(`development`) | 	pl1 := message.Payload(`development`) | ||||||
| 	t.AssertEqual(fmt.Sprint(pl1), `"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"}`) | 	t.AssertEqual(fmt.Sprint(pl2), `{"activity":"development"}`) | ||||||
| 	msg.WithPayload(pl2) | 	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"}`) | 	t.AssertEqual(fmt.Sprint(pl3), `{"activity":"development"}`) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue