Skip to content

Commit

Permalink
Use ResourceQuantity
Browse files Browse the repository at this point in the history
Signed-off-by: ItalyPaleAle <[email protected]>
  • Loading branch information
ItalyPaleAle committed Aug 3, 2023
1 parent 4fb9f84 commit 8c59285
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
15 changes: 5 additions & 10 deletions bindings/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import (
"strings"
"time"

"k8s.io/apimachinery/pkg/api/resource"

"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/internal/utils"
"github.com/dapr/components-contrib/metadata"
Expand Down Expand Up @@ -74,7 +72,7 @@ type httpMetadata struct {
// This can either be an integer which is interpreted in bytes, or a string with an added unit such as Mi.
// A value <= 0 means no limit.
// Default: 100MB
MaxResponseBodySize *resource.Quantity `mapstructure:"maxResponseBodySize"`
MaxResponseBodySize metadata.ResourceQuantity `mapstructure:"maxResponseBodySize"`

maxResponseBodySizeBytes int64
}
Expand All @@ -89,7 +87,7 @@ func NewHTTP(logger logger.Logger) bindings.OutputBinding {
// Init performs metadata parsing.
func (h *HTTPSource) Init(_ context.Context, meta bindings.Metadata) error {
h.metadata = httpMetadata{
MaxResponseBodySize: resource.NewQuantity(defaultMaxResponseBodySizeBytes, resource.BinarySI),
MaxResponseBodySize: metadata.NewResourceQuantityBytes(defaultMaxResponseBodySizeBytes),
}
err := metadata.DecodeMetadata(meta.Properties, &h.metadata)
if err != nil {
Expand All @@ -113,12 +111,9 @@ func (h *HTTPSource) Init(_ context.Context, meta bindings.Metadata) error {
}
}

if h.metadata.MaxResponseBodySize != nil && !h.metadata.MaxResponseBodySize.IsZero() {
val, ok := h.metadata.MaxResponseBodySize.AsInt64()
if !ok {
return fmt.Errorf("value for maxResponseBodySize cannot be converted to integer: %v", h.metadata.MaxResponseBodySize)
}
h.metadata.maxResponseBodySizeBytes = val
h.metadata.maxResponseBodySizeBytes, err = h.metadata.MaxResponseBodySize.GetBytes()
if err != nil {
return fmt.Errorf("invalid value for maxResponseBodySize: %w", err)
}

// See guidance on proper HTTP client settings here:
Expand Down
39 changes: 34 additions & 5 deletions metadata/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,37 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
)

func toResourceHookFunc() mapstructure.DecodeHookFunc {
quantityType := reflect.TypeOf(resource.Quantity{})
quantityPtrType := reflect.TypeOf(&resource.Quantity{})
// ResourceQuantity contains a quantity for a resource, such as data size.
// This extends the resource.Quantity struct from k8s.io/apimachinery to add some utility methods specific for Dapr.
type ResourceQuantity struct {
resource.Quantity
}

// NewResourceQuantityBytes returns a new ResourceQuantity with a default value in bytes.
func NewResourceQuantityBytes(defaultBytesValue int64) ResourceQuantity {
return ResourceQuantity{
Quantity: *resource.NewQuantity(defaultBytesValue, resource.BinarySI),
}
}

// GetBytes returns the number of bytes in the quantity.
// Note: this operation is expensive, so it's recommended to cache the returned value.
func (q *ResourceQuantity) GetBytes() (int64, error) {
if q == nil || q.IsZero() {
return 0, nil
}

val, ok := q.AsInt64()
if !ok {
return 0, fmt.Errorf("cannot get bytes from resource quantity value '%v'", q)
}

return val, nil
}

func toResourceQuantityHookFunc() mapstructure.DecodeHookFunc {
quantityType := reflect.TypeOf(ResourceQuantity{})
quantityPtrType := reflect.TypeOf(&ResourceQuantity{})

return func(
f reflect.Type,
Expand Down Expand Up @@ -55,9 +83,10 @@ func toResourceHookFunc() mapstructure.DecodeHookFunc {
}

// Return a pointer if desired
res := ResourceQuantity{Quantity: q}
if isPtr {
return &q, nil
return &res, nil
}
return q, nil
return res, nil
}
}
2 changes: 1 addition & 1 deletion metadata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func DecodeMetadata(input any, result any) error {
toTimeDurationHookFunc(),
toTruthyBoolHookFunc(),
toStringArrayHookFunc(),
toResourceHookFunc(),
toResourceQuantityHookFunc(),
),
Metadata: nil,
Result: result,
Expand Down
15 changes: 7 additions & 8 deletions metadata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestIsRawPayload(t *testing.T) {
Expand Down Expand Up @@ -241,13 +240,13 @@ func TestMetadataDecode(t *testing.T) {

t.Run("Test metadata decode hook for resources", func(t *testing.T) {
type testMetadata struct {
ResourceValue1 resource.Quantity
ResourceValue2 resource.Quantity
ResourceValue3 resource.Quantity
ResourceValue4 resource.Quantity
ResourceValueNotProvided resource.Quantity
ResourceValuePtr *resource.Quantity
ResourceValuePtrNotProvided *resource.Quantity
ResourceValue1 ResourceQuantity
ResourceValue2 ResourceQuantity
ResourceValue3 ResourceQuantity
ResourceValue4 ResourceQuantity
ResourceValueNotProvided ResourceQuantity
ResourceValuePtr *ResourceQuantity
ResourceValuePtrNotProvided *ResourceQuantity
}

var m testMetadata
Expand Down

0 comments on commit 8c59285

Please sign in to comment.