From d26ecc663a30e34389b41140d1ab9a0aaa1f4f9f Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Sun, 31 Aug 2025 20:34:00 +0200 Subject: [PATCH] nil check --- generate/{generate.go => lib.go} | 0 generate/{generate_test.go => lib_test.go} | 0 docs.go => lib.go | 62 +++++++++++++--------- docs_test.go => lib_test.go | 0 4 files changed, 37 insertions(+), 25 deletions(-) rename generate/{generate.go => lib.go} (100%) rename generate/{generate_test.go => lib_test.go} (100%) rename docs.go => lib.go (97%) rename docs_test.go => lib_test.go (100%) diff --git a/generate/generate.go b/generate/lib.go similarity index 100% rename from generate/generate.go rename to generate/lib.go diff --git a/generate/generate_test.go b/generate/lib_test.go similarity index 100% rename from generate/generate_test.go rename to generate/lib_test.go diff --git a/docs.go b/lib.go similarity index 97% rename from docs.go rename to lib.go index d81f553..4d8c9cb 100644 --- a/docs.go +++ b/lib.go @@ -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) diff --git a/docs_test.go b/lib_test.go similarity index 100% rename from docs_test.go rename to lib_test.go