Skip to content

Commit

Permalink
Reduce if statements in loops
Browse files Browse the repository at this point in the history
Improved the use of `if` statements for the first column
when outputting delimiters.
  • Loading branch information
noborus committed Aug 6, 2024
1 parent 79e3a38 commit 0b39c15
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 42 deletions.
31 changes: 21 additions & 10 deletions output_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ func (w *CSVWriter) PreWrite(columns []string, types []string) error {
if !w.outHeader {
return nil
}
for n, column := range columns {
if n > 0 {
if _, err := w.writer.WriteRune(w.outDelimiter); err != nil {
return err
}
if len(columns) == 0 {
return nil
}
if err := w.writeColumnString(columns[0]); err != nil {
return err
}

for _, column := range columns[1:] {
if _, err := w.writer.WriteRune(w.outDelimiter); err != nil {
return err
}
if err := w.writeColumnString(column); err != nil {
return err
Expand All @@ -70,11 +75,17 @@ func (w *CSVWriter) PreWrite(columns []string, types []string) error {

// WriteRow is row write.
func (w *CSVWriter) WriteRow(values []any, _ []string) error {
for n, column := range values {
if n > 0 {
if _, err := w.writer.WriteRune(w.outDelimiter); err != nil {
return err
}
if len(values) == 0 {
_, err := w.writer.WriteString(w.endLine)
return err
}

if err := w.writeColumn(values[0]); err != nil {
return err
}
for _, column := range values[1:] {
if _, err := w.writer.WriteRune(w.outDelimiter); err != nil {
return err
}
if err := w.writeColumn(column); err != nil {
return err
Expand Down
43 changes: 27 additions & 16 deletions output_ltsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,41 @@ func (w *LTSVWriter) PreWrite(columns []string, types []string) error {

// WriteRow is row write to LTSV.
func (w *LTSVWriter) WriteRow(values []any, labels []string) error {
for n, col := range values {
if n > 0 {
if _, err := w.writer.WriteRune(w.delimiter); err != nil {
return err
}
}
if _, err := w.writer.WriteString(labels[n]); err != nil {
return err
}
if err := w.writer.WriteByte(':'); err != nil {
if len(values) == 0 {
return nil
}
if err := w.writeColumn(labels[0], values[0]); err != nil {
return err
}
for n, col := range values[1:] {
if _, err := w.writer.WriteRune(w.delimiter); err != nil {
return err
}

str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
}
if _, err := w.writer.WriteString(str); err != nil {
if err := w.writeColumn(labels[n+1], col); err != nil {
return err
}
}
return w.writer.WriteByte('\n')
}

func (w *LTSVWriter) writeColumn(label string, value any) error {
if _, err := w.writer.WriteString(label); err != nil {
return err
}
if err := w.writer.WriteByte(':'); err != nil {
return err
}

str := ValString(value)
if value == nil && w.needNULL {
str = w.outNULL
}
if _, err := w.writer.WriteString(str); err != nil {
return err
}
return nil
}

// PostWrite is flush.
func (w *LTSVWriter) PostWrite() error {
return w.writer.Flush()
Expand Down
48 changes: 32 additions & 16 deletions output_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ func (w *RAWWriter) PreWrite(columns []string, types []string) error {
if !w.outHeader {
return nil
}
for n, col := range columns {
if n > 0 {
if _, err := w.writer.WriteString(w.delimiter); err != nil {
return err
}
if len(columns) == 0 {
return nil
}
if err := w.writeColumn(columns[0]); err != nil {
return err
}
for _, col := range columns[1:] {
if _, err := w.writer.WriteString(w.delimiter); err != nil {
return err
}
if _, err := w.writer.WriteString(col); err != nil {
if err := w.writeColumn(col); err != nil {
return err
}
}
Expand All @@ -56,23 +60,35 @@ func (w *RAWWriter) PreWrite(columns []string, types []string) error {

// WriteRow is row write.
func (w *RAWWriter) WriteRow(values []any, _ []string) error {
for n, col := range values {
if n > 0 {
if _, err := w.writer.WriteString(w.delimiter); err != nil {
return err
}
}
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
if len(values) == 0 {
return nil
}
if err := w.writeColumn(values[0]); err != nil {
return err
}
for _, col := range values[1:] {
if _, err := w.writer.WriteString(w.delimiter); err != nil {
return err
}
if _, err := w.writer.WriteString(str); err != nil {

if err := w.writeColumn(col); err != nil {
return err
}
}
return w.writer.WriteByte('\n')
}

func (w *RAWWriter) writeColumn(value any) error {
str := ValString(value)
if value == nil && w.needNULL {
str = w.outNULL
}
if _, err := w.writer.WriteString(str); err != nil {
return err
}
return nil
}

// PostWrite is flush.
func (w *RAWWriter) PostWrite() error {
return w.writer.Flush()
Expand Down

0 comments on commit 0b39c15

Please sign in to comment.