Skip to content

Commit

Permalink
Merge pull request #35 from PixarV/C2DEVEL-14751
Browse files Browse the repository at this point in the history
paas: fix build for 32-bit arch
  • Loading branch information
vamping111 authored Jan 18, 2024
2 parents 467e59d + 165f500 commit 3097870
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 106 deletions.
37 changes: 33 additions & 4 deletions internal/experimental/nullable/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nullable

import (
"fmt"
"math"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -51,7 +52,7 @@ func ValidateTypeStringNullableInt(v interface{}, k string) (ws []string, es []e

// ValidateTypeStringNullableIntAtLeast provides custom error messaging for TypeString ints
// Some arguments require an int value or unspecified, empty field.
func ValidateTypeStringNullableIntAtLeast(min int) schema.SchemaValidateFunc {
func ValidateTypeStringNullableIntAtLeast(min int64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (ws []string, es []error) {
value, ok := i.(string)
if !ok {
Expand All @@ -69,7 +70,7 @@ func ValidateTypeStringNullableIntAtLeast(min int) schema.SchemaValidateFunc {
return
}

if v < int64(min) {
if v < min {
es = append(es, fmt.Errorf("expected %s to be at least (%d), got %d", k, min, v))
}

Expand All @@ -79,7 +80,7 @@ func ValidateTypeStringNullableIntAtLeast(min int) schema.SchemaValidateFunc {

// ValidateTypeStringNullableIntBetween provides custom error messaging for TypeString ints
// Some arguments require an int value or unspecified, empty field.
func ValidateTypeStringNullableIntBetween(min int, max int) schema.SchemaValidateFunc {
func ValidateTypeStringNullableIntBetween(min int64, max int64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (ws []string, es []error) {
value, ok := i.(string)
if !ok {
Expand All @@ -97,10 +98,38 @@ func ValidateTypeStringNullableIntBetween(min int, max int) schema.SchemaValidat
return
}

if v < int64(min) || v > int64(max) {
if v < min || v > max {
es = append(es, fmt.Errorf("expected %s to be at between (%d) and (%d), got %d", k, min, max, v))
}

return
}
}

// ValidateTypeStringNullableIntDivisibleBy returns a SchemaValidateFunc which tests if the provided value
// is of type string, can be converted to int and is divisible by a given number.
func ValidateTypeStringNullableIntDivisibleBy(divisor int64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (ws []string, es []error) {
value, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

if value == "" {
return
}

v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
es = append(es, fmt.Errorf("%s: cannot parse '%s' as int: %w", k, value, err))
return
}

if math.Mod(float64(v), float64(divisor)) != 0 {
es = append(es, fmt.Errorf("expected %s to be divisible by %d, got: %v", k, divisor, i))
}

return
}
}
28 changes: 28 additions & 0 deletions internal/experimental/nullable/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,31 @@ func TestValidationIntAtLeast(t *testing.T) {
},
})
}

func TestValidationIntDivisibleBy(t *testing.T) {
runTestCases(t, []testCase{
{
val: "",
f: ValidateTypeStringNullableIntDivisibleBy(2),
},
{
val: "16",
f: ValidateTypeStringNullableIntDivisibleBy(8),
},
{
val: "16",
f: ValidateTypeStringNullableIntDivisibleBy(7),
expectedErr: regexp.MustCompile(`expected [\w]+ to be divisible by 7, got: 16`),
},
{
val: "words",
f: ValidateTypeStringNullableIntDivisibleBy(2),
expectedErr: regexp.MustCompile(`[\w]+: cannot parse 'words' as int: .*`),
},
{
val: 1,
f: ValidateTypeStringNullableIntAtLeast(2),
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
},
})
}
Loading

0 comments on commit 3097870

Please sign in to comment.