Explorar el Código

fix dump time.Time

Denmaseno hace 8 años
padre
commit
3e7aa5f38c
Se han modificado 2 ficheros con 33 adiciones y 22 borrados
  1. 23 16
      dump.go
  2. 10 6
      dump_test.go

+ 23 - 16
dump.go

@@ -6,6 +6,7 @@ import (
 	"io"
 	"reflect"
 	"strings"
+	"time"
 )
 
 const (
@@ -52,24 +53,28 @@ func fdump(w io.Writer, rx int, rd int, tab string, value reflect.Value) {
 	} else if kindIn(value.Kind(), reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64) {
 		fmt.Fprintf(w, "%+v /* Type:%s */", value, value.Type().String())
 	} else if value.Kind() == reflect.Struct {
-		ntab := tab + tabSpace
-		fmt.Fprintf(w, "{")
-		il := value.NumField()
-		for i, j := 0, 0; i < il && i < MaxFieldLen; i++ {
-			tf := value.Type().Field(i)
-			tag := strings.Split(value.Type().Field(i).Tag.Get("dump"), ",")
-			if !InStringSlice("ignore", tag...) {
-				if j > 0 {
-					fmt.Fprintf(w, ",\n%s\"%s\": ", ntab, tf.Name)
-				} else {
-					fmt.Fprintf(w, "\n%s\"%s\": ", ntab, tf.Name)
+		if t, ok := value.Interface().(time.Time); ok {
+			fmt.Fprintf(w, "%s /* Type:%s */", t, value.Type())
+		} else {
+			ntab := tab + tabSpace
+			fmt.Fprintf(w, "{ /* Type:%s */", value.Type().String())
+			il := value.NumField()
+			for i, j := 0, 0; i < il && i < MaxFieldLen; i++ {
+				tf := value.Type().Field(i)
+				tag := strings.Split(value.Type().Field(i).Tag.Get("dump"), ",")
+				if !InStringSlice("ignore", tag...) {
+					if j > 0 {
+						fmt.Fprintf(w, ",\n%s\"%s\": ", ntab, tf.Name)
+					} else {
+						fmt.Fprintf(w, "\n%s\"%s\": ", ntab, tf.Name)
+					}
+					vf := value.Field(i)
+					fdump(w, rx, rd+1, ntab, vf)
+					j++
 				}
-				vf := value.Field(i)
-				fdump(w, rx, rd+1, ntab, vf)
-				j++
 			}
+			fmt.Fprintf(w, "\n%s}", tab)
 		}
-		fmt.Fprintf(w, "\n%s}", tab)
 	} else if value.Kind() == reflect.Slice {
 		ntab := tab + tabSpace
 		if value.Type().String() == "[]uint8" {
@@ -115,7 +120,9 @@ func fdump(w io.Writer, rx int, rd int, tab string, value reflect.Value) {
 			fdump(w, rx, rd+1, ntab, value.MapIndex(kv))
 		}
 		fmt.Fprintf(w, "\n%s}%s", tab, "")
-	} else {
+	} else if !value.IsValid() {
+		fmt.Fprint(w, "/* NOT-VALID */")
+	} else if value.Type() != nil {
 		fmt.Fprintf(w, "undefined /* Type:%s Value:[%v] */", value.Type().String(), value)
 	}
 }

+ 10 - 6
dump_test.go

@@ -3,14 +3,16 @@ package util
 import (
 	"fmt"
 	"testing"
+	"time"
 )
 
 type Struct1 struct {
-	Name   string
-	secret string `dump:"ignore"`
-	Codes  []string
-	Config map[string]*Address
-	Age    uint
+	Name      string
+	secret    string `dump:"ignore"`
+	Codes     []string
+	Config    map[string]*Address
+	Age       uint
+	Timestamp time.Time
 }
 
 type Address struct {
@@ -33,6 +35,8 @@ func TestDump(t *testing.T) {
 	t.Logf("dump Struct1{}\n%s\n", Dump(Struct1{"seno", "secret",
 		[]string{"sono", "keling"},
 		config,
-		17}))
+		17,
+		time.Now(),
+	}))
 	t.Logf("fmt.Sprintf %s", fmt.Sprintf("HERE\n%v", &Address{"seno", "solo"}))
 }