Repository: grpc/grpc-go
File: internal/xds/xdsclient/xdsresource/unmarshal_eds.go (Go)

Code before:
	"google.golang.org/grpc/internal/envconfig"
	"google.golang.org/grpc/internal/pretty"
	xdsinternal "google.golang.org/grpc/internal/xds"
	"google.golang.org/grpc/internal/xds/clients"
	"google.golang.org/grpc/resolver"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

// hostnameKeyType is the key to store the hostname attribute in
// a resolver.Endpoint.
type hostnameKeyType struct{}

// setHostname returns a copy of the given endpoint with hostname added
// as an attribute.
func setHostname(endpoint resolver.Endpoint, hostname string) resolver.Endpoint {
	// Only set if non-empty; xds_cluster_impl uses this to trigger :authority
	// rewriting.
	if hostname == "" {
		return endpoint
	}
	endpoint.Attributes = endpoint.Attributes.WithValue(hostnameKeyType{}, hostname)
	return endpoint
}

// Hostname returns the hostname BalancerAttributes of Address.
// If this attribute is not set, it returns the empty string.
func Hostname(addr resolver.Address) string {
	hostname, _ := addr.BalancerAttributes.Value(hostnameKeyType{}).(string)
	return hostname
}

func unmarshalEndpointsResource(r *anypb.Any) (string, EndpointsUpdate, error) {
	r, err := UnwrapResource(r)
	if err != nil {
		return "", EndpointsUpdate{}, fmt.Errorf("failed to unwrap resource: %v", err)
	}

	if !IsEndpointsResource(r.GetTypeUrl()) {
		return "", EndpointsUpdate{}, fmt.Errorf("unexpected resource type: %q ", r.GetTypeUrl())
	}

	cla := &v3endpointpb.ClusterLoadAssignment{}
	if err := proto.Unmarshal(r.GetValue(), cla); err != nil {
		return "", EndpointsUpdate{}, fmt.Errorf("failed to unmarshal resource: %v", err)
	}

	u, err := parseEDSRespProto(cla)
	if err != nil {
		return cla.GetClusterName(), EndpointsUpdate{}, err
	}

Review comment (refactor):
nit: The phrasing here is a bit off. Since hostname is a single value extracted from the set of attributes, it is clearer to use 'from'. Suggestion: `Hostname returns the hostname from the BalancerAttributes of the given Address`.

Code after:
	"google.golang.org/grpc/internal/envconfig"
	"google.golang.org/grpc/internal/pretty"
	xdsinternal "google.golang.org/grpc/internal/xds"
	"google.golang.org/grpc/internal/xds/clients"
	"google.golang.org/grpc/resolver"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

// hostnameKeyType is the key to store the hostname attribute in
// a resolver.Endpoint.
type hostnameKeyType struct{}

// setHostname returns a copy of the given endpoint with hostname added
// as an attribute.
func setHostname(endpoint resolver.Endpoint, hostname string) resolver.Endpoint {
	// Only set if non-empty; xds_cluster_impl uses this to trigger :authority
	// rewriting.
	if hostname == "" {
		return endpoint
	}
	endpoint.Attributes = endpoint.Attributes.WithValue(hostnameKeyType{}, hostname)
	return endpoint
}

// Hostname returns the hostname from the BalancerAttributes of the given
// Address. If this attribute is not set, it returns the empty string.
func Hostname(addr resolver.Address) string {
	hostname, _ := addr.BalancerAttributes.Value(hostnameKeyType{}).(string)
	return hostname
}

func unmarshalEndpointsResource(r *anypb.Any) (string, EndpointsUpdate, error) {
	r, err := UnwrapResource(r)
	if err != nil {
		return "", EndpointsUpdate{}, fmt.Errorf("failed to unwrap resource: %v", err)
	}

	if !IsEndpointsResource(r.GetTypeUrl()) {
		return "", EndpointsUpdate{}, fmt.Errorf("unexpected resource type: %q ", r.GetTypeUrl())
	}

	cla := &v3endpointpb.ClusterLoadAssignment{}
	if err := proto.Unmarshal(r.GetValue(), cla); err != nil {
		return "", EndpointsUpdate{}, fmt.Errorf("failed to unmarshal resource: %v", err)
	}

	u, err := parseEDSRespProto(cla)
	if err != nil {
		return cla.GetClusterName(), EndpointsUpdate{}, err
	}