Skip to content

Commit

Permalink
Fix bug in provider schema where default int properties could not be int
Browse files Browse the repository at this point in the history
Fixes #6469
  • Loading branch information
Kyle Dixler committed Jul 26, 2023
1 parent e02a83c commit 88c7a61
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdkgen
description: Fix bug binding provider schema where type default int values could not take integers.
30 changes: 19 additions & 11 deletions pkg/codegen/schema/bind.go
Expand Up @@ -958,35 +958,43 @@ func bindConstValue(path, kind string, value interface{}, typ Type) (interface{}

switch typ = plainType(typ); typ {
case BoolType:
if _, ok := value.(bool); !ok {
v, ok := value.(bool)
if !ok {
return false, typeError("boolean")
}
return v, nil
case IntType:
v, ok := value.(float64)
v, ok := value.(int)
if !ok {
return 0, typeError("integer")
}
if math.Trunc(v) != v || v < math.MinInt32 || v > math.MaxInt32 {
return 0, typeError("integer")
v, ok := value.(float64)
if !ok {
return 0, typeError("integer")
}
if math.Trunc(v) != v || v < math.MinInt32 || v > math.MaxInt32 {
return 0, typeError("integer")
}
return int32(v), nil
}
value = int32(v)
return int32(v), nil
case NumberType:
if _, ok := value.(float64); !ok {
v, ok := value.(float64)
if !ok {
return 0.0, typeError("number")
}
return v, nil
case StringType:
if _, ok := value.(string); !ok {
v, ok := value.(string)
if !ok {
return 0.0, typeError("string")
}
return v, nil
default:
if _, isInvalid := typ.(*InvalidType); isInvalid {
return nil, nil
}
return nil, hcl.Diagnostics{errorf(path, "type %v cannot have a constant value; only booleans, integers, "+
"numbers and strings may have constant values", typ)}
}

return value, nil
}

func bindDefaultValue(path string, value interface{}, spec *DefaultSpec, typ Type) (*DefaultValue, hcl.Diagnostics) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/codegen/schema/schema_test.go
Expand Up @@ -957,3 +957,12 @@ func TestPackageIdentity(t *testing.T) {
})
}
}

func TestBindDefaultInt(t *testing.T) {
t.Parallel()
dv, diag := bindDefaultValue("fake-path", int(32), nil, IntType)
if diag.HasErrors() {
t.Fail()
}
assert.Equal(t, int32(32), dv.Value)
}

0 comments on commit 88c7a61

Please sign in to comment.