Skip to content

Commit

Permalink
Review comments #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars committed Apr 16, 2020
1 parent a45ad7e commit c3d0053
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
5 changes: 4 additions & 1 deletion attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func New(kvs ...interface{}) *Attributes {
// remove an existing key, use a nil value.
func (a *Attributes) WithValues(kvs ...interface{}) *Attributes {
if a == nil {
a = New()
return New(kvs...)
}
if len(kvs)%2 != 0 {
panic(fmt.Sprintf("attributes.New called with unexpected input: len(kvs) = %v", len(kvs)))
Expand All @@ -69,5 +69,8 @@ func (a *Attributes) WithValues(kvs ...interface{}) *Attributes {
// Value returns the value associated with these attributes for key, or nil if
// no value is associated with key.
func (a *Attributes) Value(key interface{}) interface{} {
if a == nil {
return nil
}
return a.m[key]
}
16 changes: 9 additions & 7 deletions balancer/weightedroundrobin/weightedroundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ type AddrInfo struct {
}

// SetAddrInfo sets addInfo in the Attributes field of addr.
func SetAddrInfo(addrInfo *AddrInfo, addr *resolver.Address) {
// This is an EXPERIMENTAL API.
func SetAddrInfo(addrInfo AddrInfo, addr *resolver.Address) {
addr.Attributes = addr.Attributes.WithValues(attributeKey{}, addrInfo)
}

// GetAddrInfo returns the AddrInfo stored in the Attributes fields of addr.
// Returns nil if no AddrInfo is present.
func GetAddrInfo(addr *resolver.Address) *AddrInfo {
// This is an EXPERIMENTAL API.
func GetAddrInfo(addr *resolver.Address) AddrInfo {
if addr == nil || addr.Attributes == nil {
return nil
return AddrInfo{}
}
ai := addr.Attributes.Value(attributeKey{})
if ai == nil {
return nil
return AddrInfo{}
}
if _, ok := ai.(*AddrInfo); !ok {
return nil
if _, ok := ai.(AddrInfo); !ok {
return AddrInfo{}
}
return ai.(*AddrInfo)
return ai.(AddrInfo)
}
20 changes: 10 additions & 10 deletions balancer/weightedroundrobin/weightedwoundrobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,33 @@ import (
func TestAddAddrInfoToAndFromAttributes(t *testing.T) {
tests := []struct {
desc string
inputAddrInfo *AddrInfo
inputAddrInfo AddrInfo
inputAttributes *attributes.Attributes
wantAddrInfo *AddrInfo
wantAddrInfo AddrInfo
}{
{
desc: "empty attributes",
inputAddrInfo: &AddrInfo{Weight: 100},
inputAddrInfo: AddrInfo{Weight: 100},
inputAttributes: nil,
wantAddrInfo: &AddrInfo{Weight: 100},
wantAddrInfo: AddrInfo{Weight: 100},
},
{
desc: "non-empty attributes",
inputAddrInfo: &AddrInfo{Weight: 100},
inputAddrInfo: AddrInfo{Weight: 100},
inputAttributes: attributes.New("foo", "bar"),
wantAddrInfo: &AddrInfo{Weight: 100},
wantAddrInfo: AddrInfo{Weight: 100},
},
{
desc: "addrInfo not present in empty attributes",
inputAddrInfo: nil,
inputAddrInfo: AddrInfo{},
inputAttributes: nil,
wantAddrInfo: nil,
wantAddrInfo: AddrInfo{},
},
{
desc: "addrInfo not present in non-empty attributes",
inputAddrInfo: nil,
inputAddrInfo: AddrInfo{},
inputAttributes: attributes.New("foo", "bar"),
wantAddrInfo: nil,
wantAddrInfo: AddrInfo{},
},
}

Expand Down

0 comments on commit c3d0053

Please sign in to comment.