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
@@ -32,6 +32,7 @@ func (dst *MacaddrArray) Set(src interface{}) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to match to select common types:
|
||||
switch value := src.(type) {
|
||||
|
||||
case []net.HardwareAddr:
|
||||
@@ -85,6 +86,9 @@ func (dst *MacaddrArray) 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 = MacaddrArray{Status: Null}
|
||||
@@ -190,6 +194,7 @@ func (src *MacaddrArray) 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 *[]net.HardwareAddr:
|
||||
@@ -213,6 +218,9 @@ func (src *MacaddrArray) 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()
|
||||
@@ -252,9 +260,8 @@ func (src *MacaddrArray) assignToRecursive(value reflect.Value, index, dimension
|
||||
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