-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.go
87 lines (77 loc) · 2.25 KB
/
convert.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package populate_struct
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
)
const (
SPLIT_CHAR = "."
ClearEscapePath = ""
)
var (
ErrParameterNotAccessToSet = errors.New("error Parameter Not Access To Set")
ErrNotHaveAccessKeys = errors.New("error Not Have Access Keys")
ErrFieldNotFound = errors.New("error field not found")
)
// Helper function to convert value types
func convertStringToType(val string, targetType reflect.Type) (reflect.Value, error) {
switch targetType.Kind() {
case reflect.String:
return reflect.ValueOf(val), nil
case reflect.Int:
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
return reflect.Value{}, fmt.Errorf("cannot convert %s to int: %v", val, err)
}
return reflect.ValueOf(int(floatVal)), nil
case reflect.Int32:
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
return reflect.Value{}, fmt.Errorf("cannot convert %s to int32: %v", val, err)
}
return reflect.ValueOf(int32(floatVal)), nil
case reflect.Float64:
floatVal, err := strconv.ParseFloat(val, 64)
if err != nil {
return reflect.Value{}, fmt.Errorf("cannot convert %s to float64: %v", val, err)
}
return reflect.ValueOf(floatVal), nil
case reflect.Bool:
boolVal, err := strconv.ParseBool(val)
if err != nil {
return reflect.Value{}, fmt.Errorf("cannot convert %s to bool: %v", val, err)
}
return reflect.ValueOf(boolVal), nil
case reflect.Slice:
// Assume comma-separated values for slice of strings
jsonSlice := make([]string, 0)
if err := json.Unmarshal([]byte(val), &jsonSlice); err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(jsonSlice), nil
// Add more cases as needed
default:
return reflect.Value{}, fmt.Errorf("unsupported type: %s", targetType.Kind())
}
}
func JsonReplaceInterface(s, d any) error {
if bytes, err := json.Marshal(s); err != nil {
return err
} else if err = json.Unmarshal(bytes, d); err != nil {
return err
}
return nil
}
func ConvertStructToMapStringAny(obj any) (map[string]any, error) {
jsonObj, err := json.Marshal(obj)
if err != nil {
return nil, err
}
mapStrToAny := make(map[string]any, 0)
if err := json.Unmarshal(jsonObj, &mapStrToAny); err != nil {
return nil, err
}
return mapStrToAny, nil
}