Skip to content
Open
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
9 changes: 9 additions & 0 deletions helpers/if.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 54 additions & 0 deletions helpers/if_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
20 changes: 20 additions & 0 deletions jsonx/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
5 changes: 4 additions & 1 deletion sqlr/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
5 changes: 5 additions & 0 deletions sqlr/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Loading