laniakea/utils.go
2024-12-17 09:23:53 +03:00

36 lines
695 B
Go

package laniakea
import "encoding/json"
func MapToStruct(m map[string]interface{}, s interface{}) error {
data, err := json.Marshal(m)
if err != nil {
return err
}
err = json.Unmarshal(data, s)
return err
}
func MapToJson(m map[string]interface{}) (string, error) {
data, err := json.Marshal(m)
return string(data), err
}
func StructToMap(s interface{}) (map[string]interface{}, error) {
data, err := json.Marshal(s)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
err = json.Unmarshal(data, &m)
return m, err
}
func Map[T, V any](ts []T, fn func(T) V) []V {
result := make([]V, len(ts))
for i, t := range ts {
result[i] = fn(t)
}
return result
}