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 +} 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") { 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...) +}