Skip to content

Commit

Permalink
Merge pull request #278 from noborus/reduce-unnecessary-else
Browse files Browse the repository at this point in the history
Reduce unnecessary else by using early return
  • Loading branch information
noborus authored Apr 23, 2024
2 parents d40cea9 + b642e84 commit 7f36018
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 64 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For usage as a library, please refer to the [godoc](https://pkg.go.dev/github.co
* 3.3. [Output formats](#output-formats)
* 3.3.1. [Output options](#output-options)
* 3.4. [Handling of NULL](#handling-of-null)
* 3.5. [Multiple queries (v0.20.0 or later)](#multiple-queries-(v0.20.0-or-later))
* 3.5. [Multiple queries](#multiple-queries)
* 4. [Example](#example)
* 4.1. [STDIN input](#stdin-input)
* 4.2. [Multiple files](#multiple-files)
Expand Down Expand Up @@ -257,7 +257,7 @@ $ echo '[1,null,""]' | trdsql -inull "" -ojson -ijson "SELECT * FROM -"
]
```

### 3.5. <a name='multiple-queries-(v0.20.0-or-later)'></a>Multiple queries (v0.20.0 or later)
### 3.5. <a name='multiple-queries'></a>Multiple queries

Multiple queries can be executed by separating them with a semicolon.
Update queries must be followed by a SELECT statement.
Expand Down Expand Up @@ -1061,7 +1061,7 @@ C:\Users\{"User"}\AppData\Roaming\trdsql\config.json
Or use the -config file option.

```console
$ trdsql -config config.json "SELECT * FROM test.csv"
trdsql -config config.json "SELECT * FROM test.csv"
```

sample: [config.json](config.json.sample)
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ulikunitz/xz"
)

// TableQuery is a query to use instead of TABLE.
const TableQuery = "SELECT * FROM"

// Cli wraps stdout and error output specification.
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ require (
github.com/go-sql-driver/mysql v1.8.1
github.com/goccy/go-yaml v1.11.3
github.com/iancoleman/orderedmap v0.3.0
github.com/itchyny/gojq v0.12.14
github.com/itchyny/gojq v0.12.15
github.com/jwalton/gchalk v1.3.0
github.com/klauspost/compress v1.17.7
github.com/klauspost/compress v1.17.8
github.com/lib/pq v1.10.9
github.com/mattn/go-runewidth v0.0.15
github.com/mattn/go-sqlite3 v1.14.22
Expand All @@ -17,8 +17,8 @@ require (
github.com/noborus/tbln v0.0.2
github.com/olekukonko/tablewriter v0.0.5
github.com/pierrec/lz4 v2.6.1+incompatible
github.com/ulikunitz/xz v0.5.11
golang.org/x/term v0.18.0
github.com/ulikunitz/xz v0.5.12
golang.org/x/term v0.19.0
modernc.org/sqlite v1.29.1
)

Expand All @@ -38,7 +38,7 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/itchyny/gojq v0.12.14 h1:6k8vVtsrhQSYgSGg827AD+PVVaB1NLXEdX+dda2oZCc=
github.com/itchyny/gojq v0.12.14/go.mod h1:y1G7oO7XkcR1LPZO59KyoCRy08T3j9vDYRV0GgYSS+s=
github.com/itchyny/gojq v0.12.15 h1:WC1Nxbx4Ifw5U2oQWACYz32JK8G9qxNtHzrvW4KEcqI=
github.com/itchyny/gojq v0.12.15/go.mod h1:uWAHCbCIla1jiNxmeT5/B5mOjSdfkCq6p8vxWg+BM10=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/jwalton/gchalk v1.3.0 h1:uTfAaNexN8r0I9bioRTksuT8VGjrPs9YIXR1PQbtX/Q=
Expand All @@ -39,8 +39,8 @@ github.com/jwalton/go-supportscolor v1.1.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2q
github.com/jwalton/go-supportscolor v1.2.0 h1:g6Ha4u7Vm3LIsQ5wmeBpS4gazu0UP1DRDE8y6bre4H8=
github.com/jwalton/go-supportscolor v1.2.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -88,8 +88,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
Expand All @@ -103,13 +103,13 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
Expand Down
3 changes: 1 addition & 2 deletions input_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ func NewCSVReader(reader io.Reader, opts *ReadOpts) (*CSVReader, error) {
}
r.names = make([]string, len(row))
for i, col := range row {
r.names[i] = col
if col == "" {
r.names[i] = "c" + strconv.Itoa(i+1)
} else {
r.names[i] = col
}
}
preReadN--
Expand Down
6 changes: 1 addition & 5 deletions input_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ func (r *JSONReader) objectRow(obj map[string]any) (map[string]any, []string, er
row := make(map[string]any)
for k, v := range obj {
names = append(names, k)
if v == nil {
row[k] = nil
} else {
row[k] = r.jsonString(v)
}
row[k] = r.jsonString(v)
}
return row, names, nil
}
Expand Down
12 changes: 2 additions & 10 deletions input_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ func (r *YAMLReader) objectRow(obj map[string]any) (map[string]any, []string, er
row := make(map[string]any)
for k, v := range obj {
names = append(names, k)
if v == nil {
row[k] = nil
} else {
row[k] = r.toString(v)
}
row[k] = r.toString(v)
}
return row, names, nil
}
Expand All @@ -257,11 +253,7 @@ func (r *YAMLReader) objectMapSlice(obj yaml.MapSlice) (map[string]any, []string
for _, item := range obj {
key := item.Key.(string)
names = append(names, key)
if item.Value == nil {
row[key] = nil
} else {
row[key] = r.toString(item.Value)
}
row[key] = r.toString(item.Value)
}
return row, names, nil
}
Expand Down
6 changes: 2 additions & 4 deletions output_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@ func NewCSVWriter(writeOpts *WriteOpts) *CSVWriter {
}
w.outDelimiter = d

w.outQuote = 0
if len(writeOpts.OutQuote) > 0 {
w.outQuote = ([]rune(writeOpts.OutQuote))[0]
} else {
w.outQuote = 0
}
w.outAllQuote = writeOpts.OutAllQuotes
w.outUseCRLF = writeOpts.OutUseCRLF
w.outHeader = writeOpts.OutHeader
w.needQuotes = string(w.outDelimiter) + string(w.outQuote) + "\r\n"
w.endLine = "\n"
if writeOpts.OutUseCRLF {
w.endLine = "\r\n"
} else {
w.endLine = "\n"
}
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
Expand Down
2 changes: 1 addition & 1 deletion output_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func isJSON(s []byte) bool {
return err == nil
}

// PostWrite is Actual output.
// PostWrite is actual output.
func (w *JSONWriter) PostWrite() error {
return w.writer.Encode(w.results)
}
6 changes: 2 additions & 4 deletions output_ltsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (w *LTSVWriter) PreWrite(columns []string, types []string) error {
return nil
}

// WriteRow is row write.
// WriteRow is row write to LTSV.
func (w *LTSVWriter) WriteRow(values []any, labels []string) error {
for n, col := range values {
if n > 0 {
Expand All @@ -44,11 +44,9 @@ func (w *LTSVWriter) WriteRow(values []any, labels []string) error {
return err
}

str := ""
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
} else {
str = ValString(col)
}
if _, err := w.writer.WriteString(str); err != nil {
return err
Expand Down
7 changes: 2 additions & 5 deletions output_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ func NewRAWWriter(writeOpts *WriteOpts) *RAWWriter {
w.writer = bufio.NewWriter(writeOpts.OutStream)
w.delimiter = delimiter
w.outHeader = writeOpts.OutHeader
w.endLine = "\n"
if writeOpts.OutUseCRLF {
w.endLine = "\r\n"
} else {
w.endLine = "\n"
}
w.needNULL = writeOpts.OutNeedNULL
w.outNULL = writeOpts.OutNULL
Expand Down Expand Up @@ -63,11 +62,9 @@ func (w *RAWWriter) WriteRow(values []any, _ []string) error {
return err
}
}
str := ""
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
} else {
str = ValString(col)
}
if _, err := w.writer.WriteString(str); err != nil {
return err
Expand Down
12 changes: 5 additions & 7 deletions output_tablewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ func (w *TWWriter) PreWrite(columns []string, types []string) error {
// WriteRow is Addition to array.
func (w *TWWriter) WriteRow(values []any, columns []string) error {
for i, col := range values {
str := ""
str := ValString(col)
if w.markdown {
str = strings.ReplaceAll(str, `|`, `\|`)
}
if col == nil && w.needNULL {
str = w.outNULL
} else {
str = ValString(col)
if w.markdown {
str = strings.ReplaceAll(str, `|`, `\|`)
}
}
w.results[i] = str
}
w.writer.Append(w.results)
return nil
}

// PostWrite is Actual output.
// PostWrite is actual output.
func (w *TWWriter) PostWrite() error {
w.writer.Render()
return nil
Expand Down
4 changes: 1 addition & 3 deletions output_tbln.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ func (w *TBLNWriter) PreWrite(columns []string, types []string) error {
// WriteRow is row write.
func (w *TBLNWriter) WriteRow(values []any, columns []string) error {
for i, col := range values {
str := ""
str := ValString(col)
if col == nil && w.needNULL {
str = w.outNULL
} else {
str = ValString(col)
}
w.results[i] = strings.ReplaceAll(str, "\n", "\\n")
}
Expand Down
6 changes: 2 additions & 4 deletions output_vertical.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (w *VFWriter) PreWrite(columns []string, types []string) error {
return nil
}

// WriteRow is Actual output.
// WriteRow is actual output.
func (w *VFWriter) WriteRow(values []any, columns []string) error {
w.count++
_, err := fmt.Fprintf(w.writer,
Expand All @@ -58,11 +58,9 @@ func (w *VFWriter) WriteRow(values []any, columns []string) error {
}
for i, col := range w.header {
v := w.hSize - runewidth.StringWidth(col)
str := ""
str := ValString(values[i])
if values[i] == nil && w.needNULL {
str = w.outNULL
} else {
str = ValString(values[i])
}

_, err := fmt.Fprintf(w.writer,
Expand Down
2 changes: 1 addition & 1 deletion output_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func compatibleYAML(v any, needNULL bool, outNULL string) any {
}
}

// PostWrite is Actual output.
// PostWrite is actual output.
func (w *YAMLWriter) PostWrite() error {
return w.writer.Encode(w.results)
}

0 comments on commit 7f36018

Please sign in to comment.