From 17667a1c754a65bc69ad61a42bbaa5aa6ade8a8a Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" <4903+rboyer@users.noreply.github.com> Date: Tue, 22 Aug 2023 12:31:06 -0500 Subject: [PATCH] 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 --- internal/mesh/internal/types/decoded.go | 20 ++++++++++++++++++++ internal/resource/decode.go | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 internal/mesh/internal/types/decoded.go diff --git a/internal/mesh/internal/types/decoded.go b/internal/mesh/internal/types/decoded.go new file mode 100644 index 0000000000..90ce212333 --- /dev/null +++ b/internal/mesh/internal/types/decoded.go @@ -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] +) diff --git a/internal/resource/decode.go b/internal/resource/decode.go index c610898ca3..35ee088603 100644 --- a/internal/resource/decode.go +++ b/internal/resource/decode.go @@ -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. //