refactored byte to uuid converting (#610)

* refactored byte to uuid converting

* linter fixed

* error messages updated
This commit is contained in:
Yehor Butko 2018-11-12 09:14:16 +00:00 committed by GitHub
parent e5c52c399b
commit 5352778b3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 30 deletions

View File

@ -21,8 +21,7 @@ type companies struct {
// Get is a method for querying company from the database by id
func (companies *companies) Get(ctx context.Context, id uuid.UUID) (*satellite.Company, error) {
company, err := companies.db.Get_Company_By_Id(ctx, dbx.Company_Id([]byte(id.String())))
company, err := companies.db.Get_Company_By_Id(ctx, dbx.Company_Id(id[:]))
if err != nil {
return nil, err
}
@ -33,8 +32,7 @@ func (companies *companies) Get(ctx context.Context, id uuid.UUID) (*satellite.C
// Get is a method for querying company from the database by user id
func (companies *companies) GetByUserID(ctx context.Context, userID uuid.UUID) (*satellite.Company, error) {
company, err := companies.db.Get_Company_By_UserId(ctx, dbx.Company_UserId([]byte(userID.String())))
company, err := companies.db.Get_Company_By_UserId(ctx, dbx.Company_UserId(userID[:]))
if err != nil {
return nil, err
}
@ -52,8 +50,8 @@ func (companies *companies) Insert(ctx context.Context, company *satellite.Compa
createdCompany, err := companies.db.Create_Company(
ctx,
dbx.Company_Id([]byte(companyID.String())),
dbx.Company_UserId([]byte(company.UserID.String())),
dbx.Company_Id(companyID[:]),
dbx.Company_UserId(company.UserID[:]),
dbx.Company_Name(company.Name),
dbx.Company_Address(company.Address),
dbx.Company_Country(company.Country),
@ -70,7 +68,7 @@ func (companies *companies) Insert(ctx context.Context, company *satellite.Compa
// Delete is a method for deleting company by Id from the database.
func (companies *companies) Delete(ctx context.Context, id uuid.UUID) error {
_, err := companies.db.Delete_Company_By_Id(ctx, dbx.Company_Id([]byte(id.String())))
_, err := companies.db.Delete_Company_By_Id(ctx, dbx.Company_Id(id[:]))
return err
}
@ -79,7 +77,7 @@ func (companies *companies) Delete(ctx context.Context, id uuid.UUID) error {
func (companies *companies) Update(ctx context.Context, company *satellite.Company) error {
_, err := companies.db.Update_Company_By_Id(
ctx,
dbx.Company_Id([]byte(company.ID.String())),
dbx.Company_Id(company.ID[:]),
*getCompanyUpdateFields(company))
return err
@ -91,19 +89,19 @@ func companyFromDBX(company *dbx.Company) (*satellite.Company, error) {
return nil, errs.New("company parameter is nil")
}
id, err := uuid.Parse(string(company.Id))
id, err := bytesToUUID(company.Id)
if err != nil {
return nil, errs.New("Id in not valid UUID string")
return nil, err
}
userID, err := uuid.Parse(string(company.UserId))
userID, err := bytesToUUID(company.UserId)
if err != nil {
return nil, errs.New("UserID in not valid UUID string")
return nil, err
}
comp := &satellite.Company{
ID: *id,
UserID: *userID,
ID: id,
UserID: userID,
Name: company.Name,
Address: company.Address,
Country: company.Country,

View File

@ -225,7 +225,7 @@ func TestCompanyFromDbx(t *testing.T) {
assert.Nil(t, err)
dbxCompany := dbx.Company{
Id: []byte(companyID.String()),
Id: companyID[:],
UserId: []byte("qweqwe"),
}

View File

@ -22,10 +22,7 @@ type users struct {
// Get is a method for querying user from the database by id
func (users *users) Get(ctx context.Context, id uuid.UUID) (*satellite.User, error) {
userID := dbx.User_Id([]byte(id.String()))
user, err := users.db.Get_User_By_Id(ctx, userID)
user, err := users.db.Get_User_By_Id(ctx, dbx.User_Id(id[:]))
if err != nil {
return nil, err
}
@ -51,13 +48,8 @@ func (users *users) GetByCredentials(ctx context.Context, password []byte, email
// Insert is a method for inserting user into the database
func (users *users) Insert(ctx context.Context, user *satellite.User) (*satellite.User, error) {
userID, err := uuid.New()
if err != nil {
return nil, err
}
createdUser, err := users.db.Create_User(ctx,
dbx.User_Id([]byte(userID.String())),
dbx.User_Id(user.ID[:]),
dbx.User_FirstName(user.FirstName),
dbx.User_LastName(user.LastName),
dbx.User_Email(user.Email),
@ -72,15 +64,16 @@ func (users *users) Insert(ctx context.Context, user *satellite.User) (*satellit
// Delete is a method for deleting user by Id from the database.
func (users *users) Delete(ctx context.Context, id uuid.UUID) error {
_, err := users.db.Delete_User_By_Id(ctx, dbx.User_Id([]byte(id.String())))
_, err := users.db.Delete_User_By_Id(ctx, dbx.User_Id(id[:]))
return err
}
// Update is a method for updating user entity
func (users *users) Update(ctx context.Context, user *satellite.User) error {
_, err := users.db.Update_User_By_Id(ctx,
dbx.User_Id([]byte(user.ID.String())),
dbx.User_Id(user.ID[:]),
dbx.User_Update_Fields{
FirstName: dbx.User_FirstName(user.FirstName),
LastName: dbx.User_LastName(user.LastName),
@ -98,13 +91,13 @@ func userFromDBX(user *dbx.User) (*satellite.User, error) {
return nil, errs.New("user parameter is nil")
}
id, err := uuid.Parse(string(user.Id))
id, err := bytesToUUID(user.Id)
if err != nil {
return nil, errs.New("Id in not valid UUID string")
return nil, err
}
u := &satellite.User{
ID: *id,
ID: id,
FirstName: user.FirstName,
LastName: user.LastName,
Email: user.Email,

View File

@ -0,0 +1,21 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
)
// bytesToUUID is used to convert []byte to UUID
func bytesToUUID(data []byte) (uuid.UUID, error) {
var id uuid.UUID
copy(id[:], data)
if len(id) != len(data) {
return uuid.UUID{}, errs.New("Invalid bytes array")
}
return id, nil
}

View File

@ -0,0 +1,38 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"testing"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/stretchr/testify/assert"
)
func TestBytesToUUID(t *testing.T) {
t.Run("Invalid input", func(t *testing.T) {
str := "not UUID string"
bytes := []byte(str)
_, err := bytesToUUID(bytes)
assert.NotNil(t, err)
assert.Error(t, err)
})
t.Run("Valid input", func(t *testing.T) {
id, err := uuid.New()
assert.NoError(t, err)
result, err := bytesToUUID(id[:])
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, result, *id)
})
}