Skip to content

Commit

Permalink
fix remaining panics with new reflectx.Underlying etc structure of re…
Browse files Browse the repository at this point in the history
…turning nil pointer instead of invalid; seems to be all working now
  • Loading branch information
kkoreilly committed Nov 24, 2024
1 parent b64d0a8 commit 5a6fe80
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion base/reflectx/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,12 @@ func ToFloat32(v any) (float32, error) {
// pointers, and byte is converted as string(byte), not the decimal representation.
func ToString(v any) string {
nilstr := "nil"
// TODO: this reflection is unideal for performance, but we need it to prevent panics,
// so this whole "greatest efficiency" type switch is kind of pointless.
rv := reflect.ValueOf(v)
if IsNil(rv) {
return nilstr
}
switch vt := v.(type) {
case string:
return vt
Expand Down Expand Up @@ -832,7 +838,7 @@ func ToString(v any) string {
}

// then fall back on reflection
uv := Underlying(reflect.ValueOf(v))
uv := Underlying(rv)
if IsNil(uv) {
return nilstr
}
Expand Down
2 changes: 1 addition & 1 deletion core/valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func toValue(value any, tags reflect.StructTag) Value {
return NewText()
}
uv := reflectx.Underlying(rv)
if !uv.IsValid() {
if reflectx.IsNil(uv) {
return NewText()
}
typ := uv.Type()
Expand Down

0 comments on commit 5a6fe80

Please sign in to comment.