Skip to content

Commit

Permalink
refactor connection and adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 13, 2014
1 parent 97564d9 commit 3528759
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
15 changes: 15 additions & 0 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 23 additions & 12 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
type Connection interface {
Connect() bool
Finalize() bool
port() string
name() string
params() map[string]interface{}
}

type JSONConnection struct {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}

0 comments on commit 3528759

Please sign in to comment.