Skip to content

Commit

Permalink
Update Marshal func to use VT
Browse files Browse the repository at this point in the history
  • Loading branch information
krhitesh7 committed Aug 23, 2024
1 parent 9d85983 commit 3b46674
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 4 additions & 0 deletions pkg/cache/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
// Resource is the base interface for the xDS payload.
type Resource interface {
proto.Message
}

type VtResource interface {
Resource
MarshalVTStrict() ([]byte, error)
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/cache/v3/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"google.golang.org/protobuf/proto"
"reflect"

cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
runtime "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"google.golang.org/protobuf/proto"
)

// GetResponseType returns the enumeration for a valid xDS type URL.
Expand Down Expand Up @@ -126,12 +124,14 @@ func GetResourceWithTTLNames(resources []types.ResourceWithTTL) []string {

// MarshalResource converts the Resource to MarshaledResource.
func MarshalResource(resource types.Resource) (types.MarshaledResource, error) {
t := reflect.TypeOf(resource)
_, ok := t.MethodByName("MarshalVTStrict")
if !ok {
return proto.MarshalOptions{Deterministic: true}.Marshal(resource)
switch v := resource.(type) {
case types.VtResource:
return v.MarshalVTStrict()
case types.Resource:
return proto.MarshalOptions{Deterministic: true}.Marshal(v)
default:
return nil, fmt.Errorf("failed to marshal, message is %T, must satisfy the vtprotoMessage interface or want proto.Message", v)
}
return resource.MarshalVTStrict()
}

// GetResourceReferences returns a map of dependent resources keyed by resource type, given a map of resources.
Expand Down

0 comments on commit 3b46674

Please sign in to comment.