Repository: go-logr/logr
Pull request #92 (2021-09-20 18:27:06)

Support complex{64,128} in funcr

thockin: Ahh, now I recall.  JSON does not support complex either, so the test
doesn't work well.  :(

On Mon, Sep 20, 2021 at 1:17 PM Patrick Ohly ***@***.***>
wrote:

> ***@***.**** commented on this pull request.
>
> Should we have unit tests for this?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/go-logr/logr/pull/92#pullrequestreview-759057235>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABKWAVBUOMHKDLAQYF4XH5TUC6JGFANCNFSM5EMRLIJQ>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
>
>


Patch:
diff --git a/funcr/funcr.go b/funcr/funcr.go
index ff47817..965fd75 100644
--- a/funcr/funcr.go
+++ b/funcr/funcr.go
@@ -177,7 +177,7 @@ func prettyWithFlags(value interface{}, flags uint32) string {
 			return v
 		}
 		// This is empirically faster than strings.Builder.
-		return `"` + v + `"`
+		return strconv.Quote(v)
 	case int:
 		return strconv.FormatInt(int64(v), 10)
 	case int8:
@@ -204,6 +204,10 @@ func prettyWithFlags(value interface{}, flags uint32) string {
 		return strconv.FormatFloat(float64(v), 'f', -1, 32)
 	case float64:
 		return strconv.FormatFloat(v, 'f', -1, 64)
+	case complex64:
+		return `"` + strconv.FormatComplex(complex128(v), 'f', -1, 64) + `"`
+	case complex128:
+		return `"` + strconv.FormatComplex(v, 'f', -1, 128) + `"`
 	}
 
 	buf := bytes.NewBuffer(make([]byte, 0, 256))
@@ -229,6 +233,10 @@ func prettyWithFlags(value interface{}, flags uint32) string {
 		return strconv.FormatFloat(float64(v.Float()), 'f', -1, 32)
 	case reflect.Float64:
 		return strconv.FormatFloat(v.Float(), 'f', -1, 64)
+	case reflect.Complex64:
+		return `"` + strconv.FormatComplex(complex128(v.Complex()), 'f', -1, 64) + `"`
+	case reflect.Complex128:
+		return `"` + strconv.FormatComplex(v.Complex(), 'f', -1, 128) + `"`
 	case reflect.Struct:
 		buf.WriteRune('{')
 		for i := 0; i < t.NumField(); i++ {


Test patch:
diff --git a/funcr/funcr_test.go b/funcr/funcr_test.go
index 735cbd6..1c201c9 100644
--- a/funcr/funcr_test.go
+++ b/funcr/funcr_test.go
@@ -36,84 +36,115 @@ func ptrstr(s string) *string {
 }
 
 func TestPretty(t *testing.T) {
-	cases := []interface{}{
-		"strval",
-		substr("substrval"),
-		true,
-		false,
-		int(93),
-		int8(93),
-		int16(93),
-		int32(93),
-		int64(93),
-		int(-93),
-		int8(-93),
-		int16(-93),
-		int32(-93),
-		int64(-93),
-		uint(93),
-		uint8(93),
-		uint16(93),
-		uint32(93),
-		uint64(93),
-		uintptr(93),
-		float32(93.76),
-		float64(93.76),
-		ptrint(93),
-		ptrstr("pstrval"),
-		[]int{9, 3, 7, 6},
-		[4]int{9, 3, 7, 6},
-		struct {
-			Int    int
-			String string
-		}{
-			93, "seventy-six",
+	cases := []struct {
+		val interface{}
+		exp string // used in cases where JSON can't handle it
+	}{
+		{val: "strval"},
+		{val: substr("substrval")},
+		{val: true},
+		{val: false},
+		{val: int(93)},
+		{val: int8(93)},
+		{val: int16(93)},
+		{val: int32(93)},
+		{val: int64(93)},
+		{val: int(-93)},
+		{val: int8(-93)},
+		{val: int16(-93)},
+		{val: int32(-93)},
+		{val: int64(-93)},
+		{val: uint(93)},
+		{val: uint8(93)},
+		{val: uint16(93)},
+		{val: uint32(93)},
+		{val: uint64(93)},
+		{val: uintptr(93)},
+		{val: float32(93.76)},
+		{val: float64(93.76)},
+		{
+			val: complex64(93i),
+			exp: `"(0+93i)"`,
 		},
-		map[string]int{
-			"nine": 3,
+		{
+			val: complex128(93i),
+			exp: `"(0+93i)"`,
 		},
-		map[substr]int{
-			"nine": 3,
+		{val: ptrint(93)},
+		{val: ptrstr("pstrval")},
+		{val: []int{9, 3, 7, 6}},
+		{val: [4]int{9, 3, 7, 6}},
+		{
+			val: struct {
+				Int    int
+				String string
+			}{
+				93, "seventy-six",
+			},
 		},
-		fmt.Errorf("error"),
-		struct {
-			X int `json:"x"`
-			Y int `json:"y"`
-		}{
-			93, 76,
+		{
+			val: map[string]int{
+				"nine": 3,
+			},
 		},
-		struct {
-			X []int
-			Y map[int]int
-			Z struct{ P, Q int }
-		}{
-			[]int{9, 3, 7, 6},
-			map[int]int{9: 3},
-			struct{ P, Q int }{9, 3},
+		{
+			val: map[substr]int{
+				"nine": 3,
+			},
 		},
-		[]struct{ X, Y string }{
-			{"nine", "three"},
-			{"seven", "six"},
+		{val: fmt.Errorf("error")},
+		{
+			val: struct {
+				X int `json:"x"`
+				Y int `json:"y"`
+			}{
+				93, 76,
+			},
 		},
-		struct {
-			A *int
-			B *int
-			C interface{}
-			D interface{}
-		}{
-			B: ptrint(1),
-			D: interface{}(2),
+		{
+			val: struct {
+				X []int
+				Y map[int]int
+				Z struct{ P, Q int }
+			}{
+				[]int{9, 3, 7, 6},
+				map[int]int{9: 3},
+				struct{ P, Q int }{9, 3},
+			},
+		},
+		{
+			val: []struct{ X, Y string }{
+				{"nine", "three"},
+				{"seven", "six"},
+			},
+		},
+		{
+			val: struct {
+				A *int
+				B *int
+				C interface{}
+				D interface{}
+			}{
+				B: ptrint(1),
+				D: interface{}(2),
+			},
 		},
 	}
 
 	for i, tc := range cases {
-		ours := pretty(tc)
-		std, err := json.Marshal(tc)
-		if err != nil {
-			t.Errorf("[%d]: unexpected error: %v", i, err)
+		ours := pretty(tc.val)
+		want := ""
+		if tc.exp != "" {
+			want = tc.exp
+		} else {
+			jb, err := json.Marshal(tc.val)
+			if err != nil {
+				t.Errorf("[%d]: unexpected error: %v", i, err)
+			}
+			want = string(jb)
 		}
-		if ours != string(std) {
-			t.Errorf("[%d]: expected %q, got %q", i, std, ours)
+		if ours != want {
+			t.Errorf("[%d]: expected %q, got %q", i, want, ours)
 		}
 	}
 }