Select Git revision
date_utils.go
reflection.go 1.74 KiB
package reflection
import (
"reflect"
"time"
"gitlab.com/uafrica/go-utils/logger"
)
func SetPointerTime(field reflect.Value, value *time.Time) {
if !isFieldValid(field, false) {
return // Field doesn't exist
}
if field.Kind() != reflect.Ptr {
logger.Error("Field need to be *Field")
return
}
field.Set(reflect.ValueOf(value))
}
func SetInt64(field reflect.Value, value int64) {
if !isFieldValid(field, false) {
return // Field doesn't exist
}
if field.Kind() != reflect.Int64 {
logger.Error("Claims: Field is not of type Int64")
return
}
if field.OverflowInt(value) {
logger.Error("Claims: Int overflow")
return
}
field.SetInt(value)
}
func SetPointerInt64(field reflect.Value, value *int64) {
if !isFieldValid(field, false) {
return // Field doesn't exist
}
if field.Kind() != reflect.Ptr {
logger.Error("Field need to be *Int64")
return
}
field.Set(reflect.ValueOf(value))
}
func SetString(field reflect.Value, value string) {
if !isFieldValid(field, false) {
return // Field doesn't exist
}
if field.Kind() != reflect.String {
logger.Error("Claims: Field is not of type String: %v", field.Kind())
return
}
field.SetString(value)
}
func GetStringValue(field reflect.Value) string {
if !isFieldValid(field, true) {
return ""
}
if field.Kind() == reflect.String {
return field.String()
}
if field.Kind() == reflect.Ptr && !field.IsNil() {
return field.Elem().String()