-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
62 lines (50 loc) · 1.35 KB
/
client.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
54
55
56
57
58
59
60
61
62
package filetypes
import (
csvfile "github.com/cloudquery/filetypes/v4/csv"
jsonfile "github.com/cloudquery/filetypes/v4/json"
"github.com/cloudquery/filetypes/v4/parquet"
"github.com/cloudquery/filetypes/v4/types"
)
type Client struct {
spec *FileSpec
filetype types.FileType
}
var (
_ types.FileType = (*Client)(nil)
_ types.FileType = (*csvfile.Client)(nil)
_ types.FileType = (*jsonfile.Client)(nil)
_ types.FileType = (*parquet.Client)(nil)
)
// NewClient creates a new client for the given spec
func NewClient(spec *FileSpec) (*Client, error) {
err := spec.UnmarshalSpec()
if err != nil {
return &Client{}, err
}
spec.SetDefaults()
if err := spec.Validate(); err != nil {
return &Client{}, err
}
var client types.FileType
switch spec.Format {
case FormatTypeCSV:
opts := []csvfile.Options{
csvfile.WithDelimiter([]rune(spec.csvSpec.Delimiter)[0]),
}
if !spec.csvSpec.SkipHeader {
opts = append(opts, csvfile.WithHeader())
}
client, err = csvfile.NewClient(opts...)
case FormatTypeJSON:
client, err = jsonfile.NewClient()
case FormatTypeParquet:
client, err = parquet.NewClient(parquet.WithSpec(*spec.parquetSpec))
default:
// shouldn't be possible as Validate checks for type
panic("unknown format " + spec.Format)
}
if err != nil {
return nil, err
}
return &Client{spec: spec, filetype: client}, nil
}