forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolver: introduce attributes package and use it for Address and Sta…
…te (grpc#3151)
- Loading branch information
Showing
4 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
// Package attributes defines a generic key/value store used in various gRPC | ||
// components. | ||
// | ||
// All APIs in this package are EXPERIMENTAL. | ||
package attributes | ||
|
||
import "fmt" | ||
|
||
// Attributes is an immutable struct for storing and retrieving generic | ||
// key/value pairs. Keys must be hashable, and users should define their own | ||
// types for keys. | ||
type Attributes struct { | ||
m map[interface{}]interface{} | ||
} | ||
|
||
// New returns a new Attributes containing all key/value pairs in kvs. If the | ||
// same key appears multiple times, the last value overwrites all previous | ||
// values for that key. Panics if len(kvs) is not even. | ||
func New(kvs ...interface{}) *Attributes { | ||
if len(kvs)%2 != 0 { | ||
panic(fmt.Sprintf("attributes.New called with unexpected input: len(kvs) = %v", len(kvs))) | ||
} | ||
a := &Attributes{m: make(map[interface{}]interface{}, len(kvs)/2)} | ||
for i := 0; i < len(kvs)/2; i++ { | ||
a.m[kvs[i*2]] = kvs[i*2+1] | ||
} | ||
return a | ||
} | ||
|
||
// WithValues returns a new Attributes containing all key/value pairs in a and | ||
// kvs. Panics if len(kvs) is not even. If the same key appears multiple | ||
// times, the last value overwrites all previous values for that key. To | ||
// remove an existing key, use a nil value. | ||
func (a *Attributes) WithValues(kvs ...interface{}) *Attributes { | ||
if len(kvs)%2 != 0 { | ||
panic(fmt.Sprintf("attributes.New called with unexpected input: len(kvs) = %v", len(kvs))) | ||
} | ||
n := &Attributes{m: make(map[interface{}]interface{}, len(a.m)+len(kvs)/2)} | ||
for k, v := range a.m { | ||
n.m[k] = v | ||
} | ||
for i := 0; i < len(kvs)/2; i++ { | ||
n.m[kvs[i*2]] = kvs[i*2+1] | ||
} | ||
return n | ||
} | ||
|
||
// 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{} { | ||
return a.m[key] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package attributes_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"google.golang.org/grpc/attributes" | ||
) | ||
|
||
func ExampleAttributes() { | ||
type keyOne struct{} | ||
type keyTwo struct{} | ||
a := attributes.New(keyOne{}, 1, keyTwo{}, "two") | ||
fmt.Println("Key one:", a.Value(keyOne{})) | ||
fmt.Println("Key two:", a.Value(keyTwo{})) | ||
// Output: | ||
// Key one: 1 | ||
// Key two: two | ||
} | ||
|
||
func ExampleAttributes_WithValues() { | ||
type keyOne struct{} | ||
type keyTwo struct{} | ||
a := attributes.New(keyOne{}, 1) | ||
a = a.WithValues(keyTwo{}, "two") | ||
fmt.Println("Key one:", a.Value(keyOne{})) | ||
fmt.Println("Key two:", a.Value(keyTwo{})) | ||
// Output: | ||
// Key one: 1 | ||
// Key two: two | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters