Add comments to explain the use of reflection after type assertion.
Removes one local variable, which is used twice only in an error.
This commit is contained in:
+10
-3
@@ -29,6 +29,7 @@ func (dst *EnumArray) Set(src interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to match to select common types:
|
||||
switch value := src.(type) {
|
||||
|
||||
case []string:
|
||||
@@ -82,6 +83,9 @@ func (dst *EnumArray) Set(src interface{}) error {
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Fallback to reflection if an optimised match was not found.
|
||||
// The reflection is necessary for arrays and multidimensional slices,
|
||||
// but it comes with a 20-50% performance penalty for large arrays/slices
|
||||
reflectedValue := reflect.ValueOf(src)
|
||||
if !reflectedValue.IsValid() || reflectedValue.IsZero() {
|
||||
*dst = EnumArray{Status: Null}
|
||||
@@ -187,6 +191,7 @@ func (src *EnumArray) AssignTo(dst interface{}) error {
|
||||
switch src.Status {
|
||||
case Present:
|
||||
if len(src.Dimensions) == 1 {
|
||||
// Attempt to match to select common types:
|
||||
switch v := dst.(type) {
|
||||
|
||||
case *[]string:
|
||||
@@ -210,6 +215,9 @@ func (src *EnumArray) AssignTo(dst interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to reflection if an optimised match was not found.
|
||||
// The reflection is necessary for arrays and multidimensional slices,
|
||||
// but it comes with a 20-50% performance penalty for large arrays/slices
|
||||
value := reflect.ValueOf(dst)
|
||||
if value.Kind() == reflect.Ptr {
|
||||
value = value.Elem()
|
||||
@@ -249,9 +257,8 @@ func (src *EnumArray) assignToRecursive(value reflect.Value, index, dimension in
|
||||
length := int(src.Dimensions[dimension].Length)
|
||||
if reflect.Array == kind {
|
||||
typ := value.Type()
|
||||
typLen := typ.Len()
|
||||
if typLen != length {
|
||||
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typLen)
|
||||
if typ.Len() != length {
|
||||
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
|
||||
}
|
||||
value.Set(reflect.New(typ).Elem())
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user