From f5078a04e289e383a81ec9db1b4d0a5c95d6f309 Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Mon, 8 Dec 2025 14:39:54 +0300 Subject: [PATCH 1/3] add prepared statements config --- sqlr/db.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sqlr/db.go b/sqlr/db.go index 58d3033..08b595e 100644 --- a/sqlr/db.go +++ b/sqlr/db.go @@ -93,7 +93,10 @@ func parseConfig(configs ...*Config) *Config { func createInstance(config *Config) (gormDB *gorm.DB, sqlDB *sql.DB, err error) { if strings.HasPrefix(config.Url, "postgres") { - return createPool(postgres.Open(config.Url), config) + return createPool(postgres.New(postgres.Config{ + DSN: config.Url, + PreferSimpleProtocol: config.PreparedStatements, + }), config) } if strings.HasPrefix(config.Url, "mysql") { From e0af9f0453697c56db8cbe0f80b58c5c60c7740e Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Mon, 22 Dec 2025 10:15:53 +0300 Subject: [PATCH 2/3] add migration helper --- sqlr/helpers.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sqlr/helpers.go b/sqlr/helpers.go index 45566a4..697cbbd 100644 --- a/sqlr/helpers.go +++ b/sqlr/helpers.go @@ -148,3 +148,8 @@ func TransactWithCtx(ctx context.Context, fn ...func(tx *gorm.DB) error) error { return err } + +// Migrate ... +func Migrate(models ...any) error { + return instance.gormDB.AutoMigrate(models...) +} From 01a1c8657f0f775614d6596a288d126756f916aa Mon Sep 17 00:00:00 2001 From: Richard Ochom Date: Wed, 24 Dec 2025 07:58:40 +0300 Subject: [PATCH 3/3] add helpers if --- helpers/if.go | 9 ++++++++ helpers/if_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ jsonx/json.go | 20 +++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 helpers/if.go create mode 100644 helpers/if_test.go diff --git a/helpers/if.go b/helpers/if.go new file mode 100644 index 0000000..80f4951 --- /dev/null +++ b/helpers/if.go @@ -0,0 +1,9 @@ +package helpers + +// If returns trueValue if condition is true, otherwise returns falseValue +func If[T any](condition bool, trueValue, falseValue T) T { + if condition { + return trueValue + } + return falseValue +} diff --git a/helpers/if_test.go b/helpers/if_test.go new file mode 100644 index 0000000..19920dc --- /dev/null +++ b/helpers/if_test.go @@ -0,0 +1,54 @@ +package helpers_test + +import ( + "testing" + + "github.com/ochom/gutils/helpers" +) + +func TestIf(t *testing.T) { + tests := []struct { + name string + condition bool + trueValue any + falseValue any + want any + }{ + { + name: "Condition true returns trueValue", + condition: true, + trueValue: "It's true", + falseValue: "It's false", + want: "It's true", + }, + { + name: "Condition false returns falseValue", + condition: false, + trueValue: 100, + falseValue: 200, + want: 200, + }, + { + name: "Condition true with integers", + condition: true, + trueValue: 42, + falseValue: 0, + want: 42, + }, + { + name: "Condition false with booleans", + condition: false, + trueValue: true, + falseValue: false, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := helpers.If(tt.condition, tt.trueValue, tt.falseValue) + if got != tt.want { + t.Errorf("If() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/jsonx/json.go b/jsonx/json.go index 09176a3..7419150 100644 --- a/jsonx/json.go +++ b/jsonx/json.go @@ -32,6 +32,16 @@ func Encode(payload any) byteData { return bytesPayload } +// EncodeErr encodes the given payload into JSON format in byte slice and returns error if any +func EncodeErr(payload any) (byteData, error) { + bytesPayload, err := baseJSON.Marshal(&payload) + if err != nil { + return nil, err + } + + return bytesPayload, nil +} + // Decode decodes JSON payload into the specified type func Decode[T any](payload []byte) T { var data T @@ -42,3 +52,13 @@ func Decode[T any](payload []byte) T { return data } + +// DecodeErr decodes JSON payload into the specified type and returns error if any +func DecodeErr[T any](payload []byte) (T, error) { + var data T + if err := baseJSON.Unmarshal(payload, &data); err != nil { + return data, err + } + + return data, nil +}