-
Notifications
You must be signed in to change notification settings - Fork 0
/
caster.go
38 lines (32 loc) · 882 Bytes
/
caster.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
package qkit
import (
"encoding/json"
)
// Converts any type to a given type. If conversion fails, it returns the zero value of the given type.
func Cast[T any](val any) T {
if val, ok := val.(T); ok {
return val
}
var zero T
return zero
}
// Converts any type to a given type based on their json representation. It partially fills the target in case they are not directly compatible.
func CastJSON[T any](val any) T {
return FromJSON[T](ToJSON(val))
}
// Converts a given value to a byte array.
func ToJSON(val any) []byte {
return Ok(json.Marshal(val))
}
// Converts a byte array to a given type.
func FromJSON[T any](bytes []byte) T {
var v T
json.Unmarshal(bytes, &v)
return v
}
// Converts any type to a map[string]interface{}.
func ToMap(s any) map[string]interface{} {
m := make(map[string]interface{})
json.Unmarshal(Ok(json.Marshal(s)), &m)
return m
}