nil check
This commit is contained in:
parent
b00034d8ca
commit
d26ecc663a
@ -130,6 +130,10 @@ func FunctionParams(v reflect.Value) []string {
|
|||||||
|
|
||||||
// Type returns the docuemntation for a package level type.
|
// Type returns the docuemntation for a package level type.
|
||||||
func Type(t reflect.Type) string {
|
func Type(t reflect.Type) string {
|
||||||
|
if t == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
t = unpack(t)
|
t = unpack(t)
|
||||||
p := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
|
p := fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
|
||||||
return Docs(p)
|
return Docs(p)
|
||||||
@ -141,6 +145,29 @@ func structField(visited map[reflect.Type]bool, t reflect.Type, fieldPath []stri
|
|||||||
return nil, "", false
|
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++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
f := t.Field(i)
|
f := t.Field(i)
|
||||||
if !f.Anonymous {
|
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
|
return nil, "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Field returns the docuemntation for a struct field.
|
// Field returns the docuemntation for a struct field.
|
||||||
func Field(t reflect.Type, fieldPath ...string) string {
|
func Field(t reflect.Type, fieldPath ...string) string {
|
||||||
|
if t == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
if len(fieldPath) == 0 {
|
if len(fieldPath) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -204,6 +212,10 @@ func Field(t reflect.Type, fieldPath ...string) string {
|
|||||||
|
|
||||||
// Method returns the documentation for a type method.
|
// Method returns the documentation for a type method.
|
||||||
func Method(t reflect.Type, name string) string {
|
func Method(t reflect.Type, name string) string {
|
||||||
|
if t == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
t = unpack(t)
|
t = unpack(t)
|
||||||
if t.Kind() != reflect.Struct {
|
if t.Kind() != reflect.Struct {
|
||||||
return ""
|
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.
|
// MethodParams returns the list of the parameter names of a type method.
|
||||||
func MethodParams(t reflect.Type, name string) []string {
|
func MethodParams(t reflect.Type, name string) []string {
|
||||||
t = unpack(t)
|
if t == nil {
|
||||||
if t.Kind() != reflect.Struct {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t = unpack(t)
|
||||||
p := fmt.Sprintf("%s.%s.%s", t.PkgPath(), t.Name(), name)
|
p := fmt.Sprintf("%s.%s.%s", t.PkgPath(), t.Name(), name)
|
||||||
d := docs(p)
|
d := docs(p)
|
||||||
return functionParams(d)
|
return functionParams(d)
|
Loading…
Reference in New Issue
Block a user