Skip to content

Commit 5a0cd25

Browse files
committed
chore(code-quality): errors in code standard
1 parent 19f2c5a commit 5a0cd25

File tree

8 files changed

+89
-106
lines changed

8 files changed

+89
-106
lines changed

auxiliary.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (this *SQCloud) GetAutocompleteTokens() (tokens []string) {
4646
if table != "sqlite_sequence" {
4747
tokens = append(tokens, table)
4848
for _, column := range this.ListColumns(table) {
49-
tokens = append(tokens, fmt.Sprintf("%s", column))
49+
tokens = append(tokens, column)
5050
tokens = append(tokens, fmt.Sprintf("%s.%s", table, column))
5151
}
5252
}
@@ -239,7 +239,7 @@ func resultToMap(result *Result, err error) (map[string]interface{}, error) {
239239
keyValueList[key.GetString()] = nil
240240
// case val.IsJSON():
241241
default:
242-
return map[string]interface{}{}, errors.New(fmt.Sprintf("ERROR: Value type %v not supported", val.GetType()))
242+
return map[string]interface{}{}, fmt.Errorf("ERROR: Value type %v not supported", val.GetType())
243243
}
244244
}
245245
return keyValueList, nil

chunk.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,26 @@ func (this *Chunk) Uncompress() error {
6060
var err error
6161

6262
var hStartIndex uint64 = 1 // Index of the start of the uncompressed header in chunk (*0 NROWS NCOLS ...)
63-
var zStartIndex uint64 = 0 // Index of the start of the compressed buffer in this chunk (<Compressed DATA...>)
63+
var zStartIndex uint64 // Index of the start of the compressed buffer in this chunk (<Compressed DATA...>)
6464

65-
var LEN uint64 = 0
66-
var lLEN uint64 = 0
65+
var LEN uint64
66+
var lLEN uint64
6767

68-
var COMPRESSED uint64 = 0
69-
var cLEN uint64 = 0
68+
var COMPRESSED uint64
69+
var cLEN uint64
7070

71-
var UNCOMPRESSED uint64 = 0
72-
var iUNCOMPRESSED int = 0
73-
var uLEN uint64 = 0
71+
var UNCOMPRESSED uint64
72+
var iUNCOMPRESSED int
73+
var uLEN uint64
7474

75-
LEN, _, lLEN, err = this.readUInt64At(hStartIndex) // "%TLEN "
76-
hStartIndex += lLEN // hStartIndex -> "CLEN ULEN *0 NROWS NCOLS <Compressed DATA...>"
75+
LEN, _, lLEN, _ = this.readUInt64At(hStartIndex) // "%TLEN "
76+
hStartIndex += lLEN // hStartIndex -> "CLEN ULEN *0 NROWS NCOLS <Compressed DATA...>"
7777

78-
COMPRESSED, _, cLEN, err = this.readUInt64At(hStartIndex) // "CLEN "
79-
hStartIndex += cLEN // hStartIndex -> "ULEN *0 NROWS NCOLS <Compressed DATA...>"
78+
COMPRESSED, _, cLEN, _ = this.readUInt64At(hStartIndex) // "CLEN "
79+
hStartIndex += cLEN // hStartIndex -> "ULEN *0 NROWS NCOLS <Compressed DATA...>"
8080

81-
UNCOMPRESSED, _, uLEN, err = this.readUInt64At(hStartIndex) // "ULEN "
82-
hStartIndex += uLEN // hStartIndex -> "*0 NROWS NCOLS <Compressed DATA...>"
81+
UNCOMPRESSED, _, uLEN, _ = this.readUInt64At(hStartIndex) // "ULEN "
82+
hStartIndex += uLEN // hStartIndex -> "*0 NROWS NCOLS <Compressed DATA...>"
8383

8484
zStartIndex = LEN - COMPRESSED + lLEN + 1 // zStartIndex -> "<Compressed DATA...>"
8585
hLEN := zStartIndex - hStartIndex // = len( "*0 NROWS NCOLS " )
@@ -142,7 +142,6 @@ func (this *Chunk) readUInt64At(offset uint64) (uint64, uint64, uint64, error) {
142142
}
143143
bytesRead++
144144
}
145-
return 0, 0, 0, errors.New("Overflow")
146145
}
147146

148147
func (this *Chunk) readValueAt(offset uint64) (Value, uint64, error) {
@@ -222,21 +221,19 @@ func (this *Value) readBufferAt(chunk *Chunk, offset uint64) (uint64, error) {
222221
}
223222

224223
func protocolBufferFromValue(v interface{}) [][]byte {
225-
if v == nil {
224+
switch v := v.(type) {
225+
case nil:
226226
return protocolBufferFromNull()
227-
} else {
228-
switch v.(type) {
229-
case int, int8, int16, int32, int64:
230-
return protocolBufferFromInt(v)
231-
case float32, float64:
232-
return protocolBufferFromFloat(v)
233-
case string:
234-
return protocolBufferFromString(v.(string), true)
235-
case []byte:
236-
return protocolBufferFromBytes(v.([]byte))
237-
default:
238-
return make([][]byte, 0)
239-
}
227+
case int, int8, int16, int32, int64:
228+
return protocolBufferFromInt(v)
229+
case float32, float64:
230+
return protocolBufferFromFloat(v)
231+
case string:
232+
return protocolBufferFromString(v, true)
233+
case []byte:
234+
return protocolBufferFromBytes(v)
235+
default:
236+
return make([][]byte, 0)
240237
}
241238
}
242239

@@ -404,8 +401,6 @@ func (this *SQCloud) sendArray(command string, values []interface{}) (int, error
404401
if bytesSent != bytesToSend {
405402
return bytesSent, errors.New("Partitial data sent")
406403
}
407-
} else {
408-
bytesSent = 0
409404
}
410405
}
411406

connection.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (this *SQCloud) CheckConnectionParameter() error {
248248
}
249249

250250
if this.Timeout < 0 {
251-
return errors.New(fmt.Sprintf("Invalid Timeout (%s)", this.Timeout.String()))
251+
return fmt.Errorf("Invalid Timeout (%s)", this.Timeout.String())
252252
}
253253

254254
return nil
@@ -318,7 +318,6 @@ func (this *SQCloud) reconnect() error {
318318

319319
var dialer = net.Dialer{}
320320
dialer.Timeout = this.Timeout
321-
dialer.DualStack = true
322321

323322
switch {
324323
case this.cert != nil:
@@ -532,7 +531,7 @@ func (this *SQCloud) Compress(CompressMode string) error {
532531
case this.sock == nil:
533532
return errors.New("Not connected")
534533
case c == "":
535-
return errors.New(fmt.Sprintf("Invalid method (%s)", CompressMode))
534+
return fmt.Errorf("Invalid method (%s)", CompressMode)
536535
default:
537536
return this.Execute(c)
538537
}

helper.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package sqlitecloud
2020
import (
2121
"errors"
2222
"fmt"
23-
"strconv"
2423
"strings"
2524
)
2625

@@ -102,40 +101,3 @@ func parseBool(value string, defaultValue bool) (bool, error) {
102101
return false, errors.New("ERROR: Not a Boolean value")
103102
}
104103
}
105-
106-
// parseInt parses the given string value and tries to extract its value as an int value.
107-
// If value was an empty string, the defaultValue is evaluated instead.
108-
// If the given string value does not resemble a numeric value or its numeric value is smaler than minValue or exceeds maxValue, an error describing the problem is returned.
109-
func parseInt(value string, defaultValue int, minValue int, maxValue int) (int, error) {
110-
// println( "ParseInt = " + value )
111-
value = strings.TrimSpace(value)
112-
if value == "" {
113-
value = fmt.Sprintf("%d", defaultValue)
114-
}
115-
if v, err := strconv.Atoi(value); err == nil {
116-
if v < minValue {
117-
return 0, errors.New("ERROR: The given Number is too small")
118-
}
119-
if v > maxValue {
120-
return 0, errors.New("ERROR: The given Number is too large")
121-
}
122-
return v, nil
123-
} else {
124-
return 0, err
125-
}
126-
}
127-
128-
// parseString returns a non empty string.
129-
// The given string value is trimmed.
130-
// If the given string is an empty string, the defaultValue is evaluated instead.
131-
// If the given string and the defaultValue are emptry strings, an error is returned.
132-
func parseString(value string, defaultValue string) (string, error) {
133-
value = strings.TrimSpace(value)
134-
if value == "" {
135-
value = strings.TrimSpace(defaultValue)
136-
}
137-
if value == "" {
138-
return "", errors.New("ERROR: Empty value")
139-
}
140-
return value, nil
141-
}

result.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ func (this *SQCloud) readResult() (*Result, error) {
880880
// Array
881881
case CMD_ARRAY:
882882
var offset uint64 = 1 // skip the first type byte
883-
var N uint64 = 0
884-
var bytesRead uint64 = 0
883+
var N uint64
884+
var bytesRead uint64
885885

886886
if _, _, bytesRead, err = chunk.readUInt64At(offset); err != nil {
887887
return nil, err
@@ -900,14 +900,14 @@ func (this *SQCloud) readResult() (*Result, error) {
900900

901901
for row := uint64(0); row < N; row++ {
902902
result.rows[row].result = &result
903-
result.rows[row].index = row
904-
result.rows[row].columns = make([]Value, 1)
903+
result.rows[row].Index = row
904+
result.rows[row].Columns = make([]Value, 1)
905905

906-
switch result.rows[row].columns[0], bytesRead, err = chunk.readValueAt(offset); {
906+
switch result.rows[row].Columns[0], bytesRead, err = chunk.readValueAt(offset); {
907907
case err != nil:
908908
return nil, err
909909
default:
910-
columnLength := result.rows[row].columns[0].GetLength()
910+
columnLength := result.rows[row].Columns[0].GetLength()
911911
if result.Width[0] < columnLength {
912912
result.Width[0] = columnLength
913913
}
@@ -929,12 +929,12 @@ func (this *SQCloud) readResult() (*Result, error) {
929929
// RowSet Chunk: /LEN IDX:VERSION ROWS COLS DATA
930930

931931
var offset uint64 = 1 // skip the first type byte
932-
var bytesRead uint64 = 0
933-
var LEN uint64 = 0
934-
var IDX uint64 = 0
935-
var VERSION uint64 = 0
936-
var NROWS uint64 = 0
937-
var NCOLS uint64 = 0
932+
var bytesRead uint64
933+
var LEN uint64
934+
var IDX uint64
935+
var VERSION uint64
936+
var NROWS uint64
937+
var NCOLS uint64
938938

939939
// Detect end of Rowset Chunk directly without parsing...
940940
if Type == CMD_ROWSET_CHUNK {
@@ -965,7 +965,7 @@ func (this *SQCloud) readResult() (*Result, error) {
965965
} // 0..columns-1
966966
offset += bytesRead
967967

968-
LEN = LEN + offset // check for overreading...
968+
LEN += offset // check for overreading...
969969

970970
if Type == CMD_ROWSET_CHUNK && NROWS == 0 && NCOLS == 0 {
971971
return &result, nil
@@ -1105,17 +1105,17 @@ func (this *SQCloud) readResult() (*Result, error) {
11051105
for row := uint64(0); row < NROWS; row++ {
11061106

11071107
rows[row].result = &result
1108-
rows[row].index = rowIndex
1109-
rows[row].columns = make([]Value, int(NCOLS))
1108+
rows[row].Index = rowIndex
1109+
rows[row].Columns = make([]Value, int(NCOLS))
11101110

11111111
rowIndex++
11121112

11131113
for column := uint64(0); column < NCOLS; column++ {
1114-
switch rows[row].columns[column], bytesRead, err = chunk.readValueAt(offset); {
1114+
switch rows[row].Columns[column], bytesRead, err = chunk.readValueAt(offset); {
11151115
case err != nil:
11161116
return nil, err
11171117
default:
1118-
columnLength := rows[row].columns[column].GetLength()
1118+
columnLength := rows[row].Columns[column].GetLength()
11191119
if result.Width[column] < columnLength {
11201120
result.Width[column] = columnLength
11211121
}

row.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929

3030
type ResultRow struct {
3131
result *Result
32-
index uint64 `json:"Index"` // 0, 1, ... rows-1
33-
columns []Value `json:"ColumnValues"`
32+
Index uint64 `json:"Index"` // 0, 1, ... rows-1
33+
Columns []Value `json:"ColumnValues"`
3434
}
3535

3636
// ToJSON returns a JSON representation of this query result row.
@@ -42,7 +42,7 @@ func (this *ResultRow) IsFirst() bool {
4242
case this.result.GetNumberOfRows() < 1:
4343
return false
4444
default:
45-
return this.index == 0
45+
return this.Index == 0
4646
}
4747
}
4848

@@ -52,7 +52,7 @@ func (this *ResultRow) IsLast() bool {
5252
case this.result.GetNumberOfRows() < 1:
5353
return false
5454
default:
55-
return this.index == this.result.GetNumberOfRows()-1
55+
return this.Index == this.result.GetNumberOfRows()-1
5656
}
5757
}
5858

@@ -62,7 +62,7 @@ func (this *ResultRow) IsEOF() bool {
6262
case this.result.GetNumberOfRows() < 1:
6363
return true
6464
default:
65-
return this.index >= this.result.GetNumberOfRows()
65+
return this.Index >= this.result.GetNumberOfRows()
6666
}
6767
}
6868

@@ -78,22 +78,22 @@ func (this *ResultRow) Rewind() *ResultRow {
7878

7979
// Next fetches the next row in this query result and returns it, otherwise if there is no next row, nil is returned.
8080
func (this *ResultRow) Next() *ResultRow {
81-
switch row, err := this.result.GetRow(this.index + 1); {
81+
switch row, err := this.result.GetRow(this.Index + 1); {
8282
case err != nil:
8383
return nil
8484
default:
8585
return row
8686
}
8787
}
8888

89-
func (this *ResultRow) GetNumberOfColumns() uint64 { return uint64(len(this.columns)) }
89+
func (this *ResultRow) GetNumberOfColumns() uint64 { return uint64(len(this.Columns)) }
9090

9191
func (this *ResultRow) GetValue(Column uint64) (*Value, error) {
9292
switch {
9393
case Column >= this.GetNumberOfColumns():
9494
return nil, errors.New("Column index out of bounds")
9595
default:
96-
return &this.columns[Column], nil
96+
return &this.Columns[Column], nil
9797
}
9898
}
9999

@@ -211,37 +211,37 @@ func (this *ResultRow) IsText(Column uint64) bool {
211211
// GetStringValue returns the contents in column Column of this query result row as string.
212212
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
213213
func (this *ResultRow) GetString(Column uint64) (string, error) {
214-
return this.result.GetStringValue(this.index, Column)
214+
return this.result.GetStringValue(this.Index, Column)
215215
}
216216

217217
// GetInt32Value returns the contents in column Column of this query result row as int32.
218218
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
219219
func (this *ResultRow) GetInt32(Column uint64) (int32, error) {
220-
return this.result.GetInt32Value(this.index, Column)
220+
return this.result.GetInt32Value(this.Index, Column)
221221
}
222222

223223
// GetInt64Value returns the contents in column Column of this query result row as int64.
224224
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
225225
func (this *ResultRow) GetInt64(Column uint64) (int64, error) {
226-
return this.result.GetInt64Value(this.index, Column)
226+
return this.result.GetInt64Value(this.Index, Column)
227227
}
228228

229229
// GetFloat32Value returns the contents in column Column of this query result row as float32.
230230
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
231231
func (this *ResultRow) GetFloat32(Column uint64) (float32, error) {
232-
return this.result.GetFloat32Value(this.index, Column)
232+
return this.result.GetFloat32Value(this.Index, Column)
233233
}
234234

235235
// GetFloat64Value returns the contents in column Column of this query result row as float64.
236236
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
237237
func (this *ResultRow) GetFloat64(Column uint64) (float64, error) {
238-
return this.result.GetFloat64Value(this.index, Column)
238+
return this.result.GetFloat64Value(this.Index, Column)
239239
}
240240

241241
// GetSQLDateTime parses this query result value in column Column as an SQL-DateTime and returns its value.
242242
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
243243
func (this *ResultRow) GetSQLDateTime(Column uint64) (time.Time, error) {
244-
return this.result.GetSQLDateTime(this.index, Column)
244+
return this.result.GetSQLDateTime(this.Index, Column)
245245
}
246246

247247
////////

test/commons.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test
22

33
import (
44
"flag"
5+
"fmt"
56
"log"
67
"os"
78
"testing"
@@ -31,8 +32,9 @@ func contains[T comparable](s []T, e T) bool {
3132
func setupDatabase(t *testing.T) (*sqlitecloud.SQCloud, func()) {
3233
connectionString, _ := os.LookupEnv("SQLITE_CONNECTION_STRING")
3334
apikey, _ := os.LookupEnv("SQLITE_API_KEY")
35+
dbname, _ := os.LookupEnv("SQLITE_DB")
3436

35-
db, err := sqlitecloud.Connect(connectionString + "?apikey=" + apikey)
37+
db, err := sqlitecloud.Connect(fmt.Sprintf("%s/%s?apikey=%s", connectionString, dbname, apikey))
3638
if err != nil {
3739
t.Fatal("Connection error: ", err.Error())
3840
}

0 commit comments

Comments
 (0)