Skip to content

Commit 436dae7

Browse files
⚙️ Run go fix against utils/reflection (#863)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Run go fix against utils/reflection ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 2241808 commit 436dae7

6 files changed

Lines changed: 23 additions & 22 deletions

File tree

changes/20260507133219.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:gear: Run go fix against utils/reflection

utils/http/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func genericCallAndCheckSuccessAndReturnRawResponse[T any](ctx context.Context,
182182
return
183183
}
184184

185-
if reflect.ValueOf(result).Kind() != reflect.Ptr {
185+
if reflect.ValueOf(result).Kind() != reflect.Pointer {
186186
err = commonerrors.Newf(commonerrors.ErrConflict, "result of the call is of type [%T] and so, not a pointer as expected", result)
187187
return
188188
}

utils/maps/flatten.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func flatten(result map[string]string, prefix string, v reflect.Value) (err erro
8585
result[prefix] = v.String()
8686
case reflect.Invalid:
8787
result[prefix] = ""
88-
case reflect.Ptr:
88+
case reflect.Pointer:
8989
if v.IsNil() {
9090
return
9191
}

utils/reflection/reflection.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ import (
1212
valueUtils "github.com/ARM-software/golang-utils/utils/value"
1313
)
1414

15-
func GetUnexportedStructureField(structure interface{}, fieldName string) interface{} {
15+
func GetUnexportedStructureField(structure any, fieldName string) any {
1616
return GetStructureField(fetchStructureField(structure, fieldName))
1717
}
1818

19-
func GetStructureField(field reflect.Value) interface{} {
19+
func GetStructureField(field reflect.Value) any {
2020
if !field.IsValid() {
2121
return nil
2222
}
2323
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
2424
}
25-
func SetUnexportedStructureField(structure interface{}, fieldName string, value interface{}) {
25+
func SetUnexportedStructureField(structure any, fieldName string, value any) {
2626
SetStructureField(fetchStructureField(structure, fieldName), value)
2727
}
28-
func SetStructureField(field reflect.Value, value interface{}) {
28+
func SetStructureField(field reflect.Value, value any) {
2929
if !field.IsValid() {
3030
return
3131
}
3232
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
3333
}
3434

35-
func fetchStructureField(structure interface{}, fieldName string) reflect.Value {
35+
func fetchStructureField(structure any, fieldName string) reflect.Value {
3636
return reflect.ValueOf(structure).Elem().FieldByName(fieldName)
3737
}
3838

@@ -41,13 +41,13 @@ func fetchStructureField(structure interface{}, fieldName string) reflect.Value
4141
// If the boolean is false then there is no such field on the structure.
4242
// If the boolean is true but the interface stores "" then the field exists but is not set.
4343
// If the boolean is true and the interface is not empty, the field exists and is set.
44-
func GetStructField(structure interface{}, fieldName string) (interface{}, bool) {
44+
func GetStructField(structure any, fieldName string) (any, bool) {
4545
Field := fetchStructureField(structure, fieldName)
4646
if !Field.IsValid() {
4747
return "", false
4848
}
4949

50-
if Field.Type().Kind() == reflect.Ptr {
50+
if Field.Type().Kind() == reflect.Pointer {
5151
if Field.IsNil() {
5252
return "", true
5353
}
@@ -60,7 +60,7 @@ func GetStructField(structure interface{}, fieldName string) (interface{}, bool)
6060
// SetStructField attempts to set a field of a structure to the given value
6161
// It returns nil or an error, in case the field doesn't exist on the structure
6262
// or the value and the field have different types
63-
func SetStructField(structure interface{}, fieldName string, value interface{}) error {
63+
func SetStructField(structure any, fieldName string, value any) error {
6464
ValueStructure := reflect.ValueOf(structure)
6565
Field := ValueStructure.Elem().FieldByName(fieldName)
6666
// Test field exists on structure
@@ -86,11 +86,11 @@ func SetStructField(structure interface{}, fieldName string, value interface{})
8686

8787
// helpers for determining whether the field and the value have the same underlying types
8888
valueUnderlyingType := reflect.TypeOf(value)
89-
if valueKind == reflect.Ptr {
89+
if valueKind == reflect.Pointer {
9090
valueUnderlyingType = valueUnderlyingType.Elem()
9191
}
9292
fieldUnderlyingType := Field.Type()
93-
if fieldKind == reflect.Ptr {
93+
if fieldKind == reflect.Pointer {
9494
fieldUnderlyingType = fieldUnderlyingType.Elem()
9595
}
9696

@@ -99,8 +99,8 @@ func SetStructField(structure interface{}, fieldName string, value interface{})
9999
return commonerrors.Newf(commonerrors.ErrConflict, "conflicting types, field [%v] and value [%v]", fieldKind, valueKind)
100100
}
101101

102-
if fieldKind == reflect.Ptr {
103-
if valueKind != reflect.Ptr { // value not ptr, field ptr
102+
if fieldKind == reflect.Pointer {
103+
if valueKind != reflect.Pointer { // value not ptr, field ptr
104104
if Field.IsNil() {
105105
pointerToValue := reflect.New(valueReflectValueWrapper.Type())
106106
pointerToValue.Elem().Set(valueReflectValueWrapper)
@@ -110,7 +110,7 @@ func SetStructField(structure interface{}, fieldName string, value interface{})
110110
}
111111
}
112112
} else { // field not ptr, val ptr
113-
if valueKind == reflect.Ptr {
113+
if valueKind == reflect.Pointer {
114114
Field.Set(valueReflectValueWrapper.Elem())
115115
}
116116
}
@@ -120,7 +120,7 @@ func SetStructField(structure interface{}, fieldName string, value interface{})
120120

121121
// InheritsFrom uses reflection to find if a struct "inherits" from a certain type.
122122
// In other words it checks whether the struct embeds a struct of that type.
123-
func InheritsFrom(object interface{}, parentType reflect.Type) bool {
123+
func InheritsFrom(object any, parentType reflect.Type) bool {
124124
if parentType == nil {
125125
return object == nil
126126
}
@@ -131,7 +131,7 @@ func InheritsFrom(object interface{}, parentType reflect.Type) bool {
131131
return true
132132
}
133133

134-
if r.Kind() == reflect.Ptr {
134+
if r.Kind() == reflect.Pointer {
135135
if r.IsNil() {
136136
return false
137137
}
@@ -154,7 +154,7 @@ func InheritsFrom(object interface{}, parentType reflect.Type) bool {
154154
)
155155
kind := parentType.Kind()
156156
switch kind {
157-
case reflect.Ptr:
157+
case reflect.Pointer:
158158
pointerType = parentType
159159
structType = parentType.Elem()
160160
case reflect.Interface:
@@ -197,7 +197,7 @@ func IsEmpty(value any) bool {
197197
}
198198

199199
// ToStructPtr returns an instance of the pointer (interface) to the object obj.
200-
func ToStructPtr(obj reflect.Value) (val interface{}, err error) {
200+
func ToStructPtr(obj reflect.Value) (val any, err error) {
201201
if !obj.IsValid() {
202202
err = commonerrors.Newf(commonerrors.ErrUnsupported, "obj value [%v] is not valid", obj)
203203
return

utils/serialization/maps/map.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func ToMapFromPointer[T any](o T) (m map[string]string, err error) {
1919
err = commonerrors.UndefinedVariable("pointer")
2020
return
2121
}
22-
if reflect.TypeOf(o).Kind() != reflect.Ptr {
22+
if reflect.TypeOf(o).Kind() != reflect.Pointer {
2323
err = commonerrors.Newf(commonerrors.ErrInvalid, "expected a pointer and got %T", o)
2424
return
2525
}
@@ -56,7 +56,7 @@ func FromMapToPointer[T any](m map[string]string, o T) (err error) {
5656
err = commonerrors.UndefinedVariable("pointer")
5757
return
5858
}
59-
if reflect.TypeOf(o).Kind() != reflect.Ptr {
59+
if reflect.TypeOf(o).Kind() != reflect.Pointer {
6060
err = commonerrors.Newf(commonerrors.ErrInvalid, "expected a pointer and got %T", o)
6161
return
6262
}

utils/value/empty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func IsEmpty(value any) bool {
2828
switch objValue.Kind() {
2929
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice:
3030
return objValue.Len() == 0
31-
case reflect.Ptr:
31+
case reflect.Pointer:
3232
if objValue.IsNil() {
3333
return true
3434
}

0 commit comments

Comments
 (0)