Skip to content

Commit 714318b

Browse files
FiloSottilebradfitz
authored andcommitted
expvar: add Value methods
Closes #15815 Change-Id: I08154dbff416198cf7787e446b1e00e62c03a972 Reviewed-on: https://go-review.googlesource.com/30917 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 46276d6 commit 714318b

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/expvar/expvar.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type Int struct {
4949
i int64
5050
}
5151

52+
func (v *Int) Value() int64 {
53+
return atomic.LoadInt64(&v.i)
54+
}
55+
5256
func (v *Int) String() string {
5357
return strconv.FormatInt(atomic.LoadInt64(&v.i), 10)
5458
}
@@ -66,6 +70,10 @@ type Float struct {
6670
f uint64
6771
}
6872

73+
func (v *Float) Value() float64 {
74+
return math.Float64frombits(atomic.LoadUint64(&v.f))
75+
}
76+
6977
func (v *Float) String() string {
7078
return strconv.FormatFloat(
7179
math.Float64frombits(atomic.LoadUint64(&v.f)), 'g', -1, 64)
@@ -219,6 +227,14 @@ type String struct {
219227
s string
220228
}
221229

230+
func (v *String) Value() string {
231+
v.mu.RLock()
232+
defer v.mu.RUnlock()
233+
return v.s
234+
}
235+
236+
// String implements the Val interface. To get the unquoted string
237+
// use Value.
222238
func (v *String) String() string {
223239
v.mu.RLock()
224240
s := v.s
@@ -237,6 +253,10 @@ func (v *String) Set(value string) {
237253
// and formatting the returned value using JSON.
238254
type Func func() interface{}
239255

256+
func (f Func) Value() interface{} {
257+
return f()
258+
}
259+
240260
func (f Func) String() string {
241261
v, _ := json.Marshal(f())
242262
return string(v)

src/expvar/expvar_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ package expvar
77
import (
88
"bytes"
99
"encoding/json"
10-
"math"
1110
"net"
1211
"net/http/httptest"
12+
"reflect"
1313
"runtime"
1414
"strconv"
1515
"sync"
16-
"sync/atomic"
1716
"testing"
1817
)
1918

@@ -58,6 +57,10 @@ func TestInt(t *testing.T) {
5857
if reqs.i != -2 {
5958
t.Errorf("reqs.i = %v, want -2", reqs.i)
6059
}
60+
61+
if v, want := reqs.Value(), int64(-2); v != want {
62+
t.Errorf("reqs.Value() = %q, want %q", v, want)
63+
}
6164
}
6265

6366
func BenchmarkIntAdd(b *testing.B) {
@@ -80,10 +83,6 @@ func BenchmarkIntSet(b *testing.B) {
8083
})
8184
}
8285

83-
func (v *Float) val() float64 {
84-
return math.Float64frombits(atomic.LoadUint64(&v.f))
85-
}
86-
8786
func TestFloat(t *testing.T) {
8887
RemoveAll()
8988
reqs := NewFloat("requests-float")
@@ -96,17 +95,17 @@ func TestFloat(t *testing.T) {
9695

9796
reqs.Add(1.5)
9897
reqs.Add(1.25)
99-
if v := reqs.val(); v != 2.75 {
100-
t.Errorf("reqs.val() = %v, want 2.75", v)
98+
if v := reqs.Value(); v != 2.75 {
99+
t.Errorf("reqs.Value() = %v, want 2.75", v)
101100
}
102101

103102
if s := reqs.String(); s != "2.75" {
104103
t.Errorf("reqs.String() = %q, want \"4.64\"", s)
105104
}
106105

107106
reqs.Add(-2)
108-
if v := reqs.val(); v != 0.75 {
109-
t.Errorf("reqs.val() = %v, want 0.75", v)
107+
if v := reqs.Value(); v != 0.75 {
108+
t.Errorf("reqs.Value() = %v, want 0.75", v)
110109
}
111110
}
112111

@@ -146,6 +145,10 @@ func TestString(t *testing.T) {
146145
t.Errorf("from %q, name.String() = %q, want %q", name.s, s, want)
147146
}
148147

148+
if s, want := name.Value(), "Mike"; s != want {
149+
t.Errorf("from %q, name.Value() = %q, want %q", name.s, s, want)
150+
}
151+
149152
// Make sure we produce safe JSON output.
150153
name.Set(`<`)
151154
if s, want := name.String(), "\"\\u003c\""; s != want {
@@ -177,7 +180,7 @@ func TestMapCounter(t *testing.T) {
177180
if x := colors.m["blue"].(*Int).i; x != 4 {
178181
t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
179182
}
180-
if x := colors.m[`green "midori"`].(*Float).val(); x != 4.125 {
183+
if x := colors.m[`green "midori"`].(*Float).Value(); x != 4.125 {
181184
t.Errorf("colors.m[`green \"midori\"] = %v, want 4.125", x)
182185
}
183186

@@ -242,6 +245,9 @@ func TestFunc(t *testing.T) {
242245
if s, exp := f.String(), `["a","b"]`; s != exp {
243246
t.Errorf(`f.String() = %q, want %q`, s, exp)
244247
}
248+
if v := f.Value(); !reflect.DeepEqual(v, x) {
249+
t.Errorf(`f.Value() = %q, want %q`, v, x)
250+
}
245251

246252
x = 17
247253
if s, exp := f.String(), `17`; s != exp {

0 commit comments

Comments
 (0)