Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
517 changes: 516 additions & 1 deletion README.md

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions base_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package crud

import (
"database/sql"
"time"

"github.com/google/uuid"
)
Comment thread
itsLeonB marked this conversation as resolved.

type BaseEntity struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()"`
Comment thread
itsLeonB marked this conversation as resolved.
CreatedAt time.Time
UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt sql.NullTime
}

func (be BaseEntity) IsZero() bool {
return be.ID == uuid.Nil
}

func (be BaseEntity) IsDeleted() bool {
return be.DeletedAt.Valid
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/itsLeonB/go-crud
go 1.25.0
Comment thread
itsLeonB marked this conversation as resolved.

require (
github.com/itsLeonB/ezutil/v2 v2.0.0-alpha
github.com/google/uuid v1.6.0
github.com/itsLeonB/ezutil/v2 v2.0.0
github.com/rotisserie/eris v0.5.4
github.com/stretchr/testify v1.11.0
gorm.io/driver/sqlite v1.6.0
Expand All @@ -12,7 +13,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/pretty v0.3.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/itsLeonB/ezutil/v2 v2.0.0-alpha h1:hXkL4U6KC7BAamUkdyCfbxK1mVJv/CDkLlVcVWxIcT4=
github.com/itsLeonB/ezutil/v2 v2.0.0-alpha/go.mod h1:fiUusldH3h+Y3vYimdloT+CBVO2AKT0xEtXvSHgvTts=
github.com/itsLeonB/ezutil/v2 v2.0.0 h1:4o6nVMzCIr56ggXifDG7Mr+ucDDUg3bVeuiNjsFVcRI=
github.com/itsLeonB/ezutil/v2 v2.0.0/go.mod h1:fiUusldH3h+Y3vYimdloT+CBVO2AKT0xEtXvSHgvTts=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down
4 changes: 1 addition & 3 deletions gorm_crud_repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package ezutil

// todo move to go-crud pkg
package crud

import (
"context"
Expand Down
4 changes: 1 addition & 3 deletions gorm_scopes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package ezutil

// todo move to go-crud pkg
package crud

import (
"reflect"
Expand Down
4 changes: 1 addition & 3 deletions gorm_transactor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package ezutil

// todo move to go-crud pkg
package crud

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion lib/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lib
type txKey string

const (
ContextKeyGormTx txKey = "ezutil.gormTx"
ContextKeyGormTx txKey = "go-crud.gormTx"

MsgTransactionError = "error processing transaction"
)
14 changes: 7 additions & 7 deletions test/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestConstants(t *testing.T) {
{
name: "ContextKeyGormTx value",
constant: string(lib.ContextKeyGormTx),
expected: "ezutil.gormTx",
expected: "go-crud.gormTx",
},
{
name: "MsgTransactionError value",
Expand All @@ -36,7 +36,7 @@ func TestContextKeyGormTx_Type(t *testing.T) {
// Verify that ContextKeyGormTx is of the correct type
// The constant is defined as txKey type, not string directly
keyStr := string(lib.ContextKeyGormTx)
assert.Equal(t, "ezutil.gormTx", keyStr, "ContextKeyGormTx string value should be correct")
assert.Equal(t, "go-crud.gormTx", keyStr, "ContextKeyGormTx string value should be correct")
}

func TestMsgTransactionError_Type(t *testing.T) {
Expand All @@ -52,7 +52,7 @@ func TestConstants_NotEmpty(t *testing.T) {

func TestConstants_Uniqueness(t *testing.T) {
constants := map[string]interface{}{
"ContextKeyGormTx": lib.ContextKeyGormTx,
"ContextKeyGormTx": lib.ContextKeyGormTx,
"MsgTransactionError": lib.MsgTransactionError,
}

Expand All @@ -70,9 +70,9 @@ func TestContextKeyGormTx_Usage(t *testing.T) {
// Should be usable as a context key (comparable type)
key1 := lib.ContextKeyGormTx
key2 := lib.ContextKeyGormTx

assert.Equal(t, key1, key2, "ContextKeyGormTx should be comparable and equal to itself")

// Should be different from other string values
otherKey := "some.other.key"
assert.NotEqual(t, string(key1), otherKey, "ContextKeyGormTx should be unique")
Expand All @@ -81,9 +81,9 @@ func TestContextKeyGormTx_Usage(t *testing.T) {
func TestMsgTransactionError_Usage(t *testing.T) {
// Test that the message is suitable for error messages
msg := lib.MsgTransactionError

assert.GreaterOrEqual(t, len(msg), 5, "MsgTransactionError should be a meaningful message")

// Should not contain special characters that might break error handling
for _, char := range msg {
assert.True(t, char >= 32 && char <= 126, "MsgTransactionError should not contain non-printable character: %d", char)
Expand Down
Loading