Skip to content

Commit

Permalink
feat: increase database module
Browse files Browse the repository at this point in the history
  • Loading branch information
dantasrafael committed May 14, 2024
1 parent b9c6439 commit c31607a
Show file tree
Hide file tree
Showing 24 changed files with 729 additions and 608 deletions.
3 changes: 0 additions & 3 deletions development-environment/database/add-dogs.sql

This file was deleted.

2 changes: 1 addition & 1 deletion development-environment/database/clear-database.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
delete from users;
delete from profiles;
delete from dog;
delete from contacts;
11 changes: 5 additions & 6 deletions development-environment/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ CREATE TABLE IF NOT EXISTS users (
CONSTRAINT fk_users_profiles FOREIGN KEY (profile_id) REFERENCES profiles (id)
);

CREATE TABLE IF NOT EXISTS dog
(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
characteristics TEXT[]
);
CREATE TABLE IF NOT EXISTS contacts (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE
);
1 change: 0 additions & 1 deletion development-environment/database/sql-tx/clear.sql

This file was deleted.

5 changes: 0 additions & 5 deletions development-environment/database/sql-tx/schema.sql

This file was deleted.

14 changes: 11 additions & 3 deletions pkg/base/transaction/mock_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ package transaction

import "context"

// mockSqlTransaction struct with implements Transaction for tests purpose
// mockSqlTransaction implements a transaction.Transaction
type mockSqlTransaction struct {
}

// NewMockTransaction create a mock transaction for tests purpose
// NewMockTransaction returns a new mockSqlTransaction.
//
// No parameters.
// Returns a Transaction.
func NewMockTransaction() Transaction {
return mockSqlTransaction{}
}

func (m mockSqlTransaction) ExecTx(ctx context.Context, fn func(ctx context.Context) error) error {
// Execute executes a mockSqlTransaction.
//
// ctx: The context for the transaction.
// fn: The function to be executed.
// Returns an error.
func (m mockSqlTransaction) Execute(ctx context.Context, fn func(ctx context.Context) error) error {
return fn(ctx)
}
7 changes: 4 additions & 3 deletions pkg/base/transaction/mock_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMockTransaction(t *testing.T) {
t.Run("execute no error", func(t *testing.T) {
f := func(ctx context.Context) error { return nil }
m := NewMockTransaction()

assert.NoError(t, m.ExecTx(context.Background(), f))
assert.NoError(t, m.Execute(context.Background(), f))
})

t.Run("execute with error", func(t *testing.T) {
Expand All @@ -23,6 +24,6 @@ func TestMockTransaction(t *testing.T) {
}
m := NewMockTransaction()

assert.ErrorIs(t, m.ExecTx(context.Background(), f), expectedErr)
assert.ErrorIs(t, m.Execute(context.Background(), f), expectedErr)
})
}
3 changes: 2 additions & 1 deletion pkg/base/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
)

// Transaction defines the interface for a transaction
type Transaction interface {
ExecTx(context.Context, func(ctx context.Context) error) error
Execute(context.Context, func(ctx context.Context) error) error
}
43 changes: 40 additions & 3 deletions pkg/base/types/iso_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,54 @@ import (
// IsoDate struct
type IsoDate time.Time

// Value converts iso date to sql driver value
// ParseIsoDate converts string to iso date.
//
// It takes a string value as input.
// Returns IsoDate and an error.
func ParseIsoDate(value string) (IsoDate, error) {
parsedDate, err := time.Parse(time.DateOnly, value)
if err != nil {
return IsoDate{}, err
}

return IsoDate(parsedDate), nil
}

// Value converts iso date to sql driver value.
//
// Returns driver.Value and an error.
func (t IsoDate) Value() (driver.Value, error) {
return time.Time(t), nil
}

// MarshalJSON converts iso date to json string format
// String returns the iso date formatted using the format string.
//
// No parameters.
// Returns a string.
func (t IsoDate) String() string {
return time.Time(t).Format(time.DateOnly)
}

// GoString returns the iso date in Go source code format string.
//
// No parameters.
// Returns a string.
func (t IsoDate) GoString() string {
return time.Time(t).GoString()
}

// MarshalJSON converts iso date to json string format.
//
// No parameters.
// Returns a byte slice and an error.
func (t IsoDate) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(t).Format(time.DateOnly))
}

// UnmarshalJSON converts json string to iso date
// UnmarshalJSON converts json string to iso date.
//
// It takes a byte slice as the input data.
// Returns an error.
func (t *IsoDate) UnmarshalJSON(data []byte) error {
var ptr *string
if err := json.Unmarshal(data, &ptr); err != nil {
Expand Down
41 changes: 39 additions & 2 deletions pkg/base/types/iso_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,54 @@ import (
// IsoTime struct
type IsoTime time.Time

// Value converts iso time to sql driver value
// ParseIsoTime converts string to iso time.
//
// It takes a string value as input.
// Returns IsoTime and an error.
func ParseIsoTime(value string) (IsoTime, error) {
parsedTime, err := time.Parse(time.TimeOnly, value)
if err != nil {
return IsoTime{}, err
}

return IsoTime(parsedTime), nil
}

// Value converts iso time to sql driver value.
//
// Returns driver.Value and an error.
func (t IsoTime) Value() (driver.Value, error) {
return time.Time(t), nil
}

// MarshalJSON converts iso time to json string format
// String returns the iso time formatted using the format string
//
// No parameters.
// Returns a string.
func (t IsoTime) String() string {
return time.Time(t).Format(time.TimeOnly)
}

// GoString returns the iso time in Go source code format string.
//
// No parameters.
// Returns a string.
func (t IsoTime) GoString() string {
return time.Time(t).GoString()
}

// MarshalJSON converts iso time to json string format.
//
// No parameters.
// Returns a byte slice and an error.
func (t IsoTime) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(t).Format(time.TimeOnly))
}

// UnmarshalJSON converts json string to iso time
//
// It takes a byte slice as the input data.
// Returns an error.
func (t *IsoTime) UnmarshalJSON(data []byte) error {
var ptr *string
if err := json.Unmarshal(data, &ptr); err != nil {
Expand Down
53 changes: 46 additions & 7 deletions pkg/base/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/types"
form "github.com/go-playground/form/v4"
playValidator "github.com/go-playground/validator/v10"
"github.com/google/uuid"
Expand All @@ -13,25 +14,63 @@ type Validator struct {

var instance *Validator

// Initialize initializes the Validator instance with playValidator and formDecoder, then registers custom types.
//
// No parameters.
// No return values.
func Initialize() {
instance = &Validator{
validator: playValidator.New(),
formDecoder: form.NewDecoder(),
}

registerCustomTypes()
registerUUIDCustomType()
registerIsoDateCustomType()
registerIsoTimeCustomType()
}

// registerUUIDCustomType registers a custom type function for UUID parsing.
//
// It takes an array of strings as input parameters and returns an any type and an error.
func registerUUIDCustomType() {
instance.formDecoder.RegisterCustomTypeFunc(func(vals []string) (any, error) {
return uuid.Parse(vals[0])
}, uuid.UUID{})
}

// registerIsoDateCustomType registers a custom type function for ISO date parsing.
//
// It takes an array of strings as input parameters and returns an interface{} and an error.
func registerIsoDateCustomType() {
instance.formDecoder.RegisterCustomTypeFunc(func(vals []string) (interface{}, error) {
return types.ParseIsoDate(vals[0])
}, types.IsoDate{})
}

// registerIsoTimeCustomType registers a custom type function for ISO time parsing.
//
// It takes an array of strings as input parameters and returns an interface{} and an error.
func registerIsoTimeCustomType() {
instance.formDecoder.RegisterCustomTypeFunc(func(vals []string) (interface{}, error) {
return types.ParseIsoTime(vals[0])
}, types.IsoTime{})
}

// Struct performs validation on the provided object using the validator instance.
//
// Parameter:
// - object: the object to be validated
// Return type: error
func Struct(object any) error {
return instance.validator.Struct(object)
}

// FormDecode decodes the values from the map[string][]string into the provided object using the formDecoder instance.
//
// Parameters:
// - object: the object to be decoded
// - values: the map containing the values to be decoded
// Return type: error
func FormDecode(object any, values map[string][]string) error {
return instance.formDecoder.Decode(object, values)
}

func registerCustomTypes() {
instance.formDecoder.RegisterCustomTypeFunc(func(vals []string) (any, error) {
return uuid.Parse(vals[0])
}, uuid.UUID{})
}
Loading

0 comments on commit c31607a

Please sign in to comment.