-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from nao1215/feat-import-tsv
Feat import tsv
- Loading branch information
Showing
15 changed files
with
197 additions
and
23 deletions.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
package model | ||
|
||
// Header is CSV/TSV/Table header. | ||
type Header []string | ||
|
||
// Record is CSV/TSV/Table records. | ||
type Record []string |
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,35 @@ | ||
// Package model defines Data Transfer Object (Entity, Value Object) | ||
package model | ||
|
||
import "strings" | ||
|
||
// TSV is tsv data with header. | ||
type TSV struct { | ||
Name string | ||
Header Header | ||
Records []Record | ||
} | ||
|
||
// IsHeaderEmpty return wherther header is empty or not | ||
func (t *TSV) IsHeaderEmpty() bool { | ||
return len(t.Header) == 0 | ||
} | ||
|
||
// SetHeader set header column. | ||
func (t *TSV) SetHeader(header Header) { | ||
t.Header = append(t.Header, header...) | ||
} | ||
|
||
// SetRecord set tsv record. | ||
func (t *TSV) SetRecord(record Record) { | ||
t.Records = append(t.Records, record) | ||
} | ||
|
||
// ToTable convert TSV to Table. | ||
func (t *TSV) ToTable() *Table { | ||
return &Table{ | ||
Name: strings.TrimSuffix(t.Name, ".tsv"), | ||
Header: t.Header, | ||
Records: t.Records, | ||
} | ||
} |
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,13 @@ | ||
package repository | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/nao1215/sqly/domain/model" | ||
) | ||
|
||
// TSVRepository is a repository that handles TSV file. | ||
type TSVRepository interface { | ||
// List get tsv all data with header. | ||
List(tsv *os.File) (*model.TSV, error) | ||
} |
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,43 @@ | ||
package persistence | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/nao1215/sqly/domain/model" | ||
"github.com/nao1215/sqly/domain/repository" | ||
) | ||
|
||
type tsvRepository struct{} | ||
|
||
// NewTSVRepository return TSVRepository | ||
func NewTSVRepository() repository.TSVRepository { | ||
return &tsvRepository{} | ||
} | ||
|
||
// List return tsv all record. | ||
func (tr *tsvRepository) List(f *os.File) (*model.TSV, error) { | ||
r := csv.NewReader(f) | ||
r.Comma = '\t' | ||
|
||
t := model.TSV{ | ||
Name: filepath.Base(f.Name()), | ||
} | ||
for { | ||
row, err := r.Read() | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
if t.IsHeaderEmpty() { | ||
t.SetHeader(row) | ||
continue | ||
} | ||
t.SetRecord(row) | ||
} | ||
return &t, 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
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,35 @@ | ||
package usecase | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/nao1215/sqly/domain/model" | ||
"github.com/nao1215/sqly/domain/repository" | ||
) | ||
|
||
// TSVInteractor implementation of use cases related to TSV handler. | ||
type TSVInteractor struct { | ||
Repository repository.TSVRepository | ||
} | ||
|
||
// NewTSVInteractor return TSVInteractor | ||
func NewTSVInteractor(r repository.TSVRepository) *TSVInteractor { | ||
return &TSVInteractor{Repository: r} | ||
} | ||
|
||
// List get TSV data. | ||
// The sqly command does not open many TSV files. Therefore, the file is | ||
// opened and closed in the usecase layer without worrying about processing speed. | ||
func (ti *TSVInteractor) List(TSVFilePath string) (*model.TSV, error) { | ||
f, err := os.Open(TSVFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
TSV, err := ti.Repository.List(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return TSV, nil | ||
} |