From 35287594e99dbeb8d69b8d9f517fba076bf3f833 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 13 Jun 2014 10:46:58 -0700 Subject: [PATCH] refactor connection and adaptor --- adaptor.go | 15 +++++++++++++++ connection.go | 35 +++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/adaptor.go b/adaptor.go index 09772775..de925b29 100644 --- a/adaptor.go +++ b/adaptor.go @@ -10,4 +10,19 @@ type Adaptor struct { type AdaptorInterface interface { Finalize() bool Connect() bool + port() string + name() string + params() map[string]interface{} +} + +func (a *Adaptor) port() string { + return a.Port +} + +func (a *Adaptor) name() string { + return a.Name +} + +func (a *Adaptor) params() map[string]interface{} { + return a.Params } diff --git a/connection.go b/connection.go index bf3c48b2..5ede3cdf 100644 --- a/connection.go +++ b/connection.go @@ -9,6 +9,9 @@ import ( type Connection interface { Connect() bool Finalize() bool + port() string + name() string + params() map[string]interface{} } type JSONConnection struct { @@ -50,19 +53,15 @@ func (c connections) Finalize() { } func NewConnection(adaptor AdaptorInterface, r *Robot) *connection { - c := new(connection) - s := reflect.ValueOf(adaptor).Type().String() - c.Type = s[1:len(s)] - c.Name = FieldByNamePtr(adaptor, "Name").String() - c.Port = FieldByNamePtr(adaptor, "Port").String() - c.Params = make(map[string]interface{}) - keys := FieldByNamePtr(adaptor, "Params").MapKeys() - for k := range keys { - c.Params[keys[k].String()] = FieldByNamePtr(adaptor, "Params").MapIndex(keys[k]) + t := reflect.ValueOf(adaptor).Type().String() + return &connection{ + Type: t[1:len(t)], + Name: adaptor.name(), + Port: adaptor.port(), + Params: adaptor.params(), + Robot: r, + Adaptor: adaptor, } - c.Robot = r - c.Adaptor = adaptor - return c } func (c *connection) Connect() bool { @@ -82,3 +81,15 @@ func (c *connection) ToJSON() *JSONConnection { Adaptor: c.Type, } } + +func (c *connection) port() string { + return c.Port +} + +func (c *connection) name() string { + return c.Name +} + +func (c *connection) params() map[string]interface{} { + return c.Adaptor.params() +}