Skip to content

Commit fcf255c

Browse files
committed
struct info debug
1 parent de86170 commit fcf255c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

struct_info.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package instruct
22

33
import (
4+
"errors"
45
"fmt"
6+
"io"
57
"reflect"
68
"strings"
79
)
@@ -45,6 +47,35 @@ func (s *structInfo) fieldByName(name string) *structInfo {
4547
return nil
4648
}
4749

50+
func (s *structInfo) dump(w io.Writer) error {
51+
return s.dumpIndent("", w)
52+
}
53+
54+
func (s *structInfo) dumpIndent(indent string, w io.Writer) error {
55+
var ferr error
56+
var err error
57+
_, err = fmt.Fprintf(w, "%s- ", indent)
58+
ferr = errors.Join(ferr, err)
59+
name := "{ROOT}"
60+
if s.field.Type != nil {
61+
name = s.field.Name
62+
}
63+
_, err = fmt.Fprintf(w, "%s [/%s] (type: %s, kind: %s) ", name, strings.Join(s.path, "/"), s.typ.Name(), s.typ.Kind().String())
64+
ferr = errors.Join(ferr, err)
65+
if s.field.Type != nil {
66+
_, err = fmt.Fprintf(w, "[field type: %s, kind: %s] ", s.field.Type.String(), s.field.Type.Kind().String())
67+
ferr = errors.Join(ferr, err)
68+
// _, err = fmt.Fprintf(w, "[path: '%s']", strings.Join(s.path, "/"))
69+
// ferr = errors.Join(ferr, err)
70+
}
71+
_, err = fmt.Fprintf(w, "\n")
72+
ferr = errors.Join(ferr, err)
73+
for _, field := range s.fields {
74+
ferr = errors.Join(ferr, field.dumpIndent(indent+"\t", w))
75+
}
76+
return err
77+
}
78+
4879
// structInfoWithMapTags overrides a structInfo with a MapTags. This creates a clone of all the objects and don't
4980
// change the original in any way.
5081
func structInfoWithMapTags[IT any, DC DecodeContext](si *structInfo, mapTags MapTags, options DefaultOptions[IT, DC]) (*structInfo, error) {

0 commit comments

Comments
 (0)