Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions internal/resource/decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package resource

import (
"google.golang.org/protobuf/proto"

"github.com/hashicorp/consul/proto-public/pbresource"
)

// DecodedResource is a generic holder to contain an original Resource and its
// decoded contents.
type DecodedResource[V any, PV interface {
proto.Message
*V
}] struct {
Resource *pbresource.Resource
Data PV
}

// Decode will generically decode the provided resource into a 2-field
// structure that holds onto the original Resource and the decoded contents.
//
// Returns an ErrDataParse on unmarshalling errors.
func Decode[V any, PV interface {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

proto.Message
*V
}](res *pbresource.Resource) (*DecodedResource[V, PV], error) {
data := PV(new(V))
if err := res.Data.UnmarshalTo(data); err != nil {
return nil, NewErrDataParse(data, err)
}
return &DecodedResource[V, PV]{
Resource: res,
Data: data,
}, nil
}
66 changes: 66 additions & 0 deletions internal/resource/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package resource_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"

"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo"
"github.com/hashicorp/consul/proto-public/pbresource"
pbdemo "github.com/hashicorp/consul/proto/private/pbdemo/v2"
"github.com/hashicorp/consul/proto/private/prototest"
)

func TestDecode(t *testing.T) {
t.Run("good", func(t *testing.T) {
fooData := &pbdemo.Artist{
Name: "caspar babypants",
}
any, err := anypb.New(fooData)
require.NoError(t, err)

foo := &pbresource.Resource{
Id: &pbresource.ID{
Type: demo.TypeV2Artist,
Tenancy: demo.TenancyDefault,
Name: "babypants",
},
Data: any,
Metadata: map[string]string{
"generated_at": time.Now().Format(time.RFC3339),
},
}

dec, err := resource.Decode[pbdemo.Artist, *pbdemo.Artist](foo)
require.NoError(t, err)

prototest.AssertDeepEqual(t, foo, dec.Resource)
prototest.AssertDeepEqual(t, fooData, dec.Data)
})

t.Run("bad", func(t *testing.T) {
foo := &pbresource.Resource{
Id: &pbresource.ID{
Type: demo.TypeV2Artist,
Tenancy: demo.TenancyDefault,
Name: "babypants",
},
Data: &anypb.Any{
TypeUrl: "garbage",
Value: []byte("more garbage"),
},
Metadata: map[string]string{
"generated_at": time.Now().Format(time.RFC3339),
},
}

_, err := resource.Decode[pbdemo.Artist, *pbdemo.Artist](foo)
require.Error(t, err)
})
}
12 changes: 12 additions & 0 deletions internal/resource/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ func EqualReference(a, b *pbresource.Reference) bool {
a.Section == b.Section
}

// ReferenceOrIDMatch compares two references or IDs to see if they both refer
// to the same thing.
func ReferenceOrIDMatch(ref1, ref2 ReferenceOrID) bool {
if ref1 == nil || ref2 == nil {
return false
}

return EqualType(ref1.GetType(), ref2.GetType()) &&
EqualTenancy(ref1.GetTenancy(), ref2.GetTenancy()) &&
ref1.GetName() == ref2.GetName()
}

// EqualStatusMap compares two status maps for equality without reflection.
func EqualStatusMap(a, b map[string]*pbresource.Status) bool {
if len(a) != len(b) {
Expand Down
Loading