sq/drivers/csv/detect_field_kinds.go
2023-04-09 19:29:13 -06:00

40 lines
844 B
Go

package csv
import (
"github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind"
)
// detectColKinds detects the kinds of recs' columns.
func detectColKinds(recs [][]string) ([]kind.Kind, []kind.MungeFunc, error) {
if len(recs) == 0 || len(recs[0]) == 0 {
return nil, nil, errz.New("no records")
}
fieldCount := len(recs[0])
detectors := make([]*kind.Detector, len(recs[0]))
for i := 0; i < fieldCount; i++ {
detectors[i] = kind.NewDetector()
}
for i := range recs {
for j := range recs[i] {
detectors[j].Sample(recs[i][j])
}
}
kinds := make([]kind.Kind, fieldCount)
mungers := make([]kind.MungeFunc, fieldCount)
var err error
for i := range detectors {
kinds[i], mungers[i], err = detectors[i].Detect()
if err != nil {
return nil, nil, err
}
}
return kinds, mungers, nil
}