sendmail/utils.go

53 lines
1 KiB
Go

package main
import "reflect"
func IsPresent(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
func IsEmpty(value interface{}) bool {
t := reflect.TypeOf(value)
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice:
return v.Len() == 0
default:
return value == reflect.Zero(t).Interface()
}
}
func Merge(key string, value, obj interface{}) {
if IsEmpty(value) {
return
}
field := reflect.ValueOf(obj).Elem().FieldByName(key)
if field.CanSet() {
switch field.Kind() {
case reflect.Int:
field.Set(reflect.ValueOf(value))
case reflect.Int16:
field.Set(reflect.ValueOf(value))
case reflect.Int32:
field.Set(reflect.ValueOf(value))
case reflect.Int64:
field.SetInt(value.(int64))
case reflect.Bool:
field.SetBool(value.(bool))
case reflect.String:
field.SetString(value.(string))
case reflect.Slice:
field.Set(reflect.ValueOf(value))
case reflect.Ptr:
field.Set(reflect.ValueOf(value))
default:
Merge(key, value, field)
}
}
}