Skip to content

Commit

Permalink
tweaks from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed May 12, 2017
1 parent 034d4a5 commit c59625e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 43 deletions.
27 changes: 7 additions & 20 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package courier

import (
"database/sql"
"encoding/json"
"errors"
"log"
"strings"
"sync"
"time"

"github.com/nyaruka/courier/utils"
uuid "github.com/satori/go.uuid"
)

Expand Down Expand Up @@ -112,9 +111,6 @@ func loadChannelFromDB(s *server, channel *Channel, channelType ChannelType, uui
// select just the fields we need
err := s.db.Get(channel, lookupChannelFromUUIDSQL, channelType, uuid)

// parse our config
channel.parseConfig()

// we didn't find a match
if err == sql.ErrNoRows {
return ErrChannelNotFound
Expand Down Expand Up @@ -187,28 +183,19 @@ type Channel struct {
ChannelType ChannelType `json:"channel_type" db:"channel_type"`
Address sql.NullString `json:"address" db:"address"`
Country sql.NullString `json:"country" db:"country"`
Config sql.NullString `json:"config" db:"config"`

expiration time.Time
config map[string]string
Config utils.NullDict `json:"config" db:"config"`
expiration time.Time
}

// GetConfig returns the value of the passed in config key
func (c *Channel) GetConfig(key string) string { return c.config[key] }

func (c *Channel) parseConfig() {
c.config = make(map[string]string)

func (c *Channel) GetConfig(key string) string {
if c.Config.Valid {
err := json.Unmarshal([]byte(c.Config.String), &c.config)
if err != nil {
log.Printf("ERROR parsing channel config '%s': %s", c.Config.Value, err)
}
return c.Config.Dict[key]
}
return ""
}

// Constructor to create a new empty channel
func newChannel(channelType ChannelType, uuid ChannelUUID) *Channel {
config := make(map[string]string)
return &Channel{ChannelType: channelType, UUID: uuid, config: config}
return &Channel{ChannelType: channelType, UUID: uuid}
}
2 changes: 1 addition & 1 deletion courier.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ base_url = "https://localhost:8080"
port = 8080

# Our database connection string, right now only Postgres is supported
db = "postgres://courier@localhost/courier_test?sslmode=disable"
db = "postgres://courier@localhost/courier?sslmode=disable"

# Our redis connection string, path is our database
redis = "redis://localhost:6379/0"
Expand Down
9 changes: 4 additions & 5 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ type MsgVisibility string

// Possible values for MsgVisibility
const (
MsgVisible MsgVisibility = "V"
MsgDeleted MsgVisibility = "D"
MsgArchived MsgVisibility = "A"
NilMsgVisibility MsgVisibility = ""
MsgVisible MsgVisibility = "V"
MsgDeleted MsgVisibility = "D"
MsgArchived MsgVisibility = "A"
)

// MsgUUID is the UUID of a message which has been received
Expand Down Expand Up @@ -333,7 +332,7 @@ func (m *Msg) clear() {
m.Status = NilMsgStatus
m.Text = ""
m.Priority = DefaultPriority
m.Visibility = NilMsgVisibility
m.Visibility = MsgVisible
m.MediaURLs = nil
m.ExternalID = ""

Expand Down
14 changes: 2 additions & 12 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package courier

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/gorilla/mux"
_ "github.com/lib/pq" // postgres driver
"github.com/nyaruka/courier/config"
"github.com/nyaruka/courier/utils"
)

var testDatabaseURL = "postgres://courier@localhost/courier_test?sslmode=disable"
Expand Down Expand Up @@ -148,22 +148,12 @@ func (ts *MockServer) AddChannelRoute(handler ChannelHandler, method string, act
func NewMockChannel(uuid string, channelType string, address string, country string, config map[string]string) *Channel {
cUUID, _ := NewChannelUUID(uuid)

configJSON := ""
if config != nil {
configBytes, err := json.Marshal(config)
if err != nil {
panic(err)
}
configJSON = string(configBytes)
}

channel := &Channel{
UUID: cUUID,
ChannelType: ChannelType(channelType),
Address: sql.NullString{String: address, Valid: true},
Country: sql.NullString{String: country, Valid: true},
Config: sql.NullString{String: configJSON, Valid: true},
Config: utils.NullDict{Dict: config, Valid: true},
}
channel.parseConfig()
return channel
}
10 changes: 5 additions & 5 deletions testdata.sql
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@

/* Org with id 100 */
DELETE FROM orgs_org where id = 100;
DELETE FROM orgs_org;
INSERT INTO orgs_org("id", "name", "language")
VALUES(100, 'Test Org', 'eng');

/* Channel with id 101 */
DELETE FROM channels_channel WHERE id = 101;
DELETE FROM channels_channel;
INSERT INTO channels_channel("id", "is_active", "created_on", "modified_on", "uuid", "channel_type", "org_id", "country")
VALUES('101', 'Y', NOW(), NOW(), 'dbc126ed-66bc-4e28-b67b-81dc3327c95d', 'KN', 100, 'RW');

/* Contact with id 102 */
DELETE FROM contacts_contact WHERE id = 102;
DELETE FROM contacts_contact;
INSERT INTO contacts_contact("id", "is_active", "created_on", "modified_on", "uuid", "is_blocked", "is_test", "is_stopped", "language", "created_by_id", "modified_by_id", "org_id")
VALUES(102, True, now(), now(), 'a984069d-0008-4d8c-a772-b14a8a6acccc', False, False, False, 'eng', 1, 1, 100);

/** ContactURN with id 103 */
DELETE FROM contacts_contacturn WHERE id = 103;
DELETE FROM contacts_contacturn;
INSERT INTO contacts_contacturn("id", "urn", "path", "scheme", "priority", "channel_id", "contact_id", "org_id")
VALUES(103, 'tel:+12067799192', '+12067799192', 'tel', 50, 101, 102, 100);

/** Msg with id 104 */
DELETE FROM msgs_msg WHERE id = 104;
DELETE from msgs_msg;
INSERT INTO msgs_msg("id", "text", "priority", "created_on", "modified_on", "sent_on", "queued_on", "direction", "status", "visibility",
"has_template_error", "msg_count", "error_count", "next_attempt", "external_id", "channel_id", "contact_id", "contact_urn_id", "org_id")
VALUES(104, 'test message', 500, now(), now(), now(), now(), 'O', 'W', 'V',
Expand Down
68 changes: 68 additions & 0 deletions utils/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package utils

import (
"database/sql/driver"
"encoding/json"
"errors"
)

// NullDict is a one level deep dictionary that is represented as JSON in the database
// and a string map as JSON
type NullDict struct {
Dict map[string]string
Valid bool
}

// Scan implements the Scanner interface.
func (n NullDict) Scan(src interface{}) error {
if src == nil {
n.Valid = false
return nil
}

var source []byte
switch src.(type) {
case string:
source = []byte(src.(string))
case []byte:
source = src.([]byte)
default:
return errors.New("Incompatible type for NullDict")
}

// 0 length is same as nil
if len(source) == 0 {
return nil
}

n.Dict = make(map[string]string)
n.Valid = true
return json.Unmarshal(source, n.Dict)
}

// Value implements the driver Valuer interface.
func (n NullDict) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return json.Marshal(n.Dict)
}

// MarshalJSON returns the *j as the JSON encoding of our dict
func (n NullDict) MarshalJSON() ([]byte, error) {
if !n.Valid {
return nil, nil
}
return json.Marshal(n.Dict)
}

// UnmarshalJSON sets our dict from the passed in data
func (n NullDict) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}

n.Dict = make(map[string]string)
n.Valid = true
return json.Unmarshal(data, n.Dict)
}

0 comments on commit c59625e

Please sign in to comment.