Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit a658407

Browse files
rogpepperakyll
authored andcommitted
Avoid mention of NewMap in the documentation (#759)
The NewMap function does not exist - New seems to be the only way to create a new map. Also lose the map size hint in newMap - it's currently always zero and can easily be reinstated later as an optimization if needed. Also include trivial optimization to slice allocation in String.
1 parent 76288e4 commit a658407

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

stats/view/view_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func Test_View_MeasureFloat64_AggregationDistribution(t *testing.T) {
171171
}
172172
ctx, err := tag.New(context.Background(), mods...)
173173
if err != nil {
174-
t.Errorf("%v: NewMap = %v", tc.label, err)
174+
t.Errorf("%v: New = %v", tc.label, err)
175175
}
176176
view.addSample(tag.FromContext(ctx), r.f)
177177
}

tag/map.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type Tag struct {
2828
Value string
2929
}
3030

31-
// Map is a map of tags. Use NewMap to build tag maps.
31+
// Map is a map of tags. Use New to create a context containing
32+
// a new Map.
3233
type Map struct {
3334
m map[Key]string
3435
}
3536

36-
// Value returns the value for the key if a value
37-
// for the key exists.
37+
// Value returns the value for the key if a value for the key exists.
3838
func (m *Map) Value(k Key) (string, bool) {
3939
if m == nil {
4040
return "", false
@@ -47,7 +47,7 @@ func (m *Map) String() string {
4747
if m == nil {
4848
return "nil"
4949
}
50-
var keys []Key
50+
keys := make([]Key, 0, len(m.m))
5151
for k := range m.m {
5252
keys = append(keys, k)
5353
}
@@ -83,8 +83,8 @@ func (m *Map) delete(k Key) {
8383
delete(m.m, k)
8484
}
8585

86-
func newMap(sizeHint int) *Map {
87-
return &Map{m: make(map[Key]string, sizeHint)}
86+
func newMap() *Map {
87+
return &Map{m: make(map[Key]string)}
8888
}
8989

9090
// Mutator modifies a tag map.
@@ -153,7 +153,7 @@ func Delete(k Key) Mutator {
153153
// originated from the incoming context and modified
154154
// with the provided mutators.
155155
func New(ctx context.Context, mutator ...Mutator) (context.Context, error) {
156-
m := newMap(0)
156+
m := newMap()
157157
orig := FromContext(ctx)
158158
if orig != nil {
159159
for k, v := range orig.m {

tag/map_codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func Encode(m *Map) []byte {
176176

177177
// Decode decodes the given []byte into a tag map.
178178
func Decode(bytes []byte) (*Map, error) {
179-
ts := newMap(0)
179+
ts := newMap()
180180

181181
eg := &encoderGRPC{
182182
buf: bytes,

tag/map_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestEncodeDecode(t *testing.T) {
8080
}
8181
ctx, err := New(context.Background(), mods...)
8282
if err != nil {
83-
t.Errorf("%v: NewMap = %v", tc.label, err)
83+
t.Errorf("%v: New = %v", tc.label, err)
8484
}
8585

8686
encoded := Encode(FromContext(ctx))

tag/map_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestContext(t *testing.T) {
3333
Insert(k2, "v2"),
3434
)
3535
got := FromContext(ctx)
36-
want := newMap(2)
36+
want := newMap()
3737
want.insert(k1, "v1")
3838
want.insert(k2, "v2")
3939

@@ -51,7 +51,7 @@ func TestDo(t *testing.T) {
5151
Insert(k2, "v2"),
5252
)
5353
got := FromContext(ctx)
54-
want := newMap(2)
54+
want := newMap()
5555
want.insert(k1, "v1")
5656
want.insert(k2, "v2")
5757
Do(ctx, func(ctx context.Context) {
@@ -168,7 +168,7 @@ func TestNewMap(t *testing.T) {
168168
}
169169
}
170170

171-
func TestNewMapValidation(t *testing.T) {
171+
func TestNewValidation(t *testing.T) {
172172
tests := []struct {
173173
err string
174174
seed *Map
@@ -213,7 +213,7 @@ func TestNewMapValidation(t *testing.T) {
213213
}
214214

215215
func makeTestTagMap(ids ...int) *Map {
216-
m := newMap(len(ids))
216+
m := newMap()
217217
for _, v := range ids {
218218
k, _ := NewKey(fmt.Sprintf("k%d", v))
219219
m.m[k] = fmt.Sprintf("v%d", v)

0 commit comments

Comments
 (0)