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

Fixes for reflection changes #1

Merged
merged 1 commit into from
Apr 16, 2011
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func (fo formatter) Format(f fmt.State, c int) {


func (fo formatter) format(w io.Writer) {
value := reflect.NewValue(fo.x)
switch v := value.(type) {
case *reflect.StringValue:
v := reflect.NewValue(fo.x)
switch v.Kind() {
case reflect.String:
lim := limit - 8*fo.d
s := v.Get()
s := v.String()
z := len(s)
n := (z + lim - 1) / lim
if n < 1 {
Expand All @@ -97,40 +97,40 @@ func (fo formatter) format(w io.Writer) {
fmt.Fprintf(w, "%#v", s[l:h])
}
return
case *reflect.PtrValue:
case reflect.Ptr:
e := v.Elem()
if e == nil {
if !e.IsValid() {
fmt.Fprintf(w, "%#v", fo.x)
} else {
writeByte(w, '&')
fmt.Fprintf(w, "%# v", formatter{d: fo.d, x: e.Interface()})
}
case *reflect.SliceValue:
case reflect.Slice:
s := fmt.Sprintf("%#v", fo.x)
if len(s) < limit {
io.WriteString(w, s)
return
}

t := v.Type().(*reflect.SliceType)
_, keep := t.Elem().(*reflect.InterfaceType)
t := v.Type()

io.WriteString(w, reflect.Typeof(fo.x).String())
w.Write(openCurlyLFBytes)
for i := 0; i < v.Len(); i++ {
for j := 0; j < fo.d+1; j++ {
writeByte(w, '\t')
}
inner := formatter{d: fo.d + 1, x: v.Elem(i).Interface(), omit: !keep}
inner := formatter{d: fo.d + 1, x: v.Index(i).Interface(), omit: t.Elem().Kind() != reflect.Interface}
fmt.Fprintf(w, "%# v", inner)
w.Write(commaLFBytes)
}
for j := 0; j < fo.d; j++ {
writeByte(w, '\t')
}
writeByte(w, '}')
case *reflect.StructValue:
t := v.Type().(*reflect.StructType)
if reflect.DeepEqual(reflect.MakeZero(t).Interface(), fo.x) {
case reflect.Struct:
t := v.Type()
if reflect.DeepEqual(reflect.Zero(t).Interface(), fo.x) {
if !fo.omit {
io.WriteString(w, t.String())
}
Expand All @@ -154,7 +154,7 @@ func (fo formatter) format(w io.Writer) {
var max int
for i := 0; i < v.NumField(); i++ {
if v := t.Field(i); v.Name != "" {
if len(v.Name) + 2 > max {
if len(v.Name)+2 > max {
max = len(v.Name) + 2
}
}
Expand Down
8 changes: 3 additions & 5 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ var gosyntax = []test{
{"a", `"a"`},
{1, "1"},
{F(5), "F(5)"},
{long, `"`+long[:50]+"\" +\n\""+long[50:]+`"`},
{long, `"` + long[:50] + "\" +\n\"" + long[50:] + `"`},
{
LongStructTypeName{
longFieldName: LongStructTypeName{
},
longFieldName: LongStructTypeName{},
otherLongFieldName: long,
},
`pretty.LongStructTypeName{
Expand All @@ -48,8 +47,7 @@ var gosyntax = []test{
},
{
&LongStructTypeName{
longFieldName: &LongStructTypeName{
},
longFieldName: &LongStructTypeName{},
otherLongFieldName: (*LongStructTypeName)(nil),
},
`&pretty.LongStructTypeName{
Expand Down