]> Cypherpunks repositories - gostls13.git/commitdiff
expvar: add Func for functions that return values that are JSON marshalable.
authorDavid Symonds <dsymonds@golang.org>
Fri, 15 Apr 2011 08:21:18 +0000 (01:21 -0700)
committerDavid Symonds <dsymonds@golang.org>
Fri, 15 Apr 2011 08:21:18 +0000 (01:21 -0700)
Remove {Float,Int,String}Func, which are now redundant.

Fixes #1684.

R=rsc, r, r2
CC=golang-dev
https://golang.org/cl/4410041

src/pkg/expvar/expvar.go
src/pkg/expvar/expvar_test.go

index ed6cff78db40fbf9eda4ae796f9de1c279977aa7..7736aea0a02acc50fe10a2584b46bbcb5c030e09 100644 (file)
@@ -180,23 +180,14 @@ func (v *String) String() string { return strconv.Quote(v.s) }
 
 func (v *String) Set(value string) { v.s = value }
 
-// IntFunc wraps a func() int64 to create a value that satisfies the Var interface.
-// The function will be called each time the Var is evaluated.
-type IntFunc func() int64
+// Func implements Var by calling the function
+// and formatting the returned value using JSON.
+type Func func() interface{}
 
-func (v IntFunc) String() string { return strconv.Itoa64(v()) }
-
-// FloatFunc wraps a func() float64 to create a value that satisfies the Var interface.
-// The function will be called each time the Var is evaluated.
-type FloatFunc func() float64
-
-func (v FloatFunc) String() string { return strconv.Ftoa64(v(), 'g', -1) }
-
-// StringFunc wraps a func() string to create value that satisfies the Var interface.
-// The function will be called each time the Var is evaluated.
-type StringFunc func() string
-
-func (f StringFunc) String() string { return strconv.Quote(f()) }
+func (f Func) String() string {
+       v, _ := json.Marshal(f())
+       return string(v)
+}
 
 
 // All published variables.
@@ -282,18 +273,16 @@ func expvarHandler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "\n}\n")
 }
 
-func memstats() string {
-       b, _ := json.MarshalIndent(&runtime.MemStats, "", "\t")
-       return string(b)
+func cmdline() interface{} {
+       return os.Args
 }
 
-func cmdline() string {
-       b, _ := json.Marshal(os.Args)
-       return string(b)
+func memstats() interface{} {
+       return runtime.MemStats
 }
 
 func init() {
        http.Handle("/debug/vars", http.HandlerFunc(expvarHandler))
-       Publish("cmdline", StringFunc(cmdline))
-       Publish("memstats", StringFunc(memstats))
+       Publish("cmdline", Func(cmdline))
+       Publish("memstats", Func(memstats))
 }
index a8b1a96a93cceed49379d900d99f0ce382e57b3d..94926d9f8ce045f921c2e62995b2e63d0eab5483 100644 (file)
@@ -114,41 +114,15 @@ func TestMapCounter(t *testing.T) {
        }
 }
 
-func TestIntFunc(t *testing.T) {
-       x := int64(4)
-       ix := IntFunc(func() int64 { return x })
-       if s := ix.String(); s != "4" {
-               t.Errorf("ix.String() = %v, want 4", s)
+func TestFunc(t *testing.T) {
+       var x interface{} = []string{"a", "b"}
+       f := Func(func() interface{} { return x })
+       if s, exp := f.String(), `["a","b"]`; s != exp {
+               t.Errorf(`f.String() = %q, want %q`, s, exp)
        }
 
-       x++
-       if s := ix.String(); s != "5" {
-               t.Errorf("ix.String() = %v, want 5", s)
-       }
-}
-
-func TestFloatFunc(t *testing.T) {
-       x := 8.5
-       ix := FloatFunc(func() float64 { return x })
-       if s := ix.String(); s != "8.5" {
-               t.Errorf("ix.String() = %v, want 3.14", s)
-       }
-
-       x -= 1.25
-       if s := ix.String(); s != "7.25" {
-               t.Errorf("ix.String() = %v, want 4.34", s)
-       }
-}
-
-func TestStringFunc(t *testing.T) {
-       x := "hello"
-       sx := StringFunc(func() string { return x })
-       if s, exp := sx.String(), `"hello"`; s != exp {
-               t.Errorf(`sx.String() = %q, want %q`, s, exp)
-       }
-
-       x = "goodbye"
-       if s, exp := sx.String(), `"goodbye"`; s != exp {
-               t.Errorf(`sx.String() = %q, want %q`, s, exp)
+       x = 17
+       if s, exp := f.String(), `17`; s != exp {
+               t.Errorf(`f.String() = %q, want %q`, s, exp)
        }
 }