Skip to content

Commit

Permalink
feat(repo): add a counter field (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neal committed May 21, 2021
1 parent 6202757 commit 06ba0f9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions database/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Repo struct {
Clone sql.NullString `sql:"clone"`
Branch sql.NullString `sql:"branch"`
Timeout sql.NullInt64 `sql:"timeout"`
Counter sql.NullInt32 `sql:"counter"`
Visibility sql.NullString `sql:"visibility"`
Private sql.NullBool `sql:"private"`
Trusted sql.NullBool `sql:"trusted"`
Expand Down Expand Up @@ -192,6 +193,7 @@ func (r *Repo) ToLibrary() *library.Repo {
repo.SetClone(r.Clone.String)
repo.SetBranch(r.Branch.String)
repo.SetTimeout(r.Timeout.Int64)
repo.SetCounter(int(r.Counter.Int32))
repo.SetVisibility(r.Visibility.String)
repo.SetPrivate(r.Private.Bool)
repo.SetTrusted(r.Trusted.Bool)
Expand Down Expand Up @@ -266,6 +268,7 @@ func RepoFromLibrary(r *library.Repo) *Repo {
Clone: sql.NullString{String: r.GetClone(), Valid: true},
Branch: sql.NullString{String: r.GetBranch(), Valid: true},
Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true},
Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true},
Visibility: sql.NullString{String: r.GetVisibility(), Valid: true},
Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true},
Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true},
Expand Down
3 changes: 3 additions & 0 deletions database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
want.SetClone("https://github.com/github/octocat.git")
want.SetBranch("master")
want.SetTimeout(30)
want.SetCounter(0)
want.SetVisibility("public")
want.SetPrivate(false)
want.SetTrusted(false)
Expand Down Expand Up @@ -292,6 +293,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
r.SetClone("https://github.com/github/octocat.git")
r.SetBranch("master")
r.SetTimeout(30)
r.SetCounter(0)
r.SetVisibility("public")
r.SetPrivate(false)
r.SetTrusted(false)
Expand Down Expand Up @@ -326,6 +328,7 @@ func testRepo() *Repo {
Clone: sql.NullString{String: "https://github.com/github/octocat.git", Valid: true},
Branch: sql.NullString{String: "master", Valid: true},
Timeout: sql.NullInt64{Int64: 30, Valid: true},
Counter: sql.NullInt32{Int32: 0, Valid: true},
Visibility: sql.NullString{String: "public", Valid: true},
Private: sql.NullBool{Bool: false, Valid: true},
Trusted: sql.NullBool{Bool: false, Valid: true},
Expand Down
29 changes: 29 additions & 0 deletions library/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Repo struct {
Clone *string `json:"clone,omitempty"`
Branch *string `json:"branch,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
Counter *int `json:"counter,omitempty"`
Visibility *string `json:"visibility,omitempty"`
Private *bool `json:"private,omitempty"`
Trusted *bool `json:"trusted,omitempty"`
Expand Down Expand Up @@ -202,6 +203,19 @@ func (r *Repo) GetTimeout() int64 {
return *r.Timeout
}

// GetCounter returns the Counter field.
//
// When the provided Repo type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (r *Repo) GetCounter() int {
// return zero value if Repo type or Counter field is nil
if r == nil || r.Counter == nil {
return 0
}

return *r.Counter
}

// GetVisibility returns the Visibility field.
//
// When the provided Repo type is nil, or the field within
Expand Down Expand Up @@ -449,6 +463,19 @@ func (r *Repo) SetTimeout(v int64) {
r.Timeout = &v
}

// SetCounter sets the Counter field.
//
// When the provided Repo type is nil, it
// will set nothing and immediately return.
func (r *Repo) SetCounter(v int) {
// return if Repo type is nil
if r == nil {
return
}

r.Counter = &v
}

// SetVisibility sets the Visibility field.
//
// When the provided Repo type is nil, it
Expand Down Expand Up @@ -584,6 +611,7 @@ func (r *Repo) String() string {
Org: %s,
Private: %t,
Timeout: %d,
Counter: %d,
Trusted: %t,
UserID: %d
Visibility: %s,
Expand All @@ -603,6 +631,7 @@ func (r *Repo) String() string {
r.GetOrg(),
r.GetPrivate(),
r.GetTimeout(),
r.GetCounter(),
r.GetTrusted(),
r.GetUserID(),
r.GetVisibility(),
Expand Down
4 changes: 4 additions & 0 deletions library/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func TestLibrary_Repo_Setters(t *testing.T) {
test.repo.SetClone(test.want.GetClone())
test.repo.SetBranch(test.want.GetBranch())
test.repo.SetTimeout(test.want.GetTimeout())
test.repo.SetCounter(test.want.GetCounter())
test.repo.SetVisibility(test.want.GetVisibility())
test.repo.SetPrivate(test.want.GetPrivate())
test.repo.SetTrusted(test.want.GetTrusted())
Expand Down Expand Up @@ -290,6 +291,7 @@ func TestLibrary_Repo_String(t *testing.T) {
Org: %s,
Private: %t,
Timeout: %d,
Counter: %d,
Trusted: %t,
UserID: %d
Visibility: %s,
Expand All @@ -309,6 +311,7 @@ func TestLibrary_Repo_String(t *testing.T) {
r.GetOrg(),
r.GetPrivate(),
r.GetTimeout(),
r.GetCounter(),
r.GetTrusted(),
r.GetUserID(),
r.GetVisibility(),
Expand All @@ -335,6 +338,7 @@ func testRepo() *Repo {
r.SetClone("https://github.com/github/octocat.git")
r.SetBranch("master")
r.SetTimeout(30)
r.SetCounter(0)
r.SetVisibility("public")
r.SetPrivate(false)
r.SetTrusted(false)
Expand Down

0 comments on commit 06ba0f9

Please sign in to comment.