Skip to content

Commit

Permalink
Merge pull request #142 from tinh-tinh/feat/ren/141-change-transform-…
Browse files Browse the repository at this point in the history
…function

feat: remove useless transform functions and create tostring funtion
  • Loading branch information
Ren0503 authored Dec 4, 2024
2 parents bf9f9b7 + 5ce74b7 commit fdfeb3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
48 changes: 21 additions & 27 deletions dto/transform/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import (
"time"
)

func StringToBool(str string) bool {
val, _ := strconv.ParseBool(str)
return val
}

func ToBool(str interface{}) bool {
switch v := str.(type) {
case bool:
Expand All @@ -19,7 +14,7 @@ func ToBool(str interface{}) bool {
val, _ := strconv.ParseBool(str.(string))
return val
default:
panic(fmt.Sprintf("cannot transform bool with type %v", v))
panic(fmt.Sprintf("cannot transform bool with type %v, currently only support bool, string", v))
}
}

Expand All @@ -31,20 +26,10 @@ func ToInt(str interface{}) interface{} {
val, _ := strconv.Atoi(str.(string))
return val
default:
panic(fmt.Sprintf("cannot transform int with type %v", v))
panic(fmt.Sprintf("cannot transform int with type %v, currently only support int, string", v))
}
}

func StringToInt64(str string) int64 {
val, _ := strconv.ParseInt(str, 10, 64)
return val
}

func StringToInt(str string) int {
val, _ := strconv.Atoi(str)
return val
}

func ToFloat(str interface{}) interface{} {
switch v := str.(type) {
case float32:
Expand All @@ -57,7 +42,7 @@ func ToFloat(str interface{}) interface{} {
val, _ := strconv.ParseFloat(str.(string), 64)
return val
default:
panic(fmt.Sprintf("cannot transform with type %v", v))
panic(fmt.Sprintf("cannot transform with type %v, currently only support float, int, string", v))
}
}

Expand All @@ -69,16 +54,25 @@ func ToDate(str interface{}) time.Time {
date, _ := time.Parse("2006-01-02", str.(string))
return date
default:
panic(fmt.Sprintf("cannot transform with type %v", v))
panic(fmt.Sprintf("cannot transform with type %v, currently only support time, string", v))
}
}

func StringToDate(str string) time.Time {
date, _ := time.Parse("2006-01-02", str)
return date
}

func StringToTimeDuration(str string) time.Duration {
val, _ := time.ParseDuration(str)
return val
func ToString(str interface{}) string {
switch v := str.(type) {
case string:
return str.(string)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return strconv.Itoa(int(str.(int)))
case float32:
return strconv.FormatFloat(float64(str.(float32)), 'f', -1, 32)
case float64:
return strconv.FormatFloat(str.(float64), 'f', -1, 64)
case bool:
return strconv.FormatBool(str.(bool))
case time.Time:
return str.(time.Time).String()
default:
panic(fmt.Sprintf("cannot transform with type %v, currently only support string, int, float, bool, time", v))
}
}
30 changes: 12 additions & 18 deletions dto/transform/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,16 @@ func Test_ToDate(t *testing.T) {
require.Panics(t, func() { transform.ToDate(true) })
}

func Test_StringToDate(t *testing.T) {
require.Equal(t, time.Date(2024, 9, 27, 0, 0, 0, 0, time.UTC), transform.StringToDate("2024-09-27"))
}

func Test_StringToBool(t *testing.T) {
require.True(t, transform.StringToBool("true"))
require.False(t, transform.StringToBool("false"))
}

func Test_StringToInt(t *testing.T) {
require.Equal(t, int64(123), transform.StringToInt64("123"))
require.Equal(t, int(123), transform.StringToInt("123"))
}

func Test_StringToTimeDuration(t *testing.T) {
require.Equal(t, time.Second, transform.StringToTimeDuration("1s"))
require.Equal(t, time.Minute, transform.StringToTimeDuration("1m"))
require.Equal(t, time.Hour, transform.StringToTimeDuration("1h"))
func Test_ToString(t *testing.T) {
require.Equal(t, "true", transform.ToString(true))
require.Equal(t, "false", transform.ToString(false))
require.Equal(t, "123", transform.ToString(123))
require.Equal(t, "0.45", transform.ToString(0.45))
require.Equal(t, "0.1", transform.ToString(float32(0.1)))
require.Equal(t, "2024-01-01", transform.ToString("2024-01-01"))

current := time.Now()
require.Equal(t, current.String(), transform.ToString(current))

require.Panics(t, func() { transform.ToString(make(map[string]interface{})) })
}

0 comments on commit fdfeb3f

Please sign in to comment.