Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slice and array annotation to struct fields #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type formatter struct {
// breaks and tabs. Object f responds to the "%v" formatting verb when both the
// "#" and " " (space) flags are set, for example:
//
// fmt.Sprintf("%# v", Formatter(x))
// fmt.Sprintf("%# v", Formatter(x))
//
// If one of these two flags is not set, or any other verb is used, f will
// format x according to the usual rules of package fmt.
Expand Down Expand Up @@ -329,7 +329,7 @@ func canExpand(t reflect.Type) bool {

func labelType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Interface, reflect.Struct:
case reflect.Interface, reflect.Struct, reflect.Array, reflect.Slice:
return true
}
return false
Expand Down
24 changes: 24 additions & 0 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,27 @@ func TestCycle(t *testing.T) {
*iv = *i
t.Logf("Example long interface cycle:\n%# v", Formatter(i))
}

func TestSliceField(t *testing.T) {
type MyField struct {
Name string
}
type MyObj struct {
Name string
Fields []MyField
}

o := MyObj{
Name: "xxx",
Fields: []MyField{
{
Name: "fff",
},
},
}

s := fmt.Sprintf("%# v", Formatter(o))
if strings.Contains(s, "[]MyField") {
t.Error("Slice field missing in struct")
}
}