From 0fce9349d0b9d7787e3ba1868403e868acd49496 Mon Sep 17 00:00:00 2001 From: Marcelo Pires Date: Fri, 7 Sep 2018 17:50:16 +0200 Subject: [PATCH] turn Advise structure in idiomatic go code --- message/message.go | 84 ++++++++++++++++++++++++++++++++++-- subscription/subscription.go | 1 + test/client_test.go | 10 +++-- transport/transport.go | 17 -------- 4 files changed, 88 insertions(+), 24 deletions(-) diff --git a/message/message.go b/message/message.go index 8f1379b..953e8c7 100644 --- a/message/message.go +++ b/message/message.go @@ -1,6 +1,11 @@ package message -import "errors" +import ( + "bytes" + "encoding/json" + "errors" + "time" +) type Extension func(message *Message) @@ -31,8 +36,79 @@ func (m *Message) GetError() error { return errors.New(m.Error) } +type Reconnect string + +const ( + //ReconnectRetry indicates that a client MAY attempt to reconnect with a /meta/connect message, + //after the interval (as defined by interval advice field or client-default backoff), and with the same credentials. + ReconnectRetry Reconnect = "retry" + + //ReconnectHandshake indicates that the server has terminated any prior connection status and the client MUST reconnect + // with a /meta/handshake message. + //A client MUST NOT automatically retry when a reconnect advice handshake has been received. + ReconnectHandshake Reconnect = "handshake" + + //ReconnectNone indicates a hard failure for the connect attempt. + //A client MUST respect reconnect advice none and MUST NOT automatically retry or handshake. + ReconnectNone Reconnect = "none" +) + type Advise struct { - Reconnect string `json:"reconnect,omitempty"` - Interval int64 `json:"interval,omitempty"` - Timeout int64 `json:"timeout,omitempty"` + Reconnect Reconnect `json:"reconnect,omitempty"` + Interval time.Duration `json:"interval,omitempty"` + Timeout time.Duration `json:"timeout,omitempty"` + MultipleClients bool `json:"multiple-clients,omitempty"` + Hosts []string `json:"hosts,omitempty"` +} + +func (a *Advise) MarshalJSON() ([]byte, error) { + type jsonStruct struct { + Reconnect string `json:"reconnect,omitempty"` + Interval int64 `json:"interval,omitempty"` + Timeout int64 `json:"timeout,omitempty"` + MultipleClients bool `json:"multiple-clients,omitempty"` + Hosts []string `json:"hosts,omitempty"` + } + var builder bytes.Buffer + err := json.NewEncoder(&builder).Encode(jsonStruct{ + Reconnect: string(a.Reconnect), + Interval: int64(a.Interval / time.Millisecond), + Timeout: int64(a.Timeout / time.Millisecond), + MultipleClients: a.MultipleClients, + Hosts: a.Hosts, + }) + + return builder.Bytes(), err + +} + +func (a *Advise) UnmarshalJSON(b []byte) error { + var raw map[string]interface{} + err := json.Unmarshal(b, &raw) + if err != nil { + return err + } + reconnect, ok := raw["reconnect"] + if ok { + a.Reconnect = Reconnect(reconnect.(string)) + } + interval, ok := raw["interval"] + if ok { + a.Interval = time.Duration(interval.(float64)) * time.Millisecond + } + timeout, ok := raw["timeout"] + if ok { + a.Timeout = time.Duration(timeout.(float64)) * time.Millisecond + } + mc, ok := raw["multiple-clients"] + if ok { + a.MultipleClients = mc.(bool) + } + + hosts, ok := raw["hosts"] + if ok { + a.Hosts = hosts.([]string) + } + + return nil } diff --git a/subscription/subscription.go b/subscription/subscription.go index 466ffaa..60a1c7c 100644 --- a/subscription/subscription.go +++ b/subscription/subscription.go @@ -51,6 +51,7 @@ func (s *Subscription) Channel() string { return s.channel } +//todo remove func (s *Subscription) SubscriptionResult() chan error { return s.ok } diff --git a/test/client_test.go b/test/client_test.go index 733d96d..b358514 100644 --- a/test/client_test.go +++ b/test/client_test.go @@ -124,9 +124,13 @@ func TestSubscribeUnauthorizedChannel(t *testing.T) { } _, err = client.Subscribe("/unauthorized") - if err.Error() != unauthorizedErr.Error() { - t.Fatalf("expecting `500::unauthorized channel` got : `%s`", err.Error()) + if err != nil { + if err.Error() != unauthorizedErr.Error() { + t.Fatalf("expecting `500::unauthorized channel` got : `%s`", err.Error()) + } + return } - log.Println(err) + + t.Fatal("expecting error") } diff --git a/transport/transport.go b/transport/transport.go index 25bf221..c62bf8c 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -69,23 +69,6 @@ const ( EventDelivery ) -type Reconnect = string - -const ( - //ReconnectRetry indicates that a client MAY attempt to reconnect with a /meta/connect message, - //after the interval (as defined by interval advice field or client-default backoff), and with the same credentials. - ReconnectRetry Reconnect = "retry" - - //ReconnectHandshake indicates that the server has terminated any prior connection status and the client MUST reconnect - // with a /meta/handshake message. - //A client MUST NOT automatically retry when a reconnect advice handshake has been received. - ReconnectHandshake Reconnect = "handshake" - - //ReconnectNone indicates a hard failure for the connect attempt. - //A client MUST respect reconnect advice none and MUST NOT automatically retry or handshake. - ReconnectNone Reconnect = "none" -) - var metaMessages = []MetaMessage{MetaSubscribe, MetaConnect, MetaUnsubscribe, MetaHandshake, MetaDisconnect} func IsMetaMessage(msg *message.Message) bool {