1
0

nil check

This commit is contained in:
Arpad Ryszka 2025-08-31 20:34:00 +02:00
parent b00034d8ca
commit d26ecc663a
4 changed files with 37 additions and 25 deletions

View File

@ -130,6 +130,10 @@ func FunctionParams(v reflect.Value) []string {
// Type returns the docuemntation for a package level type.
func Type(t reflect.Type) string {
if t == nil {
return ""
}
t = unpack(t)
p := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
return Docs(p)
@ -141,6 +145,29 @@ func structField(visited map[reflect.Type]bool, t reflect.Type, fieldPath []stri
return nil, "", false
}
name := fieldPath[0]
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Anonymous || f.Name != name {
continue
}
if len(fieldPath) == 1 {
return t, name, true
}
tt, s, ok := structField(nil, unpack(f.Type), fieldPath[1:])
if !ok {
return nil, "", false
}
if tt.Name() == "" {
return t, fmt.Sprintf("%s.%s", name, s), true
}
return tt, s, true
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if !f.Anonymous {
@ -162,34 +189,15 @@ func structField(visited map[reflect.Type]bool, t reflect.Type, fieldPath []stri
}
}
name := fieldPath[0]
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Name != name {
continue
}
if len(fieldPath) == 1 {
return t, name, true
}
tt, s, ok := structField(nil, unpack(f.Type), fieldPath[1:])
if !ok {
return nil, "", false
}
if tt.Name() == "" {
return t, fmt.Sprintf("%s.%s", name, s), true
}
return tt, s, true
}
return nil, "", false
}
// Field returns the docuemntation for a struct field.
func Field(t reflect.Type, fieldPath ...string) string {
if t == nil {
return ""
}
if len(fieldPath) == 0 {
return ""
}
@ -204,6 +212,10 @@ func Field(t reflect.Type, fieldPath ...string) string {
// Method returns the documentation for a type method.
func Method(t reflect.Type, name string) string {
if t == nil {
return ""
}
t = unpack(t)
if t.Kind() != reflect.Struct {
return ""
@ -215,11 +227,11 @@ func Method(t reflect.Type, name string) string {
// MethodParams returns the list of the parameter names of a type method.
func MethodParams(t reflect.Type, name string) []string {
t = unpack(t)
if t.Kind() != reflect.Struct {
if t == nil {
return nil
}
t = unpack(t)
p := fmt.Sprintf("%s.%s.%s", t.PkgPath(), t.Name(), name)
d := docs(p)
return functionParams(d)