mesh: adding type aliases for mesh resource usage (#18448)

Introduces some simple type aliases for DecodedResource[*X] wrappers for each type which cut down on the verbosity
This commit is contained in:
R.B. Boyer 2023-08-22 12:31:06 -05:00 committed by GitHub
parent 0d60380214
commit 17667a1c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,20 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package types
import (
"github.com/hashicorp/consul/internal/resource"
pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1"
)
type (
DecodedHTTPRoute = resource.DecodedResource[*pbmesh.HTTPRoute]
DecodedGRPCRoute = resource.DecodedResource[*pbmesh.GRPCRoute]
DecodedTCPRoute = resource.DecodedResource[*pbmesh.TCPRoute]
DecodedDestinationPolicy = resource.DecodedResource[*pbmesh.DestinationPolicy]
DecodedComputedRoutes = resource.DecodedResource[*pbmesh.ComputedRoutes]
DecodedFailoverPolicy = resource.DecodedResource[*pbcatalog.FailoverPolicy]
DecodedService = resource.DecodedResource[*pbcatalog.Service]
)

View File

@ -20,6 +20,23 @@ type DecodedResource[T proto.Message] struct {
Data T
}
func (d *DecodedResource[T]) GetResource() *pbresource.Resource {
if d == nil {
return nil
}
return d.Resource
}
func (d *DecodedResource[T]) GetData() T {
if d == nil {
var zero T
return zero
}
return d.Data
}
// Decode will generically decode the provided resource into a 2-field
// structure that holds onto the original Resource and the decoded contents.
//