-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.functions.go
73 lines (65 loc) · 2.29 KB
/
common.functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package taigo
import (
"encoding/json"
"strconv"
"github.com/google/go-querystring/query"
)
/*
// Attachments Manager
since go does not have generics, a common attachments manager had to be created
Taiga objects (epics, tasks, issues, etc...) can wrap around this method to simplify otherwise redundant requests
*/
// listAttachmentsForEndpoint is a common method to get attachments for an endpoint (userstories, tasks, etc...)
func listAttachmentsForEndpoint(c *Client, queryParams *attachmentsQueryParams) ([]Attachment, error) {
paramValues, _ := query.Values(queryParams)
url := c.MakeURL(queryParams.endpointURI, "attachments?", paramValues.Encode())
var attachments []Attachment
_, err := c.Request.Get(url, &attachments)
if err != nil {
return nil, err
}
return attachments, nil
}
// getAttachmentForEndpoint is a common method to get a specific attachment for an endpoint (epic, issue, etc...)
func getAttachmentForEndpoint(c *Client, attachmentID int, endpointURI string) (*Attachment, error) {
url := c.MakeURL(endpointURI, "attachments", strconv.Itoa(attachmentID))
var a Attachment
_, err := c.Request.Get(url, &a)
if err != nil {
return nil, err
}
return &a, nil
}
// convertStructViaJSON takes a model struct and converts it to another struct
//
// Since Type Conversion (https://golang.org/ref/spec#Conversions) is limited to identical types in go,
// JSON is used as an intermediate language to achieve this functionality
//
// NOTE: Both `sourcePtr` and `targetPtr` MUST BE POINTERS!
func convertStructViaJSON(sourcePtr interface{}, targetPtr interface{}) error {
payloadInJSON, err := json.Marshal(sourcePtr)
if err != nil {
return err
}
err = json.Unmarshal([]byte(payloadInJSON), targetPtr)
if err != nil {
return err
}
return nil
}
// isEmpty is a generic-ish function to check if a struct's field is empty/default
// it is convenient when making sure the bare minimum values are set when creating an object
func isEmpty(structField interface{}) bool {
if structField == nil {
return true
} else if structField == "" {
return true
} else if structField == false {
return true
}
return false
}
// projectIDQueryParam returns gives project ID formatted as a generic QueryParam
func projectIDQueryParam(ProjectID int) string {
return "?project=" + strconv.Itoa(ProjectID)
}