Skip to content
Snippets Groups Projects
Select Git revision
  • 8190f92a5f6eb416f138574dc3c42f15dcc54d4c
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

reflection.go

Blame
  • 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()