turn Advise structure in idiomatic go code

This commit is contained in:
Marcelo Pires 2018-09-07 17:50:16 +02:00
parent 66218f4616
commit 0fce9349d0
4 changed files with 88 additions and 24 deletions

View File

@ -1,6 +1,11 @@
package message package message
import "errors" import (
"bytes"
"encoding/json"
"errors"
"time"
)
type Extension func(message *Message) type Extension func(message *Message)
@ -31,8 +36,79 @@ func (m *Message) GetError() error {
return errors.New(m.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 { type Advise struct {
Reconnect string `json:"reconnect,omitempty"` Reconnect Reconnect `json:"reconnect,omitempty"`
Interval int64 `json:"interval,omitempty"` Interval time.Duration `json:"interval,omitempty"`
Timeout int64 `json:"timeout,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
} }

View File

@ -51,6 +51,7 @@ func (s *Subscription) Channel() string {
return s.channel return s.channel
} }
//todo remove
func (s *Subscription) SubscriptionResult() chan error { func (s *Subscription) SubscriptionResult() chan error {
return s.ok return s.ok
} }

View File

@ -124,9 +124,13 @@ func TestSubscribeUnauthorizedChannel(t *testing.T) {
} }
_, err = client.Subscribe("/unauthorized") _, err = client.Subscribe("/unauthorized")
if err.Error() != unauthorizedErr.Error() { if err != nil {
t.Fatalf("expecting `500::unauthorized channel` got : `%s`", err.Error()) if err.Error() != unauthorizedErr.Error() {
t.Fatalf("expecting `500::unauthorized channel` got : `%s`", err.Error())
}
return
} }
log.Println(err)
t.Fatal("expecting error")
} }

View File

@ -69,23 +69,6 @@ const (
EventDelivery 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} var metaMessages = []MetaMessage{MetaSubscribe, MetaConnect, MetaUnsubscribe, MetaHandshake, MetaDisconnect}
func IsMetaMessage(msg *message.Message) bool { func IsMetaMessage(msg *message.Message) bool {