Skip to content

Commit

Permalink
Make ChannelID nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Aug 20, 2017
1 parent 1834073 commit 21bada8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 35 deletions.
2 changes: 1 addition & 1 deletion backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (ts *MsgTestSuite) TestMsgUnmarshal() {
err := json.Unmarshal([]byte(msgJSON), &msg)
ts.NoError(err)
ts.Equal(msg.ChannelUUID_.String(), "f3ad3eb6-d00d-4dc3-92e9-9f34f32940ba")
ts.Equal(msg.ChannelID_, courier.ChannelID(11))
ts.Equal(msg.ChannelID_, courier.NewChannelID(11))
ts.Equal([]string{"https://foo.bar/image.jpg"}, msg.Attachments())
ts.Equal(msg.ExternalID_, "")
}
Expand Down
41 changes: 7 additions & 34 deletions channel.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package courier

import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"

null "gopkg.in/guregu/null.v3"

uuid "github.com/satori/go.uuid"
)

Expand Down Expand Up @@ -60,43 +59,17 @@ func NewChannelUUID(u string) (ChannelUUID, error) {
}

// ChannelID is our SQL type for a channel's id
type ChannelID int64
type ChannelID struct {
null.Int
}

// NewChannelID creates a new ChannelID for the passed in int64
func NewChannelID(id int64) ChannelID {
return ChannelID(id)
}

// UnmarshalText satisfies text unmarshalling so ids can be decoded from forms
func (i *ChannelID) UnmarshalText(text []byte) (err error) {
id, err := strconv.ParseInt(string(text), 10, 64)
*i = ChannelID(id)
if err != nil {
return err
}
return err
}

// UnmarshalJSON satisfies json unmarshalling so ids can be decoded from JSON
func (i *ChannelID) UnmarshalJSON(bytes []byte) (err error) {
var id int64
err = json.Unmarshal(bytes, &id)
*i = ChannelID(id)
return err
}

// MarshalJSON satisfies json marshalling so ids can be encoded to JSON
func (i *ChannelID) MarshalJSON() ([]byte, error) {
return json.Marshal(int64(*i))
}

// String satisfies the Stringer interface
func (i *ChannelID) String() string {
return fmt.Sprintf("%d", i)
return ChannelID{null.NewInt(id, true)}
}

// NilChannelID is our nil value for ChannelIDs
var NilChannelID = ChannelID(0)
var NilChannelID = ChannelID{null.NewInt(0, false)}

// ErrChannelExpired is returned when our cached channel has outlived it's TTL
var ErrChannelExpired = errors.New("channel expired")
Expand Down

0 comments on commit 21bada8

Please sign in to comment.