Skip to content

Commit

Permalink
log error if 'name' is not a key in schema map (#3952)
Browse files Browse the repository at this point in the history
  • Loading branch information
megan07 authored Sep 8, 2020
1 parent ec816a3 commit 4df6b35
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions third_party/terraform/resources/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ import (
"google.golang.org/api/bigquery/v2"
)

func checkNameExists(jsonList []interface{}) error {
for _, m := range jsonList {
if _, ok := m.(map[string]interface{})["name"]; !ok {
return fmt.Errorf("No name in schema %+v", m)
}
}

return nil
}

// JSONBytesEqual compares the JSON in two byte slices.
// Reference: https://stackoverflow.com/questions/32408890/how-to-compare-two-json-requests
func JSONBytesEqual(a, b []byte) (bool, error) {
Expand All @@ -22,13 +32,19 @@ func JSONBytesEqual(a, b []byte) (bool, error) {
return false, err
}
jList := j.([]interface{})
if err := checkNameExists(jList); err != nil {
return false, err
}
sort.Slice(jList, func(i, k int) bool {
return jList[i].(map[string]interface{})["name"].(string) < jList[k].(map[string]interface{})["name"].(string)
})
if err := json.Unmarshal(b, &j2); err != nil {
return false, err
}
j2List := j2.([]interface{})
if err := checkNameExists(j2List); err != nil {
return false, err
}
sort.Slice(j2List, func(i, k int) bool {
return j2List[i].(map[string]interface{})["name"].(string) < j2List[k].(map[string]interface{})["name"].(string)
})
Expand All @@ -42,6 +58,7 @@ func bigQueryTableSchemaDiffSuppress(_, old, new string, _ *schema.ResourceData)

eq, err := JSONBytesEqual(oldBytes, newBytes)
if err != nil {
log.Printf("[DEBUG] %v", err)
log.Printf("[DEBUG] Error comparing JSON bytes: %v, %v", old, new)
}

Expand Down

0 comments on commit 4df6b35

Please sign in to comment.