Skip to content

Commit c3d0053

Browse files
committed
Review comments #2.
1 parent a45ad7e commit c3d0053

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

attributes/attributes.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func New(kvs ...interface{}) *Attributes {
5151
// remove an existing key, use a nil value.
5252
func (a *Attributes) WithValues(kvs ...interface{}) *Attributes {
5353
if a == nil {
54-
a = New()
54+
return New(kvs...)
5555
}
5656
if len(kvs)%2 != 0 {
5757
panic(fmt.Sprintf("attributes.New called with unexpected input: len(kvs) = %v", len(kvs)))
@@ -69,5 +69,8 @@ func (a *Attributes) WithValues(kvs ...interface{}) *Attributes {
6969
// Value returns the value associated with these attributes for key, or nil if
7070
// no value is associated with key.
7171
func (a *Attributes) Value(key interface{}) interface{} {
72+
if a == nil {
73+
return nil
74+
}
7275
return a.m[key]
7376
}

balancer/weightedroundrobin/weightedroundrobin.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ type AddrInfo struct {
3737
}
3838

3939
// SetAddrInfo sets addInfo in the Attributes field of addr.
40-
func SetAddrInfo(addrInfo *AddrInfo, addr *resolver.Address) {
40+
// This is an EXPERIMENTAL API.
41+
func SetAddrInfo(addrInfo AddrInfo, addr *resolver.Address) {
4142
addr.Attributes = addr.Attributes.WithValues(attributeKey{}, addrInfo)
4243
}
4344

4445
// GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr.
4546
// Returns nil if no AddrInfo is present.
46-
func GetAddrInfo(addr *resolver.Address) *AddrInfo {
47+
// This is an EXPERIMENTAL API.
48+
func GetAddrInfo(addr *resolver.Address) AddrInfo {
4749
if addr == nil || addr.Attributes == nil {
48-
return nil
50+
return AddrInfo{}
4951
}
5052
ai := addr.Attributes.Value(attributeKey{})
5153
if ai == nil {
52-
return nil
54+
return AddrInfo{}
5355
}
54-
if _, ok := ai.(*AddrInfo); !ok {
55-
return nil
56+
if _, ok := ai.(AddrInfo); !ok {
57+
return AddrInfo{}
5658
}
57-
return ai.(*AddrInfo)
59+
return ai.(AddrInfo)
5860
}

balancer/weightedroundrobin/weightedwoundrobin_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,33 @@ import (
2929
func TestAddAddrInfoToAndFromAttributes(t *testing.T) {
3030
tests := []struct {
3131
desc string
32-
inputAddrInfo *AddrInfo
32+
inputAddrInfo AddrInfo
3333
inputAttributes *attributes.Attributes
34-
wantAddrInfo *AddrInfo
34+
wantAddrInfo AddrInfo
3535
}{
3636
{
3737
desc: "empty attributes",
38-
inputAddrInfo: &AddrInfo{Weight: 100},
38+
inputAddrInfo: AddrInfo{Weight: 100},
3939
inputAttributes: nil,
40-
wantAddrInfo: &AddrInfo{Weight: 100},
40+
wantAddrInfo: AddrInfo{Weight: 100},
4141
},
4242
{
4343
desc: "non-empty attributes",
44-
inputAddrInfo: &AddrInfo{Weight: 100},
44+
inputAddrInfo: AddrInfo{Weight: 100},
4545
inputAttributes: attributes.New("foo", "bar"),
46-
wantAddrInfo: &AddrInfo{Weight: 100},
46+
wantAddrInfo: AddrInfo{Weight: 100},
4747
},
4848
{
4949
desc: "addrInfo not present in empty attributes",
50-
inputAddrInfo: nil,
50+
inputAddrInfo: AddrInfo{},
5151
inputAttributes: nil,
52-
wantAddrInfo: nil,
52+
wantAddrInfo: AddrInfo{},
5353
},
5454
{
5555
desc: "addrInfo not present in non-empty attributes",
56-
inputAddrInfo: nil,
56+
inputAddrInfo: AddrInfo{},
5757
inputAttributes: attributes.New("foo", "bar"),
58-
wantAddrInfo: nil,
58+
wantAddrInfo: AddrInfo{},
5959
},
6060
}
6161

0 commit comments

Comments
 (0)