Skip to content

Commit

Permalink
Adjusted variable name to go style
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Jun 9, 2019
1 parent c161c2e commit 50cfb43
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 265 deletions.
2 changes: 1 addition & 1 deletion _example/import/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type importer struct {
}

func (f *importer) Import(db *trdsql.DDB, query string) (string, error) {
func (f *importer) Import(db *trdsql.DB, query string) (string, error) {
err := db.CreateTable("test", []string{"a"}, []string{"text"})
if err != nil {
return query, err
Expand Down
4 changes: 2 additions & 2 deletions _example/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ArrayTable struct {
table [][]string
}

func (a *ArrayTable) First(columns []string, types []string) error {
func (a *ArrayTable) PreWrite(columns []string, types []string) error {
a.table = make([][]string, 0)
fmt.Println(columns, types)
return nil
Expand All @@ -24,7 +24,7 @@ func (a *ArrayTable) WriteRow(values []interface{}, columns []string) error {
a.table = append(a.table, row)
return nil
}
func (a *ArrayTable) Last() error {
func (a *ArrayTable) PostWrite() error {
return nil
}

Expand Down
34 changes: 2 additions & 32 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (e *exporter) Export(db *DB, query string) error {
types[i] = ct.DatabaseTypeName()
}

err = e.Writer.First(columns, types)
err = e.Writer.PreWrite(columns, types)
if err != nil {
return err
}
Expand All @@ -90,7 +90,7 @@ func (e *exporter) Export(db *DB, query string) error {
return err
}
}
return e.Writer.Last()
return e.Writer.PostWrite()
}

func ConvertTypes(dbTypes []string) []string {
Expand Down Expand Up @@ -139,33 +139,3 @@ func ValString(v interface{}) string {
}
return str
}

// Writer is file format writer
type Writer interface {
First([]string, []string) error
WriteRow([]interface{}, []string) error
Last() error
}

func NewWriter(writeOpts WriteOpts) Writer {
switch writeOpts.OutFormat {
case LTSV:
return NewLTSVWrite(writeOpts)
case JSON:
return NewJSONWrite(writeOpts)
case RAW:
return NewRAWWrite(writeOpts)
case MD:
return NewTWWrite(writeOpts, true)
case AT:
return NewTWWrite(writeOpts, false)
case VF:
return NewVFWrite(writeOpts)
case TBLN:
return NewTBLNWrite(writeOpts)
case CSV:
return NewCSVWrite(writeOpts)
default:
return NewCSVWrite(writeOpts)
}
}
72 changes: 36 additions & 36 deletions input_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@ type CSVRead struct {
inHeader bool
}

func NewCSVReader(r io.Reader, opts ReadOpts) (Reader, error) {
func NewCSVReader(reader io.Reader, opts ReadOpts) (Reader, error) {
var err error

if opts.InHeader {
opts.InPreRead--
}
cr := &CSVRead{}
cr.reader = csv.NewReader(r)
cr.reader.LazyQuotes = true
cr.reader.FieldsPerRecord = -1 // no check count
cr.reader.TrimLeadingSpace = true
cr.inHeader = opts.InHeader
cr.reader.Comma, err = delimiter(opts.InDelimiter)
r := &CSVRead{}
r.reader = csv.NewReader(reader)
r.reader.LazyQuotes = true
r.reader.FieldsPerRecord = -1 // no check count
r.reader.TrimLeadingSpace = true
r.inHeader = opts.InHeader
r.reader.Comma, err = delimiter(opts.InDelimiter)

if opts.InSkip > 0 {
skip := make([]interface{}, 1)
for i := 0; i < opts.InSkip; i++ {
r, e := cr.ReadRow(skip)
if e != nil {
log.Printf("ERROR: skip error %s", e)
row, err := r.ReadRow(skip)
if err != nil {
log.Printf("ERROR: skip error %s", err)
break
}
debug.Printf("Skip row:%s\n", r)
debug.Printf("Skip row:%s\n", row)
}
}

return cr, err
return r, err
}

func delimiter(sepString string) (rune, error) {
Expand All @@ -60,65 +60,65 @@ func delimiter(sepString string) (rune, error) {

// GetColumn is reads the specified number of rows and determines the column name.
// The previously read row is stored in preRead.
func (cr *CSVRead) GetColumn(rowNum int) ([]string, error) {
func (r *CSVRead) GetColumn(rowNum int) ([]string, error) {
// Header
if cr.inHeader {
row, err := cr.reader.Read()
if r.inHeader {
row, err := r.reader.Read()
if err != nil {
return nil, err
}
cr.names = make([]string, len(row))
r.names = make([]string, len(row))
for i, col := range row {
if col == "" {
cr.names[i] = "c" + strconv.Itoa(i+1)
r.names[i] = "c" + strconv.Itoa(i+1)
} else {
cr.names[i] = col
r.names[i] = col
}
}
}

for n := 0; n < rowNum; n++ {
row, err := cr.reader.Read()
row, err := r.reader.Read()
if err != nil {
return cr.names, err
return r.names, err
}
rows := make([]string, len(row))
for i, col := range row {
rows[i] = col
if len(cr.names) < i+1 {
cr.names = append(cr.names, "c"+strconv.Itoa(i+1))
if len(r.names) < i+1 {
r.names = append(r.names, "c"+strconv.Itoa(i+1))
}
}
cr.preRead = append(cr.preRead, rows)
r.preRead = append(r.preRead, rows)
}
return cr.names, nil
return r.names, nil
}

// GetTypes is reads the specified number of rows and determines the column type.
func (cr *CSVRead) GetTypes() ([]string, error) {
cr.types = make([]string, len(cr.names))
for i := 0; i < len(cr.names); i++ {
cr.types[i] = DefaultDBType
func (r *CSVRead) GetTypes() ([]string, error) {
r.types = make([]string, len(r.names))
for i := 0; i < len(r.names); i++ {
r.types[i] = DefaultDBType
}
return cr.types, nil
return r.types, nil
}

// PreReadRow is returns only columns that store preread rows.
func (cr *CSVRead) PreReadRow() [][]interface{} {
rowNum := len(cr.preRead)
func (r *CSVRead) PreReadRow() [][]interface{} {
rowNum := len(r.preRead)
rows := make([][]interface{}, rowNum)
for n := 0; n < rowNum; n++ {
rows[n] = make([]interface{}, len(cr.names))
for i, f := range cr.preRead[n] {
rows[n] = make([]interface{}, len(r.names))
for i, f := range r.preRead[n] {
rows[n][i] = f
}
}
return rows
}

// ReadRow is read the rest of the row.
func (cr *CSVRead) ReadRow(row []interface{}) ([]interface{}, error) {
record, err := cr.reader.Read()
func (r *CSVRead) ReadRow(row []interface{}) ([]interface{}, error) {
record, err := r.reader.Read()
if err != nil {
return row, err
}
Expand Down
Loading

0 comments on commit 50cfb43

Please sign in to comment.