From ae7cf745e48094dd9c5bd2bdc5160530994e6087 Mon Sep 17 00:00:00 2001 From: lixxxww <941403820@qq.com> Date: Mon, 22 Jan 2024 08:13:30 +0000 Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91=E9=87=8D=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81:=20=E6=8F=90=E5=8F=96SnakeString=E5=92=8CCamelString?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E7=9B=B8=E5=90=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixxxww <941403820@qq.com> --- pkg/tool/conv.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/tool/conv.go b/pkg/tool/conv.go index d34ddab..bea2823 100644 --- a/pkg/tool/conv.go +++ b/pkg/tool/conv.go @@ -46,7 +46,7 @@ func CamelString(s string) string { return string(data) } -func FirstLowCamelString(s string) string { +func convertString(s string, firstLower bool) string { data := make([]byte, 0, len(s)) flag, num := true, len(s)-1 for i := 0; i <= num; i++ { @@ -62,12 +62,18 @@ func FirstLowCamelString(s string) string { } data = append(data, d) } - if len(data) > 0 && data[0] >= 65 && data[0] <= 90 { + if firstLower && len(data) > 0 && data[0] >= 'A' && data[0] <= 'Z' { data[0] = data[0] + 32 } return string(data) } +// FirstLowCamelString first low camel string, xx_yy to xxYy +func FirstLowCamelString(s string) string { + return convertString(s, true) +} + +// StructToMap convert struct to map func StructToMap(s interface{}) map[string]interface{} { result := make(map[string]interface{}) @@ -83,6 +89,7 @@ func StructToMap(s interface{}) map[string]interface{} { return result } +// MapToStruct convert map to struct func MapToStruct(m map[string]interface{}, s interface{}) error { data, err := json.Marshal(m) if err != nil { @@ -97,6 +104,7 @@ func MapToStruct(m map[string]interface{}, s interface{}) error { return nil } +// InterfaceToStruct convert interface to struct func InterfaceToStruct(m interface{}, s interface{}) error { data, err := json.Marshal(m) if err != nil { @@ -111,6 +119,7 @@ func InterfaceToStruct(m interface{}, s interface{}) error { return nil } +// StringToStruct convert string to struct func StringToStruct(m string, s interface{}) error { err := json.Unmarshal([]byte(m), s) if err != nil { @@ -119,6 +128,7 @@ func StringToStruct(m string, s interface{}) error { return nil } +// TimeToFormat convert time to formatted string func TimeToFormat(val interface{}) string { switch v := val.(type) { case int64: @@ -140,4 +150,4 @@ func TimeToFormat(val interface{}) string { default: return "" } -} +} \ No newline at end of file