mirror of
https://github.com/golang/go
synced 2024-11-21 19:34:46 -07:00
expvar: add Func for functions that return values that are JSON marshalable.
Remove {Float,Int,String}Func, which are now redundant. Fixes #1684. R=rsc, r, r2 CC=golang-dev https://golang.org/cl/4410041
This commit is contained in:
parent
6ca71fb897
commit
57d0c26c01
@ -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))
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user