/
types.go
89 lines (71 loc) · 1.89 KB
/
types.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package githubclient
import (
"time"
"github.com/giantswarm/microerror"
"github.com/google/go-github/github"
)
type Repository struct {
Name string
Language string
Owner string
UpdatedAt time.Time
}
func newRepository(ghRepo *github.Repository, owner string) (Repository, error) {
if ghRepo == nil {
return Repository{}, microerror.Maskf(executionError, "expected non nil argument but got %#v", ghRepo)
}
name, err := toString(ghRepo.Name)
if err != nil {
return Repository{}, microerror.Maskf(executionError, "expected non nil %T.Name value but got %#v", ghRepo, ghRepo.Name)
}
language, err := toString(ghRepo.Language)
if err != nil {
language = ""
}
updatedAt, err := toTime(ghRepo.UpdatedAt)
if err != nil {
return Repository{}, microerror.Maskf(executionError, "expected non nil %T.UpdatedAt value for repository %#q but got %#v", ghRepo, name, ghRepo.UpdatedAt)
}
r := Repository{
Name: name,
Language: language,
Owner: owner,
UpdatedAt: updatedAt,
}
return r, nil
}
type RepositoryFile struct {
Data []byte
Path string
}
func newRepositoryFile(ghContent *github.RepositoryContent) (RepositoryFile, error) {
if ghContent == nil {
return RepositoryFile{}, microerror.Maskf(executionError, "expected non nil argument but got %#v", ghContent)
}
path, err := toString(ghContent.Path)
if err != nil {
return RepositoryFile{}, microerror.Mask(err)
}
data, err := ghContent.GetContent()
if err != nil {
return RepositoryFile{}, microerror.Mask(err)
}
r := RepositoryFile{
Data: []byte(data),
Path: path,
}
return r, err
}
func toString(p *string) (string, error) {
if p == nil {
return "", microerror.Maskf(executionError, "value is nil")
}
return *p, nil
}
func toTime(p *github.Timestamp) (time.Time, error) {
if p == nil {
return time.Time{}, microerror.Maskf(executionError, "value is nil")
}
t := *p
return t.Time, nil
}