core: Avoid crash on empty TypeSet blocks (#14305)

This commit is contained in:
Radek Simko 2017-05-09 20:45:53 +02:00 committed by GitHub
parent 35fc047b69
commit 44a99e0ae5
2 changed files with 20 additions and 0 deletions

View File

@ -85,6 +85,9 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) {
// to hash complex substructures when used in sets, and so the serialization
// is not reversible.
func SerializeResourceForHash(buf *bytes.Buffer, val interface{}, resource *Resource) {
if val == nil {
return
}
sm := resource.Schema
m := val.(map[string]interface{})
var keys []string

View File

@ -111,3 +111,20 @@ func TestSetUnion(t *testing.T) {
func testSetInt(v interface{}) int {
return v.(int)
}
func TestHashResource_nil(t *testing.T) {
resource := &Resource{
Schema: map[string]*Schema{
"name": {
Type: TypeString,
Optional: true,
},
},
}
f := HashResource(resource)
idx := f(nil)
if idx != 0 {
t.Fatalf("Expected 0 when hashing nil, given: %d", idx)
}
}