-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConv.go
55 lines (47 loc) · 1.28 KB
/
Conv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gohelper
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
// ToString converts the given data to a string representation.
//
// data: the data to be converted.
// string: the string representation of the data.
func ToString(data any) string {
return fmt.Sprintf("%v", data)
}
// ToJson converts the given data to a JSON string representation.
//
// data: the data to be converted.
// string: the JSON string representation of the data.
// error: an error if the conversion fails.
func ToJson(data any) (string, error) {
res, err := json.Marshal(data)
if err != nil {
res = []byte("")
}
return string(res), nil
}
// StructToMap converts a struct to a map[string]T, where T is the type of the struct fields.
// This function is optimized for performance and memory allocation.
//
// Parameter:
// data - the input struct (of type struct{...} with fields of type T)
// Return type:
// map[string]T - a map containing the struct fields
func StructToMap[T any](data any) map[string]T {
t := reflect.TypeOf(data)
v := reflect.ValueOf(data)
r := make(map[string]T, t.NumField())
for i := 0; i < t.NumField(); i++ {
fieldInfo := t.Field(i)
k := fieldInfo.Tag.Get("json")
if k == "" {
k = fieldInfo.Name
}
r[strings.ToLower(k)] = v.Field(i).Interface().(T)
}
return r
}