-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvalidators.go
53 lines (46 loc) · 1.2 KB
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package schema
import (
"encoding/json"
"github.com/apache/arrow-go/v18/arrow"
"github.com/cloudquery/plugin-sdk/v4/types"
)
func FindEmptyColumns(table *Table, records []arrow.Record) []string {
columnsWithValues := make([]bool, len(table.Columns))
emptyColumns := make([]string, 0)
for _, resource := range records {
for colIndex, arr := range resource.Columns() {
for i := 0; i < arr.Len(); i++ {
if arr.IsValid(i) {
if arrow.TypeEqual(arr.DataType(), types.ExtensionTypes.JSON) {
// JSON column shouldn't be empty
val := arr.GetOneForMarshal(i).(json.RawMessage)
if isEmptyJSON(val) {
continue
}
}
columnsWithValues[colIndex] = true
}
}
}
}
// Make sure every column has at least one value.
for i, hasValue := range columnsWithValues {
col := table.Columns[i]
emptyExpected := col.Name == "_cq_parent_id" && table.Parent == nil
if !hasValue && !emptyExpected && !col.IgnoreInTests {
emptyColumns = append(emptyColumns, col.Name)
}
}
return emptyColumns
}
func isEmptyJSON(msg json.RawMessage) bool {
if len(msg) == 0 {
return true
}
switch string(msg) {
case "null", "{}", "[]":
return true
default:
return false
}
}