refactor connection and adaptor

This commit is contained in:
Adrian Zankich 2014-06-13 10:46:58 -07:00
parent 97564d9808
commit 35287594e9
2 changed files with 38 additions and 12 deletions

View File

@ -10,4 +10,19 @@ type Adaptor struct {
type AdaptorInterface interface { type AdaptorInterface interface {
Finalize() bool Finalize() bool
Connect() 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
} }

View File

@ -9,6 +9,9 @@ import (
type Connection interface { type Connection interface {
Connect() bool Connect() bool
Finalize() bool Finalize() bool
port() string
name() string
params() map[string]interface{}
} }
type JSONConnection struct { type JSONConnection struct {
@ -50,19 +53,15 @@ func (c connections) Finalize() {
} }
func NewConnection(adaptor AdaptorInterface, r *Robot) *connection { func NewConnection(adaptor AdaptorInterface, r *Robot) *connection {
c := new(connection) t := reflect.ValueOf(adaptor).Type().String()
s := reflect.ValueOf(adaptor).Type().String() return &connection{
c.Type = s[1:len(s)] Type: t[1:len(t)],
c.Name = FieldByNamePtr(adaptor, "Name").String() Name: adaptor.name(),
c.Port = FieldByNamePtr(adaptor, "Port").String() Port: adaptor.port(),
c.Params = make(map[string]interface{}) Params: adaptor.params(),
keys := FieldByNamePtr(adaptor, "Params").MapKeys() Robot: r,
for k := range keys { Adaptor: adaptor,
c.Params[keys[k].String()] = FieldByNamePtr(adaptor, "Params").MapIndex(keys[k])
} }
c.Robot = r
c.Adaptor = adaptor
return c
} }
func (c *connection) Connect() bool { func (c *connection) Connect() bool {
@ -82,3 +81,15 @@ func (c *connection) ToJSON() *JSONConnection {
Adaptor: c.Type, 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()
}