Skip to content

Commit

Permalink
feat: add Hash field to repo type (#37)
Browse files Browse the repository at this point in the history
* feat: add Hash field to repo type

* chore: fix linter errors
  • Loading branch information
jbrockopp committed Jan 29, 2020
1 parent 712ac70 commit 33636d2
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
17 changes: 17 additions & 0 deletions database/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ var (
// Repo type has an empty FullName field provided.
ErrEmptyRepoFullName = errors.New("empty repo full_name provided")

// ErrEmptyRepoHash defines the error type when a
// Repo type has an empty Hash field provided.
ErrEmptyRepoHash = errors.New("empty repo hash provided")

// ErrEmptyRepoName defines the error type when a
// Repo type has an empty Name field provided.
ErrEmptyRepoName = errors.New("empty repo name provided")
Expand All @@ -33,6 +37,7 @@ var (
type Repo struct {
ID sql.NullInt64 `sql:"id"`
UserID sql.NullInt64 `sql:"user_id"`
Hash sql.NullString `sql:"hash"`
Org sql.NullString `sql:"org"`
Name sql.NullString `sql:"name"`
FullName sql.NullString `sql:"full_name"`
Expand Down Expand Up @@ -71,6 +76,11 @@ func (r *Repo) Nullify() *Repo {
r.UserID.Valid = false
}

// check if the Hash field should be false
if len(r.Hash.String) == 0 {
r.Hash.Valid = false
}

// check if the Org field should be false
if len(r.Org.String) == 0 {
r.Org.Valid = false
Expand Down Expand Up @@ -121,6 +131,7 @@ func (r *Repo) ToLibrary() *library.Repo {

repo.SetID(r.ID.Int64)
repo.SetUserID(r.UserID.Int64)
repo.SetHash(r.Hash.String)
repo.SetOrg(r.Org.String)
repo.SetName(r.Name.String)
repo.SetFullName(r.FullName.String)
Expand Down Expand Up @@ -148,6 +159,11 @@ func (r *Repo) Validate() error {
return ErrEmptyRepoUserID
}

// verify the Hash field is populated
if len(r.Hash.String) == 0 {
return ErrEmptyRepoHash
}

// verify the Org field is populated
if len(r.Org.String) == 0 {
return ErrEmptyRepoOrg
Expand All @@ -172,6 +188,7 @@ func RepoFromLibrary(r *library.Repo) *Repo {
repo := &Repo{
ID: sql.NullInt64{Int64: r.GetID(), Valid: true},
UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true},
Hash: sql.NullString{String: r.GetHash(), Valid: true},
Org: sql.NullString{String: r.GetOrg(), Valid: true},
Name: sql.NullString{String: r.GetName(), Valid: true},
FullName: sql.NullString{String: r.GetFullName(), Valid: true},
Expand Down
29 changes: 29 additions & 0 deletions database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestDatabase_Repo_Nullify(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: 0, Valid: true},
UserID: sql.NullInt64{Int64: 0, Valid: true},
Hash: sql.NullString{String: "", Valid: true},
Org: sql.NullString{String: "", Valid: true},
Name: sql.NullString{String: "", Valid: true},
FullName: sql.NullString{String: "", Valid: true},
Expand All @@ -36,6 +37,7 @@ func TestDatabase_Repo_Nullify(t *testing.T) {
want := &Repo{
ID: sql.NullInt64{Int64: 0, Valid: false},
UserID: sql.NullInt64{Int64: 0, Valid: false},
Hash: sql.NullString{String: "", Valid: false},
Org: sql.NullString{String: "", Valid: false},
Name: sql.NullString{String: "", Valid: false},
FullName: sql.NullString{String: "", Valid: false},
Expand Down Expand Up @@ -82,6 +84,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
want := &library.Repo{
ID: &num64,
UserID: &num64,
Hash: &str,
Org: &str,
Name: &str,
FullName: &str,
Expand All @@ -101,6 +104,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: num64, Valid: true},
UserID: sql.NullInt64{Int64: num64, Valid: true},
Hash: sql.NullString{String: str, Valid: true},
Org: sql.NullString{String: str, Valid: true},
Name: sql.NullString{String: str, Valid: true},
FullName: sql.NullString{String: str, Valid: true},
Expand Down Expand Up @@ -131,6 +135,7 @@ func TestDatabase_Repo_Validate(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Hash: sql.NullString{String: "baz", Valid: true},
Org: sql.NullString{String: "foo", Valid: true},
Name: sql.NullString{String: "bar", Valid: true},
FullName: sql.NullString{String: "foo/bar", Valid: true},
Expand All @@ -149,6 +154,25 @@ func TestDatabase_Repo_Validate_NoUserID(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
Org: sql.NullString{String: "foo", Valid: true},
Hash: sql.NullString{String: "baz", Valid: true},
Name: sql.NullString{String: "bar", Valid: true},
FullName: sql.NullString{String: "foo/bar", Valid: true},
}

// run test
err := r.Validate()

if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestDatabase_Repo_Validate_NoHash(t *testing.T) {
// setup types
r := &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Org: sql.NullString{String: "foo", Valid: true},
Name: sql.NullString{String: "bar", Valid: true},
FullName: sql.NullString{String: "foo/bar", Valid: true},
}
Expand All @@ -166,6 +190,7 @@ func TestDatabase_Repo_Validate_NoOrg(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Hash: sql.NullString{String: "baz", Valid: true},
Name: sql.NullString{String: "bar", Valid: true},
FullName: sql.NullString{String: "foo/bar", Valid: true},
}
Expand All @@ -183,6 +208,7 @@ func TestDatabase_Repo_Validate_NoName(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Hash: sql.NullString{String: "baz", Valid: true},
Org: sql.NullString{String: "foo", Valid: true},
FullName: sql.NullString{String: "foo/bar", Valid: true},
}
Expand All @@ -199,6 +225,7 @@ func TestDatabase_Repo_Validate_NoFullName(t *testing.T) {
r := &Repo{
ID: sql.NullInt64{Int64: 1, Valid: true},
UserID: sql.NullInt64{Int64: 1, Valid: true},
Hash: sql.NullString{String: "baz", Valid: true},
Org: sql.NullString{String: "foo", Valid: true},
Name: sql.NullString{String: "bar", Valid: true},
}
Expand All @@ -219,6 +246,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
want := &Repo{
ID: sql.NullInt64{Int64: num64, Valid: true},
UserID: sql.NullInt64{Int64: num64, Valid: true},
Hash: sql.NullString{String: str, Valid: true},
Org: sql.NullString{String: str, Valid: true},
Name: sql.NullString{String: str, Valid: true},
FullName: sql.NullString{String: str, Valid: true},
Expand All @@ -238,6 +266,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
r := &library.Repo{
ID: &num64,
UserID: &num64,
Hash: &str,
Org: &str,
Name: &str,
FullName: &str,
Expand Down
27 changes: 27 additions & 0 deletions library/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "fmt"
type Repo struct {
ID *int64 `json:"id,omitempty"`
UserID *int64 `json:"user_id,omitempty"`
Hash *string `json:"-"`
Org *string `json:"org,omitempty"`
Name *string `json:"name,omitempty"`
FullName *string `json:"full_name,omitempty"`
Expand Down Expand Up @@ -53,6 +54,19 @@ func (r *Repo) GetUserID() int64 {
return *r.UserID
}

// GetHash returns the Hash 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) GetHash() string {
// return zero value if Repo type or Hash field is nil
if r == nil || r.Hash == nil {
return ""
}

return *r.Hash
}

// GetOrg returns the Org field.
//
// When the provided Repo type is nil, or the field within
Expand Down Expand Up @@ -274,6 +288,19 @@ func (r *Repo) SetUserID(v int64) {
r.UserID = &v
}

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

r.Hash = &v
}

// SetOrg sets the Org field.
//
// When the provided Repo type is nil, it
Expand Down
Loading

0 comments on commit 33636d2

Please sign in to comment.