Skip to content

Commit

Permalink
log error if 'name' is not a key in schema map (#3952) (#7215)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 8, 2020
1 parent be16160 commit 8f0eea7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3952.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fixed a bug when a big query table schema didn't have `name` in the schema it'd panic, now logs an error.
```
17 changes: 17 additions & 0 deletions google/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 8f0eea7

Please sign in to comment.