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

Elide printing type assertions on unnamed types #21

Merged
merged 2 commits into from
Sep 27, 2017
Merged
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
56 changes: 55 additions & 1 deletion cmp/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmp_test
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -357,7 +358,9 @@ root:
}

func transformerTests() []test {
const label = "Transformer/"
type JSON string

const label = "Transformer"

return []test{{
label: label,
Expand Down Expand Up @@ -418,6 +421,57 @@ func transformerTests() []test {
λ({int}):
-: "string"
+: 1`,
}, {
label: label,
x: JSON(`{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"isAlive": true,
"address": {
"city": "Los Angeles",
"postalCode": "10021-3100",
"state": "CA",
"streetAddress": "21 2nd Street"
},
"phoneNumbers": [{
"type": "home",
"number": "212 555-4321"
},{
"type": "office",
"number": "646 555-4567"
},{
"number": "123 456-7890",
"type": "mobile"
}],
"children": []
}`),
y: JSON(`{"firstName":"John","lastName":"Smith","isAlive":true,"age":25,
"address":{"streetAddress":"21 2nd Street","city":"New York",
"state":"NY","postalCode":"10021-3100"},"phoneNumbers":[{"type":"home",
"number":"212 555-1234"},{"type":"office","number":"646 555-4567"},{
"type":"mobile","number":"123 456-7890"}],"children":[],"spouse":null}`),
opts: []cmp.Option{
cmp.Transformer("ParseJSON", func(s JSON) (m map[string]interface{}) {
if err := json.Unmarshal([]byte(s), &m); err != nil {
panic(err)
}
return m
}),
},
wantDiff: `
ParseJSON({cmp_test.JSON})["address"]["city"]:
-: "Los Angeles"
+: "New York"
ParseJSON({cmp_test.JSON})["address"]["state"]:
-: "CA"
+: "NY"
ParseJSON({cmp_test.JSON})["phoneNumbers"][0]["number"]:
-: "212 555-4321"
+: "212 555-1234"
ParseJSON({cmp_test.JSON})["spouse"]:
-: <non-existent>
+: interface {}(nil)`,
}}
}

Expand Down
13 changes: 6 additions & 7 deletions cmp/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,12 @@ func (pa Path) GoString() string {
ssPost = append(ssPost, ")")
continue
case *typeAssertion:
// Elide type assertions immediately following a transform to
// prevent overly verbose path printouts.
// Some transforms return interface{} because of Go's lack of
// generics, but typically take in and return the exact same
// concrete type. Other times, the transform creates an anonymous
// struct, which will be very verbose to print.
if _, ok := nextStep.(*transform); ok {
// As a special-case, elide type assertions on anonymous types
// since they are typically generated dynamically and can be very
// verbose. For example, some transforms return interface{} because
// of Go's lack of generics, but typically take in and return the
// exact same concrete type.
if s.Type().PkgPath() == "" {
continue
}
}
Expand Down