Skip to content

Commit

Permalink
Add extension support for text format
Browse files Browse the repository at this point in the history
Add support for determining text format by extension.
  • Loading branch information
noborus committed Nov 28, 2024
1 parent 577f8b2 commit 99a9513
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ func strToFormat(format string) trdsql.Format {
return trdsql.TBLN
case "width":
return trdsql.WIDTH
case "text":
return trdsql.TEXT
default:
return trdsql.GUESS
}
Expand Down Expand Up @@ -361,9 +363,9 @@ func init() {
rootCmd.PersistentFlags().Var(&inNull, "null", "value(string) to convert to null on input.")
rootCmd.PersistentFlags().BoolVarP(&inRowNumber, "row-number", "n", false, "add row number.")

rootCmd.PersistentFlags().StringVarP(&inFormat, "in", "i", "GUESS", "format for input. [CSV|LTSV|JSON|YAML|TBLN|WIDTH]")
rootCmd.PersistentFlags().StringVarP(&inFormat, "in", "i", "GUESS", "format for input. [CSV|LTSV|JSON|YAML|TBLN|WIDTH|TEXT]")
rootCmd.RegisterFlagCompletionFunc("in", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"CSV", "LTSV", "JSON", "YAML", "TBLN", "WIDTH"}, cobra.ShellCompDirectiveDefault
return []string{"CSV", "LTSV", "JSON", "YAML", "TBLN", "WIDTH", "TEXT"}, cobra.ShellCompDirectiveDefault
})
rootCmd.PersistentFlags().StringVar(&outDelimiter, "out-delimiter", ",", "field delimiter for output.")
rootCmd.PersistentFlags().StringVar(&outFile, "out-file", "", "output file name.")
Expand Down
4 changes: 4 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var extToFormat map[string]Format = map[string]Format{
"TSV": TSV,
"PSV": PSV,
"WIDTH": WIDTH,
"TEXT": TEXT,
}

// ReaderFunc is a function that creates a new Reader.
Expand Down Expand Up @@ -49,6 +50,9 @@ var readerFuncs = map[Format]ReaderFunc{
WIDTH: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewGWReader(reader, opts)
},
TEXT: func(reader io.Reader, opts *ReadOpts) (Reader, error) {
return NewTextReader(reader, opts)

Check failure on line 54 in reader.go

View workflow job for this annotation

GitHub Actions / dbtest

undefined: NewTextReader

Check failure on line 54 in reader.go

View workflow job for this annotation

GitHub Actions / build (oldstable, ubuntu-latest)

undefined: NewTextReader

Check failure on line 54 in reader.go

View workflow job for this annotation

GitHub Actions / build (oldstable, macos-latest)

undefined: NewTextReader

Check failure on line 54 in reader.go

View workflow job for this annotation

GitHub Actions / build (stable, ubuntu-latest)

undefined: NewTextReader
},
}

var (
Expand Down
5 changes: 5 additions & 0 deletions trdsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ const (
// Format using guesswidth library.
WIDTH

// import
TEXT

// export
// Output as it is.
// Multiple characters can be selected as delimiter.
Expand Down Expand Up @@ -142,6 +145,8 @@ func (f Format) String() string {
return "PSV"
case YAML:
return "YAML"
case TEXT:
return "TEXT"
default:
return "Unknown"
}
Expand Down
29 changes: 29 additions & 0 deletions trdsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,35 @@ func TestTBLNRun(t *testing.T) {
}
}

func TestTextRun(t *testing.T) {
ctx := context.Background()
testText := [][]string{
{"test.csv", `1,"1,Orange"
2,"2,Melon"
3,"3,Apple"
`},
{"aiu.csv", "1,あ\n2,い\n3,う\n"},
}
outStream := new(bytes.Buffer)
importer := NewImporter(
InFormat(TEXT),
InRowNumber(true),
)
exporter := NewExporter(NewWriter(OutStream(outStream)))
trd := NewTRDSQL(importer, exporter)
for _, c := range testText {
sqlQuery := "SELECT * FROM " + filepath.Join(dataDir, c[0])
err := trd.Exec(ctx, sqlQuery)
if err != nil {
t.Errorf("trdsql error %s", err)
}
if outStream.String() != c[1] {
t.Fatalf("trdsql error %s:%s:%s", c[0], c[1], outStream)
}
outStream.Reset()
}
}

func setOutFormatTRDSQL(outFormat Format, outStream io.Writer) *TRDSQL {
importer := NewImporter(
InFormat(GUESS),
Expand Down

0 comments on commit 99a9513

Please sign in to comment.