Skip to content

Commit af00576

Browse files
authored
Merge pull request #61 from NextronSystems/refactor/restore-go1.20-compatibility-by-dropping-typefor
refactor: restore go1.20 compatibility by dropping reflect.TypeFor
2 parents e05db27 + b9ac357 commit af00576

4 files changed

Lines changed: 20 additions & 14 deletions

File tree

jsonpointer/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func findByLabel(base reflect.Value, jsonLabel string) (reflect.Value, bool) {
3333
return reflect.Value{}, false
3434
}
3535

36-
for base.Kind() == reflect.Ptr || base.Kind() == reflect.Interface {
36+
for base.Kind() == reflect.Pointer || base.Kind() == reflect.Interface {
3737
if base.IsNil() {
3838
return reflect.Value{}, false
3939
}

reference.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"golang.org/x/exp/slices"
1111
)
1212

13-
// NewReference creates a new reference to a field of a Object.
13+
// NewReference creates a new reference to a field of an Object.
1414
// The base must be a pointer to a struct implementing Object.
1515
// The field must be a pointer to a field within the base.
1616
func NewReference(base Object, field any) *Reference {
17-
if reflect.ValueOf(base).Kind() != reflect.Ptr {
17+
if reflect.ValueOf(base).Kind() != reflect.Pointer {
1818
panic("Base must be a pointer to a struct implementing Object")
1919
}
20-
if reflect.ValueOf(field).Kind() != reflect.Ptr {
20+
if reflect.ValueOf(field).Kind() != reflect.Pointer {
2121
panic("field must be a pointer to a field within base")
2222
}
2323
return &Reference{
@@ -26,9 +26,9 @@ func NewReference(base Object, field any) *Reference {
2626
}
2727
}
2828

29-
// Reference is a reference to a field of a Object
29+
// Reference is a reference to a field of an Object
3030
type Reference struct {
31-
Base any // Must be a pointer to a Object
31+
Base any // Must be a pointer to an Object
3232
PointedField any // Must be a pointer to a (possibly nested) field of Base
3333

3434
textLabel string
@@ -42,7 +42,7 @@ func (r *Reference) ToJsonPointer() jsonpointer.Pointer {
4242
}
4343
baseValue := reflect.ValueOf(r.Base)
4444
pointedValue := reflect.ValueOf(r.PointedField)
45-
if pointedValue.Kind() != reflect.Ptr {
45+
if pointedValue.Kind() != reflect.Pointer {
4646
panic("PointedField must be a pointer")
4747
}
4848
r.jsonPointer = findRelativeJsonPointer(baseValue, pointedValue)
@@ -52,8 +52,12 @@ func (r *Reference) ToJsonPointer() jsonpointer.Pointer {
5252
return r.jsonPointer
5353
}
5454

55-
var referenceType = reflect.TypeFor[Reference]()
55+
var referenceType = reflect.TypeOf((*Reference)(nil)).Elem()
5656

57+
// findRelativeJsonPointer finds a JSON pointer from base to pointedField. The
58+
// base must be pointer. The pointed field should be a pointer to a field
59+
// within the base; if the pointed field is not found within the base, nil is
60+
// returned.
5761
func findRelativeJsonPointer(base reflect.Value, pointedField reflect.Value) jsonpointer.Pointer {
5862
for {
5963
if base.Equal(pointedField) {
@@ -65,7 +69,7 @@ func findRelativeJsonPointer(base reflect.Value, pointedField reflect.Value) jso
6569
if base.Type() == referenceType { // Don't recurse into other references
6670
return nil
6771
}
68-
if base.Kind() == reflect.Ptr || base.Kind() == reflect.Interface {
72+
if base.Kind() == reflect.Pointer || base.Kind() == reflect.Interface {
6973
if base.IsNil() {
7074
return nil
7175
}
@@ -123,7 +127,7 @@ func (r *Reference) ToTextLabel() string {
123127
}
124128
baseValue := reflect.ValueOf(r.Base).Elem()
125129
pointedValue := reflect.ValueOf(r.PointedField)
126-
if pointedValue.Kind() != reflect.Ptr {
130+
if pointedValue.Kind() != reflect.Pointer {
127131
panic("PointedField must be a pointer")
128132
}
129133
r.textLabel, _ = findTextLabel(baseValue, reflect.ValueOf(r.PointedField))
@@ -143,7 +147,7 @@ func findTextLabel(base reflect.Value, pointedField reflect.Value) (string, bool
143147
if base.Addr().Equal(pointedField) {
144148
return "", true
145149
}
146-
if base.Kind() == reflect.Ptr || base.Kind() == reflect.Interface {
150+
if base.Kind() == reflect.Pointer || base.Kind() == reflect.Interface {
147151
base = base.Elem()
148152
}
149153
if base.Kind() != reflect.Struct {
@@ -156,7 +160,7 @@ func findTextLabel(base reflect.Value, pointedField reflect.Value) (string, bool
156160
continue
157161
}
158162
var fieldPointer = field
159-
if field.Kind() != reflect.Ptr {
163+
if field.Kind() != reflect.Pointer {
160164
fieldPointer = field.Addr()
161165
}
162166
var label string

reference_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func TestReference_ToJsonPointer(t *testing.T) {
111111
{&test.Valuer.Subfield7, "/valuer/subfield7"},
112112
{&test.SubObject, "/subobject"},
113113
{&test.SubObject.Subfield8, "/subobject/subfield8"},
114+
// test.Recursive not required here: if there is a flaw in the pointer
115+
// search logic, it will panic when encountering this cycle.
114116
}
115117
for _, tt := range tests {
116118
t.Run(tt.want, func(t *testing.T) {

textlog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func withUppercaseKeys(entry TextlogEntry) TextlogEntry {
8686
}
8787

8888
func (t TextlogFormatter) toEntry(object reflect.Value) TextlogEntry {
89-
for object.Kind() == reflect.Ptr || object.Kind() == reflect.Interface {
89+
for object.Kind() == reflect.Pointer || object.Kind() == reflect.Interface {
9090
if object.IsNil() {
9191
return nil
9292
}
@@ -138,7 +138,7 @@ func (t TextlogFormatter) toEntry(object reflect.Value) TextlogEntry {
138138
} else {
139139
// Add the field as a single value
140140
key := logfield
141-
if field.Kind() == reflect.Ptr {
141+
if field.Kind() == reflect.Pointer {
142142
field = field.Elem()
143143
}
144144
details = append(details, TextlogValuePair{

0 commit comments

Comments
 (0)