Skip to content

Commit

Permalink
Make sure all entities have a client
Browse files Browse the repository at this point in the history
field. Add a public SetClient method
to allow overriding it.
  • Loading branch information
adlio committed Mar 29, 2021
1 parent 0f3d8d6 commit d40b381
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 8 deletions.
7 changes: 7 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// Actions are immutable event traces generated whenever an action occurs in Trello.
// See https://developers.trello.com/reference/#actions.
type Action struct {
client *Client
ID string `json:"id"`
IDMemberCreator string `json:"idMemberCreator"`
Type string `json:"type"`
Expand Down Expand Up @@ -173,6 +174,12 @@ func (a *Action) DidCommentCard() bool {
}
}

// SetClient can be used to override this Action's internal connection to
// the Trello API. Normally, this is set automatically after API calls.
func (a *Action) SetClient(newClient *Client) {
a.client = newClient
}

// ListAfterAction calculates which List the card ended up in after this action
// completed. Returns nil when the action resulted in the card being archived (in
// which case we consider it to not be in a list anymore), or when the action isn't
Expand Down
11 changes: 10 additions & 1 deletion action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestListAfterActionOnCopyCard(t *testing.T) {
}
}

func TestAction_DidCommentCard(t *testing.T) {
func TestActionDidCommentCard(t *testing.T) {
tests := []struct {
name string
fields *Action
Expand Down Expand Up @@ -142,3 +142,12 @@ func TestAction_DidCommentCard(t *testing.T) {
})
}
}

func TestActionSetClient(t *testing.T) {
a := Action{}
client := testClient()
a.SetClient(client)
if a.client == nil {
t.Error("Expected non-nil Action.client")
}
}
8 changes: 8 additions & 0 deletions attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package trello
// Attachment represent the attachments of cards. This is a nested resource of Card.
// https://developers.trello.com/reference/#attachments
type Attachment struct {
client *Client
ID string `json:"id"`
Card *Card `json:"-"`
Name string `json:"name"`
Pos float32 `json:"pos"`
Bytes int `json:"int"`
Expand All @@ -30,3 +32,9 @@ type AttachmentPreview struct {
Bytes int `json:"bytes"`
Scaled bool `json:"scaled"`
}

// SetClient can be used to override this Attachment's internal connection to
// the Trello API. Normally, this is set automatically after API calls.
func (a *Attachment) SetClient(newClient *Client) {
a.client = newClient
}
12 changes: 12 additions & 0 deletions attachment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package trello

import "testing"

func TestAttachmentSetClient(t *testing.T) {
a := Attachment{}
client := testClient()
a.SetClient(client)
if a.client == nil {
t.Error("Expected non-nil Attachment.client")
}
}
31 changes: 25 additions & 6 deletions checklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ import "fmt"
// A card can have one zero or more checklists.
// https://developers.trello.com/reference/#checklist-object
type Checklist struct {
client *Client
ID string `json:"id"`
Name string `json:"name"`
IDBoard string `json:"idBoard,omitempty"`
IDCard string `json:"idCard,omitempty"`
Card *Card `json:"-"`
Pos float64 `json:"pos,omitempty"`
CheckItems []CheckItem `json:"checkItems,omitempty"`
client *Client
}

// CheckItem is a nested resource representing an item in Checklist.
type CheckItem struct {
ID string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
IDChecklist string `json:"idChecklist,omitempty"`
Pos float64 `json:"pos,omitempty"`
client *Client
ID string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
IDChecklist string `json:"idChecklist,omitempty"`
Checklist *Checklist `json:"-"`
Pos float64 `json:"pos,omitempty"`
}

// CheckItemState represents a CheckItem when it appears in CheckItemStates on a Card.
Expand Down Expand Up @@ -103,3 +106,19 @@ func (c *Client) GetChecklist(checklistID string, args Arguments) (checklist *Ch
}
return checklist, err
}

// SetClient can be used to override this Checklist's internal connection to the
// Trello API. Normally, this is set automatically after API calls.
func (cl *Checklist) SetClient(newClient *Client) {
cl.client = newClient
for _, checkitem := range cl.CheckItems {
checkitem.SetClient(newClient)
checkitem.Checklist = cl
}
}

// SetClient can be used to override this CheckItem's internal connection to the
// Trello API. Normally, this is set automatically after API calls.
func (ci *CheckItem) SetClient(client *Client) {
ci.client = client
}
18 changes: 18 additions & 0 deletions checklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ func TestGetChecklist(t *testing.T) {
}
}

func TestChecklistSetClient(t *testing.T) {
cl := Checklist{}
client := testClient()
cl.SetClient(client)
if cl.client == nil {
t.Error("Expected non-nil CheckList.client")
}
}

func TestCheckItemSetClient(t *testing.T) {
ci := CheckItem{}
client := testClient()
ci.SetClient(client)
if ci.client == nil {
t.Error("Expected non-nil CheckItem.client")
}
}

// Utility function to get a simple response from Client.GetChecklist()
//
func testChecklist(t *testing.T) *Checklist {
Expand Down
7 changes: 7 additions & 0 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "fmt"
// Labels are defined per board, and can be applied to the cards on that board.
// https://developers.trello.com/reference/#label-object
type Label struct {
client *Client
ID string `json:"id"`
IDBoard string `json:"idBoard"`
Name string `json:"name"`
Expand All @@ -34,3 +35,9 @@ func (b *Board) GetLabels(extraArgs ...Arguments) (labels []*Label, err error) {
err = b.client.Get(path, args, &labels)
return
}

// SetClient can be used to override this Label's internal connection to the
// Trello API. Normally, this is set automatically after API calls.
func (l *Label) SetClient(newClient *Client) {
l.client = newClient
}
9 changes: 9 additions & 0 deletions label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func TestGetLabelsOnBoard(t *testing.T) {
}
}

func TestLabelSetClient(t *testing.T) {
l := Label{}
client := testClient()
l.SetClient(client)
if l.client == nil {
t.Error("Expected non-nil Label.client")
}
}

// Utility function to get the standard case Client.GetList() response
//
func testLabel(t *testing.T) *Label {
Expand Down
6 changes: 6 additions & 0 deletions member.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ func (c *Card) GetMembers(extraArgs ...Arguments) (members []*Member, err error)
}
return
}

// SetClient can be used to override this Member's internal connection to the
// Trello API. Normally, this is set automatically after API calls.
func (m *Member) SetClient(newClient *Client) {
m.client = newClient
}
9 changes: 9 additions & 0 deletions member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ func TestGetMembersOnCard(t *testing.T) {
t.Errorf("Expected 1 member, got %d", len(members))
}
}

func TestMemberSetClient(t *testing.T) {
m := Member{}
client := testClient()
m.SetClient(client)
if m.client == nil {
t.Error("Expected non-nil Member.client")
}
}
8 changes: 7 additions & 1 deletion notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ func (c *Client) GetMyNotifications(extraArgs ...Arguments) (notifications []*No
path := "members/me/notifications"
err = c.Get(path, args, &notifications)
for i := range notifications {
notifications[i].client = c
notifications[i].SetClient(c)
}
return
}

// SetClient can be used to override this Notification's internal connection to
// the Trello API. Normally, this is set automatically after API calls.
func (n *Notification) SetClient(newClient *Client) {
n.client = newClient
}
9 changes: 9 additions & 0 deletions notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ func TestGetMyNotifications(t *testing.T) {
t.Errorf("Name of second notification incorrect. Got: '%s'", notifications[1].Data.Board.Name)
}
}

func TestNotificationSetClient(t *testing.T) {
n := Notification{}
client := testClient()
n.SetClient(client)
if n.client == nil {
t.Error("Expected non-nil Notification.client")
}
}
6 changes: 6 additions & 0 deletions organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ func (c *Client) GetOrganization(orgID string, extraArgs ...Arguments) (organiza
}
return
}

// SetClient can be used to override this Organization's internal connection
// to the Trello API. Normally, this is set automatically after API calls.
func (o *Organization) SetClient(newClient *Client) {
o.client = newClient
}
9 changes: 9 additions & 0 deletions organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func TestGetOrganization(t *testing.T) {
}
}

func TestOrganizationSetClient(t *testing.T) {
o := Organization{}
client := testClient()
o.SetClient(client)
if o.client == nil {
t.Error("Expected non-nil Organization.client")
}
}

func testOrganization(t *testing.T) *Organization {
client := testClient()
client.BaseURL = mockResponse("organizations", "culturefoundry.json").URL
Expand Down
6 changes: 6 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ func (c *Client) GetToken(tokenID string, extraArgs ...Arguments) (token *Token,
}
return
}

// SetClient can be used to override this Token's internal connection to the
// Trello API. Normally, this is set automatically after API calls.
func (t *Token) SetClient(newClient *Client) {
t.client = newClient
}
9 changes: 9 additions & 0 deletions token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ func TestGetExpiringToken(t *testing.T) {
}
}

func TestTokenSetClient(t *testing.T) {
tok := Token{}
client := testClient()
tok.SetClient(client)
if tok.client == nil {
t.Error("Expected non-nil Member.client")
}
}

func testToken(t *testing.T) *Token {
client := testClient()
client.BaseURL = mockResponse("tokens", "token.json").URL
Expand Down

0 comments on commit d40b381

Please sign in to comment.