Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource: adding various helpers for working with resources #18342

Merged
merged 5 commits into from
Aug 1, 2023
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
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