Skip to content

Commit

Permalink
gracefully handle anonymous structs
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicbarnes committed Oct 25, 2024
1 parent 8f44bb2 commit 745cbb8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
24 changes: 17 additions & 7 deletions testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func assert(log *logger, dir string, values ...any) error {
}

if !cmp.Equal(expected, actual) {
return fmt.Errorf("test of %T failed: %s", expected, cmp.Diff(expected, actual))
return fmt.Errorf("test of %s failed: %s", getTypeName(expected), cmp.Diff(expected, actual))
}
}

Expand All @@ -131,7 +131,7 @@ func loadDirs(log *logger, inputs []string, outputs ...any) error {
return errors.New("output cannot be nil")
}

vlog := log.WithPrefix(fmt.Sprintf("%T", output))
vlog := log.WithPrefix(getTypeName(output))

if err := loadDir(vlog, inputs, output); err != nil {
return err
Expand All @@ -155,7 +155,7 @@ func loadDir(log *logger, inputs []string, output any) error {

tags, err := structtag.Parse(string(field.Tag))
if err != nil {
return fmt.Errorf("%T.%s: failed to parse struct tags: %w", output, field.Name, err)
return fmt.Errorf("%s.%s: failed to parse struct tags: %w", getTypeName(output), field.Name, err)
}

tag, err := tags.Get(tagName)
Expand All @@ -167,7 +167,7 @@ func loadDir(log *logger, inputs []string, output any) error {

for _, input := range inputs {
if err := loadDirInput(log, input, tag, field, value); err != nil {
return fmt.Errorf("%T.%s: %w", output, field.Name, err)
return fmt.Errorf("%s.%s: %w", getTypeName(output), field.Name, err)
}
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func saveDir(log *logger, dir string, input any) error {

tags, err := structtag.Parse(string(field.Tag))
if err != nil {
return fmt.Errorf("%T.%s: failed to parse struct tags: %w", input, field.Name, err)
return fmt.Errorf("%s.%s: failed to parse struct tags: %w", getTypeName(input), field.Name, err)
}

tag, err := tags.Get(tagName)
Expand All @@ -287,8 +287,8 @@ func saveDir(log *logger, dir string, input any) error {
continue
}

if err := saveDirField(log.WithPrefix("."+field.Name), dir, tag, field, value); err != nil {
return fmt.Errorf("%T.%s error: %w", input, field.Name, err)
if err := saveDirField(log.WithPrefix(fmt.Sprintf("%s.%s", getTypeName(input), field.Name)), dir, tag, field, value); err != nil {
return fmt.Errorf("%s.%s error: %w", getTypeName(input), field.Name, err)
}
}

Expand Down Expand Up @@ -394,3 +394,13 @@ func isBytes(targetType reflect.Type) bool {
func isMap(targetType reflect.Type) bool {
return targetType.Kind() == reflect.Map && isString(targetType.Key())
}

func getTypeName(input any) string {
t := reflect.TypeOf(input)

if t.Kind() == reflect.Ptr && t.Elem().PkgPath() == "" {
return "<anonymous>"
} else {
return t.String()
}
}
20 changes: 10 additions & 10 deletions testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func TestAssert(t *testing.T) {
Input: "hello world",
},
logs: []string{
`[GoT] Assert: .Input: saved file "<tmp>/input.txt" (size 11)`,
`[GoT] Assert: <anonymous>.Input: saved file "<tmp>/input.txt" (size 11)`,
},
},
{
Expand All @@ -425,7 +425,7 @@ func TestAssert(t *testing.T) {
Input: []byte("hello world"),
},
logs: []string{
`[GoT] Assert: .Input: saved file "<tmp>/input.txt" (size 11)`,
`[GoT] Assert: <anonymous>.Input: saved file "<tmp>/input.txt" (size 11)`,
},
},
{
Expand All @@ -436,7 +436,7 @@ func TestAssert(t *testing.T) {
Input: json.RawMessage(`{}`),
},
logs: []string{
`[GoT] Assert: .Input: saved file "<tmp>/input.json" (size 2)`,
`[GoT] Assert: <anonymous>.Input: saved file "<tmp>/input.json" (size 2)`,
},
},
{
Expand All @@ -453,7 +453,7 @@ func TestAssert(t *testing.T) {
},
},
logs: []string{
`[GoT] Assert: .Input: saved file "<tmp>/input.json" (size 22)`,
`[GoT] Assert: <anonymous>.Input: saved file "<tmp>/input.json" (size 22)`,
},
},
{
Expand All @@ -464,7 +464,7 @@ func TestAssert(t *testing.T) {
Input: map[string]string{"hello": "world"},
},
logs: []string{
`[GoT] Assert: .Input: saved file "<tmp>/input.json" (size 22)`,
`[GoT] Assert: <anonymous>.Input: saved file "<tmp>/input.json" (size 22)`,
},
},
{
Expand All @@ -475,8 +475,8 @@ func TestAssert(t *testing.T) {
Files: map[string]string{"a.txt": "A", "b.txt": "B"},
},
logs: []string{
`[GoT] Assert: .Files: saved file "<tmp>/a.txt" (size 1)`,
`[GoT] Assert: .Files: saved file "<tmp>/b.txt" (size 1)`,
`[GoT] Assert: <anonymous>.Files: saved file "<tmp>/a.txt" (size 1)`,
`[GoT] Assert: <anonymous>.Files: saved file "<tmp>/b.txt" (size 1)`,
},
},
{
Expand All @@ -501,7 +501,7 @@ func TestAssert(t *testing.T) {
Empty string `testdata:"-"`
}{},
logs: []string{
`[GoT] Assert: .Output: removed file "<tmp>/output.txt": empty`,
`[GoT] Assert: <anonymous>.Output: removed file "<tmp>/output.txt": empty`,
},
},
{
Expand All @@ -513,7 +513,7 @@ func TestAssert(t *testing.T) {
Output: "hello world",
},
logs: []string{
`[GoT] Assert: .Output: saved file "<tmp>/output.txt" (size 11)`,
`[GoT] Assert: <anonymous>.Output: saved file "<tmp>/output.txt" (size 11)`,
},
},
{
Expand All @@ -525,7 +525,7 @@ func TestAssert(t *testing.T) {
Output: "hello world",
},
logs: []string{
`[GoT] Assert: .Output: saved file "<tmp>/output.txt" (size 11)`,
`[GoT] Assert: <anonymous>.Output: saved file "<tmp>/output.txt" (size 11)`,
},
},
{
Expand Down

0 comments on commit 745cbb8

Please sign in to comment.