mirror of https://github.com/grpc/grpc-go.git
57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
/*
|
|
*
|
|
* 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 internal contains functions/structs shared by xds
|
|
// balancers/resolvers.
|
|
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// LocalityID is xds.Locality without XXX fields, so it can be used as map
|
|
// keys.
|
|
//
|
|
// xds.Locality cannot be map keys because one of the XXX fields is a slice.
|
|
type LocalityID struct {
|
|
Region string `json:"region,omitempty"`
|
|
Zone string `json:"zone,omitempty"`
|
|
SubZone string `json:"subZone,omitempty"`
|
|
}
|
|
|
|
// String generates a string representation of LocalityID by adding ":" between
|
|
// the components of the LocalityID.
|
|
func (l LocalityID) String() string {
|
|
return fmt.Sprintf("%s:%s:%s", l.Region, l.Zone, l.SubZone)
|
|
}
|
|
|
|
// LocalityIDFromString converts a string representation of locality, of the
|
|
// form region:zone:sub-zone (as generated by the above String() method), into a
|
|
// LocalityID struct.
|
|
func LocalityIDFromString(l string) (LocalityID, error) {
|
|
parts := strings.Split(l, ":")
|
|
if len(parts) != 3 {
|
|
return LocalityID{}, fmt.Errorf("%s is not a well formatted locality ID", l)
|
|
}
|
|
return LocalityID{
|
|
Region: parts[0],
|
|
Zone: parts[1],
|
|
SubZone: parts[2],
|
|
}, nil
|
|
}
|