Skip to content

Commit

Permalink
add id
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Jan 21, 2017
1 parent 1e2475f commit c5adc8c
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
74 changes: 74 additions & 0 deletions todoist/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package todoist

import (
"github.com/satori/go.uuid"
"strconv"
"fmt"
)

type ID string

func NewID(id string) (ID, error) {
if _, err := strconv.Atoi(id); err == nil {
return ID(id), nil
}
if IsTempID(ID(id)) {
return ID(id), nil
}
return "", fmt.Errorf("Invalid ID: %s", id)
}

func (i ID) MarshalJSON() ([]byte, error) {
s := string(i)
if IsTempID(i) {
s = `"` + s + `"`
}
return []byte(s), nil
}

func (i *ID) UnmarshalJSON(b []byte) (err error) {
s, err := strconv.Unquote(string(b))
if err != nil {
s = string(b) // integer id
}
id, err := NewID(s)
if err != nil {
return err
}
*i = id
return nil
}

func GenerateTempID() ID {
return ID(uuid.NewV4().String())
}

func IsTempID(id ID) bool {
if _, err := uuid.FromString(string(id)); err == nil {
return true
}
return false
}

type UUID string

func GenerateUUID() UUID {
return UUID(uuid.NewV4().String())
}

func (i UUID) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(string(i))), nil
}

func (i *UUID) UnmarshalJSON(b []byte) (err error) {
s, err := strconv.Unquote(string(b))
if err != nil {
return err
}
id, err := uuid.FromString(s)
if err != nil {
return err
}
*i = UUID(id.String())
return nil
}
102 changes: 102 additions & 0 deletions todoist/id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package todoist

import (
"testing"
"reflect"
"errors"
"strconv"
)

var testIDs = []struct {
s string
v ID
e error
}{
{
"1000000",
ID("1000000"),
nil,
},
{
"df43406d-db7e-4ea5-b3b4-c822ccdab3bf",
ID("df43406d-db7e-4ea5-b3b4-c822ccdab3bf"),
nil,
},
{
"invalid",
"",
errors.New("Invalid ID: invalid"),
},
}

func TestNewID(t *testing.T) {
for _, test := range testIDs {
v, err := NewID(test.s)
if !reflect.DeepEqual(err, test.e) {
t.Errorf("Expect %s, but got %s", test.e, err)
} else if test.e == nil && v != test.v {
t.Errorf("Expect %s, but got %s", test.v, v)
}
}
}

func TestID_MarshalJSON(t *testing.T) {
test := testIDs[0]
b, err := test.v.MarshalJSON()
if err != nil || string(b) != test.s {
t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b))
}

test = testIDs[1]
b, err = test.v.MarshalJSON()
if err != nil || string(b) != strconv.Quote(test.s) {
t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b))
}
}

func TestID_UnmarshalJSON(t *testing.T) {
for _, test := range testIDs {
var v ID
err := v.UnmarshalJSON([]byte(test.s))
if !reflect.DeepEqual(err, test.e) {
t.Errorf("Expect %s, but got %s", test.e, err)
} else if test.e == nil && v != test.v {

}
}
}

func TestIsTempID(t *testing.T) {
test := testIDs[0]
if IsTempID(test.v) == true {
t.Errorf("%s is not temp id, but returns true", test.v)
}
test = testIDs[1]
if IsTempID(test.v) == false {
t.Errorf("%s is temp id, but returns false", test.v)
}
}

func TestUUID_MarshalJSON(t *testing.T) {
s := "c0afd2be-7576-4fc4-8b3c-696c4c6cf794"
v := UUID(s)
b, err := v.MarshalJSON()
if err != nil || string(b) != strconv.Quote(s) {
t.Errorf("Expect %s, but got %s", strconv.Quote(s), string(b))
}
}

func TestUUID_UnmarshalJSON(t *testing.T) {
s := "c0afd2be-7576-4fc4-8b3c-696c4c6cf794"
var v UUID
err := v.UnmarshalJSON([]byte(strconv.Quote(s)))
if err != nil || v != UUID(s) {
t.Errorf("Expect %s, but got %s", UUID(s), v)
}

s = "invalid"
err = v.UnmarshalJSON([]byte(s))
if err == nil {
t.Error("Expect error, but no error")
}
}

0 comments on commit c5adc8c

Please sign in to comment.