Skip to content

Commit 2d9b797

Browse files
committed
💚 linting
1 parent 820bda7 commit 2d9b797

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
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)

utils/reflection/reflection_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stretchr/testify/require"
1616

1717
"github.com/ARM-software/golang-utils/utils/commonerrors"
18+
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
1819
"github.com/ARM-software/golang-utils/utils/field"
1920
)
2021

@@ -245,7 +246,7 @@ func TestSetStructField_InvalidField(t *testing.T) {
245246
err := SetStructField(&testStructure, "Title", "NEW_title")
246247

247248
assert.NotNil(t, err)
248-
assert.Equal(t, err, fmt.Errorf("error with field [%v]: %w", "Title", commonerrors.ErrInvalid))
249+
errortest.AssertError(t, err, commonerrors.ErrInvalid)
249250
}
250251

251252
func TestSetStructField_UnsettableField(t *testing.T) {
@@ -259,7 +260,7 @@ func TestSetStructField_UnsettableField(t *testing.T) {
259260
err := SetStructField(&testStructure, "unexported", "NEW_title")
260261

261262
assert.NotNil(t, err)
262-
assert.Equal(t, err, fmt.Errorf("error with unsettable field [%v]: %w", "unexported", commonerrors.ErrUnsupported))
263+
errortest.AssertError(t, err, commonerrors.ErrUnsupported)
263264
assert.NotEqual(t, testStructure.unexported, "NEW_title")
264265
assert.Equal(t, testStructure.unexported, "unsettable_field")
265266
}
@@ -277,7 +278,7 @@ func TestSetStructField_FieldAndValueDifferentTypes(t *testing.T) {
277278
err := SetStructField(&testStructure, "Title", 133)
278279

279280
assert.NotNil(t, err)
280-
assert.Equal(t, err, fmt.Errorf("conflicting types, field [%v] and value [%v]: %w", reflect.ValueOf(testStructure).FieldByName("Title").Type().Kind(), reflect.TypeOf(123), commonerrors.ErrConflict))
281+
errortest.AssertError(t, err, commonerrors.ErrConflict)
281282
}
282283

283284
func TestInheritsFrom(t *testing.T) {

0 commit comments

Comments
 (0)