Skip to content

Commit 212a35f

Browse files
committed
💚 linting
1 parent 820bda7 commit 212a35f

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

utils/reflection/reflection.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package reflection
66

77
import (
8-
"fmt"
98
"reflect"
109
"unsafe"
1110

@@ -21,9 +20,7 @@ func GetStructureField(field reflect.Value) interface{} {
2120
if !field.IsValid() {
2221
return nil
2322
}
24-
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). //nolint:gosec // this conversion is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
25-
Elem().
26-
Interface()
23+
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface() //nolint:gosec // this conversion is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
2724
}
2825
func SetUnexportedStructureField(structure interface{}, fieldName string, value interface{}) {
2926
SetStructureField(fetchStructureField(structure, fieldName), value)
@@ -32,9 +29,7 @@ func SetStructureField(field reflect.Value, value interface{}) {
3229
if !field.IsValid() {
3330
return
3431
}
35-
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())). //nolint:gosec // this conversion is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
36-
Elem().
37-
Set(reflect.ValueOf(value))
32+
reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Set(reflect.ValueOf(value)) //nolint:gosec // this conversion is between types recommended by Go https://cs.opensource.google/go/go/+/master:src/reflect/value.go;l=2445
3833
}
3934

4035
func fetchStructureField(structure interface{}, fieldName string) reflect.Value {
@@ -62,20 +57,20 @@ func GetStructField(structure interface{}, fieldName string) (interface{}, bool)
6257
}
6358
}
6459

65-
// SetStructField attempts to set a field of a structure to the given vaule
60+
// SetStructField attempts to set a field of a structure to the given value
6661
// It returns nil or an error, in case the field doesn't exist on the structure
6762
// or the value and the field have different types
6863
func SetStructField(structure interface{}, fieldName string, value interface{}) error {
6964
ValueStructure := reflect.ValueOf(structure)
7065
Field := ValueStructure.Elem().FieldByName(fieldName)
7166
// Test field exists on structure
7267
if !Field.IsValid() {
73-
return fmt.Errorf("error with field [%v]: %w", fieldName, commonerrors.ErrInvalid)
68+
return commonerrors.Newf(commonerrors.ErrInvalid, "error with field [%v]", fieldName)
7469
}
7570

7671
// test field is settable
7772
if !Field.CanSet() {
78-
return fmt.Errorf("error with unsettable field [%v]: %w", fieldName, commonerrors.ErrUnsupported)
73+
return commonerrors.Newf(commonerrors.ErrUnsupported, "error with unsettable field [%v]", fieldName)
7974
}
8075

8176
// Helper variables
@@ -101,7 +96,7 @@ func SetStructField(structure interface{}, fieldName string, value interface{})
10196

10297
// Check that the underlying types are the same (e.g. no int and string)
10398
if fieldUnderlyingType != valueUnderlyingType {
104-
return fmt.Errorf("conflicting types, field [%v] and value [%v]: %w", fieldKind, valueKind, commonerrors.ErrConflict)
99+
return commonerrors.Newf(commonerrors.ErrConflict, "conflicting types, field [%v] and value [%v]", fieldKind, valueKind)
105100
}
106101

107102
if fieldKind == reflect.Ptr {
@@ -204,13 +199,13 @@ func IsEmpty(value any) bool {
204199
// ToStructPtr returns an instance of the pointer (interface) to the object obj.
205200
func ToStructPtr(obj reflect.Value) (val interface{}, err error) {
206201
if !obj.IsValid() {
207-
err = fmt.Errorf("%w: obj value [%v] is not valid", commonerrors.ErrUnsupported, obj)
202+
err = commonerrors.Newf(commonerrors.ErrUnsupported, "obj value [%v] is not valid", obj)
208203
return
209204
}
210205

211206
vp := reflect.New(obj.Type())
212207
if !vp.CanInterface() || !obj.CanInterface() {
213-
err = fmt.Errorf("%w: cannot get the value of the object pointer of type %T", commonerrors.ErrUnsupported, obj.Type())
208+
err = commonerrors.Newf(commonerrors.ErrUnsupported, "cannot get the value of the object pointer of type %T", obj.Type())
214209
return
215210
}
216211
vp.Elem().Set(obj)

0 commit comments

Comments
 (0)