-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
530 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package filetypes | ||
|
||
import ( | ||
csvFile "github.com/cloudquery/filetypes/csv" | ||
jsonFile "github.com/cloudquery/filetypes/json" | ||
) | ||
|
||
type Client struct { | ||
spec *FileSpec | ||
csv *csvFile.Client | ||
json *jsonFile.Client | ||
csvTransformer *csvFile.Transformer | ||
csvReverseTransformer *csvFile.ReverseTransformer | ||
jsonTransformer *jsonFile.Transformer | ||
jsonReverseTransformer *jsonFile.ReverseTransformer | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
switch spec.Format { | ||
case FormatTypeCSV: | ||
opts := []csvFile.Options{ | ||
csvFile.WithDelimiter([]rune(spec.csvSpec.Delimiter)[0]), | ||
} | ||
if spec.csvSpec.IncludeHeaders { | ||
opts = append(opts, csvFile.WithHeader()) | ||
} | ||
|
||
client, err := csvFile.NewClient(opts...) | ||
if err != nil { | ||
return &Client{}, err | ||
} | ||
return &Client{ | ||
spec: spec, | ||
csvTransformer: &csvFile.Transformer{}, | ||
csvReverseTransformer: &csvFile.ReverseTransformer{}, | ||
csv: client, | ||
}, nil | ||
|
||
case FormatTypeJSON: | ||
client, err := jsonFile.NewClient() | ||
if err != nil { | ||
return &Client{}, err | ||
} | ||
return &Client{ | ||
spec: spec, | ||
jsonTransformer: &jsonFile.Transformer{}, | ||
jsonReverseTransformer: &jsonFile.ReverseTransformer{}, | ||
json: client, | ||
}, nil | ||
|
||
default: | ||
panic("unknown format " + spec.Format) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package csv | ||
|
||
import "fmt" | ||
|
||
type Spec struct { | ||
IncludeHeaders bool `json:"include_headers,omitempty"` | ||
Delimiter string `json:"delimiter,omitempty"` | ||
} | ||
|
||
func (s *Spec) SetDefaults() { | ||
if s.Delimiter == "" { | ||
s.Delimiter = "," | ||
} | ||
} | ||
|
||
func (s *Spec) Validate() error { | ||
if len(s.Delimiter) != 1 { | ||
return fmt.Errorf("delimiter must be a single character") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package json | ||
|
||
type Spec struct{} | ||
|
||
func (*Spec) SetDefaults() {} | ||
|
||
func (*Spec) Validate() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package filetypes | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/cloudquery/plugin-sdk/schema" | ||
) | ||
|
||
func (cl *Client) Read(r io.Reader, table *schema.Table, sourceName string, res chan<- []any) error { | ||
switch cl.spec.Format { | ||
case FormatTypeCSV: | ||
if err := cl.csv.Read(r, table, sourceName, res); err != nil { | ||
return err | ||
} | ||
case FormatTypeJSON: | ||
if err := cl.json.Read(r, table, sourceName, res); err != nil { | ||
return err | ||
} | ||
default: | ||
panic("unknown format " + cl.spec.Format) | ||
} | ||
return nil | ||
} | ||
|
||
func (cl *Client) ReverseTransformValues(table *schema.Table, values []any) (schema.CQTypes, error) { | ||
switch cl.spec.Format { | ||
case FormatTypeCSV: | ||
return cl.csvReverseTransformer.ReverseTransformValues(table, values) | ||
case FormatTypeJSON: | ||
return cl.jsonReverseTransformer.ReverseTransformValues(table, values) | ||
default: | ||
panic("unknown format " + cl.spec.Format) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package filetypes | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/cloudquery/filetypes/csv" | ||
jsonFile "github.com/cloudquery/filetypes/json" | ||
) | ||
|
||
type FormatType string | ||
|
||
const ( | ||
FormatTypeCSV = "csv" | ||
FormatTypeJSON = "json" | ||
) | ||
|
||
type FileSpec struct { | ||
Format FormatType `json:"format,omitempty"` | ||
FormatSpec any `json:"format_spec,omitempty"` | ||
csvSpec *csv.Spec | ||
jsonSpec *jsonFile.Spec | ||
} | ||
|
||
func (s *FileSpec) SetDefaults() { | ||
switch s.Format { | ||
case FormatTypeCSV: | ||
s.csvSpec.SetDefaults() | ||
case FormatTypeJSON: | ||
s.jsonSpec.SetDefaults() | ||
} | ||
} | ||
func (s *FileSpec) Validate() error { | ||
if s.Format == "" { | ||
return fmt.Errorf("format is required") | ||
} | ||
switch s.Format { | ||
case FormatTypeCSV: | ||
return s.csvSpec.Validate() | ||
case FormatTypeJSON: | ||
return s.jsonSpec.Validate() | ||
default: | ||
return fmt.Errorf("unknown format %s", s.Format) | ||
} | ||
} | ||
|
||
func (s *FileSpec) UnmarshalSpec() error { | ||
b, err := json.Marshal(s.FormatSpec) | ||
if err != nil { | ||
return err | ||
} | ||
dec := json.NewDecoder(bytes.NewReader(b)) | ||
dec.UseNumber() | ||
dec.DisallowUnknownFields() | ||
|
||
switch s.Format { | ||
case FormatTypeCSV: | ||
s.csvSpec = &csv.Spec{} | ||
return dec.Decode(s.csvSpec) | ||
case FormatTypeJSON: | ||
s.jsonSpec = &jsonFile.Spec{} | ||
return dec.Decode(s.jsonSpec) | ||
default: | ||
return fmt.Errorf("unknown format %s", s.Format) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package filetypes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cloudquery/filetypes/csv" | ||
"github.com/cloudquery/filetypes/json" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSpecMethods(t *testing.T) { | ||
testCases := []struct { | ||
FileSpec *FileSpec | ||
preDefaultsCSV *csv.Spec | ||
preDefaultsJSON *json.Spec | ||
postDefaultsCSV *csv.Spec | ||
postDefaultsJSON *json.Spec | ||
expectError bool | ||
}{ | ||
{ | ||
FileSpec: &FileSpec{ | ||
Format: FormatTypeCSV, | ||
FormatSpec: map[string]any{}, | ||
}, | ||
preDefaultsCSV: &csv.Spec{}, | ||
postDefaultsCSV: &csv.Spec{ | ||
IncludeHeaders: false, | ||
Delimiter: ",", | ||
}, | ||
}, | ||
{ | ||
FileSpec: &FileSpec{ | ||
Format: FormatTypeCSV, | ||
FormatSpec: map[string]any{ | ||
"delimiter": ",", | ||
"include_headers": true, | ||
}, | ||
}, | ||
preDefaultsCSV: &csv.Spec{ | ||
IncludeHeaders: true, | ||
Delimiter: ",", | ||
}, | ||
postDefaultsCSV: &csv.Spec{ | ||
IncludeHeaders: true, | ||
Delimiter: ",", | ||
}, | ||
}, | ||
{ | ||
FileSpec: &FileSpec{ | ||
Format: FormatTypeCSV, | ||
FormatSpec: map[string]any{}, | ||
}, | ||
preDefaultsCSV: &csv.Spec{ | ||
IncludeHeaders: false, | ||
Delimiter: "", | ||
}, | ||
postDefaultsCSV: &csv.Spec{ | ||
IncludeHeaders: false, | ||
Delimiter: ",", | ||
}, | ||
}, | ||
{ | ||
FileSpec: &FileSpec{ | ||
Format: FormatTypeJSON, | ||
}, | ||
preDefaultsJSON: &json.Spec{}, | ||
postDefaultsJSON: &json.Spec{}, | ||
}, | ||
|
||
{ | ||
FileSpec: &FileSpec{ | ||
Format: FormatTypeJSON, | ||
FormatSpec: map[string]any{ | ||
"delimiter": ",", | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
FileSpec: &FileSpec{ | ||
Format: FormatTypeCSV, | ||
FormatSpec: map[string]any{ | ||
"delimiter22": ",", | ||
}, | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
err := tc.FileSpec.UnmarshalSpec() | ||
if tc.expectError { | ||
assert.NotNil(t, err) | ||
continue | ||
} | ||
assert.Equal(t, tc.preDefaultsCSV, tc.FileSpec.csvSpec) | ||
assert.Equal(t, tc.preDefaultsJSON, tc.FileSpec.jsonSpec) | ||
|
||
tc.FileSpec.SetDefaults() | ||
|
||
assert.Equal(t, tc.postDefaultsCSV, tc.FileSpec.csvSpec) | ||
assert.Equal(t, tc.postDefaultsJSON, tc.FileSpec.jsonSpec) | ||
} | ||
} |
Oops, something went wrong.