Skip to content

Commit

Permalink
prettier types
Browse files Browse the repository at this point in the history
  • Loading branch information
Achille Roussel committed Feb 25, 2019
1 parent 355e3c5 commit fcd869f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/segmentio/conf

require (
github.com/segmentio/go-snakecase v1.1.0 // indirect
github.com/segmentio/objconv v1.0.1
gopkg.in/go-playground/mold.v2 v2.2.0
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/segmentio/go-snakecase v1.1.0 h1:ZJO4SNKKV0MjGOv0LHnixxN5FYv1JKBnVXEuBpwcbQI=
github.com/segmentio/go-snakecase v1.1.0/go.mod h1:jk1miR5MS7Na32PZUykG89Arm+1BUSYhuGR6b7+hJto=
github.com/segmentio/objconv v1.0.1 h1:QjfLzwriJj40JibCV3MGSEiAoXixbp4ybhwfTB8RXOM=
github.com/segmentio/objconv v1.0.1/go.mod h1:auayaH5k3137Cl4SoXTgrzQcuQDmvuVtZgS0fb1Ahys=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/mold.v2 v2.2.0 h1:Y4IYB4/HYQfuq43zaKh6vs9cVelLE9qbqe2fkyfCTWQ=
gopkg.in/go-playground/mold.v2 v2.2.0/go.mod h1:XMyyRsGtakkDPbxXbrA5VODo6bUXyvoDjLd5l3T0XoA=
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 h1:WB265cn5OpO+hK3pikC9hpP1zI/KTwmyMFKloW9eOVc=
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
18 changes: 10 additions & 8 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func TestNodeString(t *testing.T) {
node: Scalar{reflect.ValueOf("Hello World!")},
},
{
repr: `2016-12-31T23:42:59Z`,
repr: `"2016-12-31T23:42:59Z"`,
node: Scalar{reflect.ValueOf(date)},
},
{
Expand Down Expand Up @@ -373,7 +373,9 @@ func TestNodeString(t *testing.T) {
for _, test := range tests {
t.Run(test.repr, func(t *testing.T) {
if repr := test.node.String(); repr != test.repr {
t.Error(repr)
t.Error("representation mismatch")
t.Log("expected:", test.repr)
t.Log("found: ", repr)
}
})
}
Expand Down Expand Up @@ -471,12 +473,12 @@ func Test_FlattenedEmbeddedStructs(t *testing.T) {
}

type Medium struct {
Small `conf:"_"`
Small `conf:"_"`
MediumOne string
}

type Matroska struct {
Medium `conf:"_"`
Medium `conf:"_"`
LargeOne string
}

Expand Down Expand Up @@ -523,17 +525,17 @@ func Test_InvalidFlattenedEmbeddedStructs(t *testing.T) {
tests := []struct {
val interface{}
errFragments []string
} {
}{
{
val: ConflictingName{},
val: ConflictingName{},
errFragments: []string{"'Stuff'", "duplicate"},
},
{
val: EmbedPrimitive{},
val: EmbedPrimitive{},
errFragments: []string{"\"_\"", "at path EmbedPrimitive.Str"},
},
{
val: EmbedNamedStruct{},
val: EmbedNamedStruct{},
errFragments: []string{"\"_\"", "at path EmbedNamedStruct.Thing"},
},
}
Expand Down
6 changes: 5 additions & 1 deletion print.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ func prettyType(t reflect.Type) string {
case reflect.Ptr:
return prettyType(t.Elem())
default:
return t.String()
s := strings.ToLower(t.String())
if i := strings.LastIndexByte(s, '.'); i >= 0 {
s = s[i+1:]
}
return s
}
}

Expand Down
4 changes: 4 additions & 0 deletions print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"
)

type Bytes uint64

func TestPrettyType(t *testing.T) {
tests := []struct {
v interface{}
Expand Down Expand Up @@ -43,6 +45,8 @@ func TestPrettyType(t *testing.T) {
{map[int]int{}, "object"},
{struct{}{}, "object"},
{&struct{}{}, "object"},

{Bytes(0), "bytes"},
}

for _, test := range tests {
Expand Down

0 comments on commit fcd869f

Please sign in to comment.