From 5352778b3c97a00e28500f94f70303575e024a97 Mon Sep 17 00:00:00 2001 From: Yehor Butko Date: Mon, 12 Nov 2018 09:14:16 +0000 Subject: [PATCH] refactored byte to uuid converting (#610) * refactored byte to uuid converting * linter fixed * error messages updated --- pkg/satellite/satellitedb/companies.go | 26 +++++++------- pkg/satellite/satellitedb/companies_test.go | 2 +- pkg/satellite/satellitedb/db/.keep | 0 pkg/satellite/satellitedb/users.go | 23 +++++-------- pkg/satellite/satellitedb/utils.go | 21 ++++++++++++ pkg/satellite/satellitedb/utils_test.go | 38 +++++++++++++++++++++ 6 files changed, 80 insertions(+), 30 deletions(-) delete mode 100644 pkg/satellite/satellitedb/db/.keep create mode 100644 pkg/satellite/satellitedb/utils.go create mode 100644 pkg/satellite/satellitedb/utils_test.go diff --git a/pkg/satellite/satellitedb/companies.go b/pkg/satellite/satellitedb/companies.go index a48c3a449..f1bfedbd1 100644 --- a/pkg/satellite/satellitedb/companies.go +++ b/pkg/satellite/satellitedb/companies.go @@ -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, diff --git a/pkg/satellite/satellitedb/companies_test.go b/pkg/satellite/satellitedb/companies_test.go index a4a0aa0a0..42512b02f 100644 --- a/pkg/satellite/satellitedb/companies_test.go +++ b/pkg/satellite/satellitedb/companies_test.go @@ -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"), } diff --git a/pkg/satellite/satellitedb/db/.keep b/pkg/satellite/satellitedb/db/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/pkg/satellite/satellitedb/users.go b/pkg/satellite/satellitedb/users.go index 3cc6ba969..caaad43df 100644 --- a/pkg/satellite/satellitedb/users.go +++ b/pkg/satellite/satellitedb/users.go @@ -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, diff --git a/pkg/satellite/satellitedb/utils.go b/pkg/satellite/satellitedb/utils.go new file mode 100644 index 000000000..0d6bdb86b --- /dev/null +++ b/pkg/satellite/satellitedb/utils.go @@ -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 +} diff --git a/pkg/satellite/satellitedb/utils_test.go b/pkg/satellite/satellitedb/utils_test.go new file mode 100644 index 000000000..5dcb3d909 --- /dev/null +++ b/pkg/satellite/satellitedb/utils_test.go @@ -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) + }) +}