Skip to content

Commit

Permalink
Fix staticcheck warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Mar 16, 2021
1 parent 86edffb commit 7853532
Show file tree
Hide file tree
Showing 31 changed files with 189 additions and 377 deletions.
12 changes: 6 additions & 6 deletions cmd/genji/dbutil/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func TestInsertJSON(t *testing.T) {
want string
fails bool
}{
{"Simple Json", `{"a": 1}`, `{"a": 1}`, false},
{"JSON object", `{"a": {"b": [1, 2, 3]}}`, `{"a": {"b": [1, 2, 3]}}`, false},
{"nested document", `{"a": {"b": [1, 2, 3]}}`, `{"a": {"b": [1, 2, 3]}}`, false},
{"nested array multiple indexes", `{"a": {"b": [1, 2, [1, 2, {"c": "foo"}]]}}`, `{"a": {"b": [1, 2, [1, 2, {"c": "foo"}]]}}`, false},
{"document in array", `{"a": [{"b":"foo"}, 2, 3]}`, `{"a": [{"b":"foo"}, 2, 3]}`, false},
{"Simple Json", `{"a": 1}`, `[{"a": 1}]`, false},
{"JSON object", `{"a": {"b": [1, 2, 3]}}`, `[{"a": {"b": [1, 2, 3]}}]`, false},
{"nested document", `{"a": {"b": [1, 2, 3]}}`, `[{"a": {"b": [1, 2, 3]}}]`, false},
{"nested array multiple indexes", `{"a": {"b": [1, 2, [1, 2, {"c": "foo"}]]}}`, `[{"a": {"b": [1, 2, [1, 2, {"c": "foo"}]]}}]`, false},
{"document in array", `{"a": [{"b":"foo"}, 2, 3]}`, `[{"a": [{"b":"foo"}, 2, 3]}]`, false},
{"Non closed json array", `[{"foo":"bar"}`, ``, true},
{"Non closed json stream", `{"foo":"bar"`, ``, true},
}
Expand All @@ -46,7 +46,7 @@ func TestInsertJSON(t *testing.T) {
require.NoError(t, err)

var buf bytes.Buffer
err = document.IteratorToJSON(&buf, res)
err = document.IteratorToJSONArray(&buf, res)
require.NoError(t, err)
require.JSONEq(t, tt.want, buf.String())

Expand Down
4 changes: 1 addition & 3 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,7 @@ func (ti *TableInfo) ScanDocument(d document.Document) error {
func (ti *TableInfo) Clone() *TableInfo {
cp := *ti
cp.FieldConstraints = nil
for _, fc := range ti.FieldConstraints {
cp.FieldConstraints = append(cp.FieldConstraints, fc)
}
cp.FieldConstraints = append(cp.FieldConstraints, ti.FieldConstraints...)
return &cp
}

Expand Down
5 changes: 2 additions & 3 deletions document/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ func (vb ValueBuffer) MarshalJSON() ([]byte, error) {
// UnmarshalJSON implements the json.Unmarshaler interface.
func (vb *ValueBuffer) UnmarshalJSON(data []byte) error {
var err error
_, perr := jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
var v Value
v, err = parseJSONValue(dataType, value)
_, perr := jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, _ error) {
v, err := parseJSONValue(dataType, value)
if err != nil {
return
}
Expand Down
1 change: 1 addition & 0 deletions document/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ func TestValueBufferApply(t *testing.T) {
require.NoError(t, err)

got, err := json.Marshal(buf)
require.NoError(t, err)
require.JSONEq(t, `[6, [6, 6], {"4": 6}]`, string(got))
}
8 changes: 4 additions & 4 deletions document/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ func compareBooleans(op operator, a, b bool) bool {
case operatorEq:
return a == b
case operatorGt:
return a == true && b == false
return a && !b
case operatorGte:
return a == b || a == true
return a == b || a
case operatorLt:
return a == false && b == true
return !a && b
case operatorLte:
return a == b || a == false
return a == b || !a
}

return false
Expand Down
3 changes: 3 additions & 0 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func setValueAtPath(v Value, p Path, newValue Value) (Value, error) {
}

va, err = setValueAtPath(va, p[1:], newValue)
if err != nil {
return v, err
}
err = vb.Replace(p[0].ArrayIndex, va)
return NewArrayValue(&vb), err
}
Expand Down
2 changes: 2 additions & 0 deletions document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func TestFieldBuffer(t *testing.T) {
require.NoError(t, err)

got, err := json.Marshal(buf)
require.NoError(t, err)
require.JSONEq(t, `{"a": 1, "c": [1, 1], "f": {"g": 1}}`, string(got))
})

Expand Down Expand Up @@ -345,6 +346,7 @@ func TestNewFromStruct(t *testing.T) {
Ig: 100,
},
BB: time.Date(2020, 11, 15, 16, 37, 10, 20, time.UTC),
t: 99,
}

q := 5
Expand Down
25 changes: 0 additions & 25 deletions document/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,6 @@ func (rr documentsIterator) Iterate(fn func(d Document) error) error {
return nil
}

// The IteratorFunc type is an adapter to allow the use of ordinary functions as Iterators.
// If f is a function with the appropriate signature, IteratorFunc(f) is an Iterator that calls f.
type IteratorFunc func(fn func(d Document) error) error

// Iterate calls f(fn).
func (f IteratorFunc) Iterate(fn func(d Document) error) error {
return f(fn)
}

// IteratorToJSON encodes all the documents of an iterator to JSON stream.
func IteratorToJSON(w io.Writer, s Iterator) error {
buf := bufio.NewWriter(w)
defer buf.Flush()

return s.Iterate(func(d Document) error {
data, err := jsonDocument{d}.MarshalJSON()
if err != nil {
return err
}

_, err = buf.Write(data)
return err
})
}

// IteratorToJSONArray encodes all the documents of an iterator to a JSON array.
func IteratorToJSONArray(w io.Writer, s Iterator) error {
buf := bufio.NewWriter(w)
Expand Down
97 changes: 0 additions & 97 deletions document/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,109 +4,12 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"testing"

"github.com/genjidb/genji"
"github.com/genjidb/genji/document"
"github.com/stretchr/testify/require"
)

func ExampleStream_Iterate() {
type User struct {
ID int64
Name string
Age uint32
Address struct {
City string
ZipCode string
}
}

db, err := genji.Open(":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()

err = db.Exec("CREATE TABLE IF NOT EXISTS user")
if err != nil {
log.Fatal(err)
}

for i := 1; i <= 10; i++ {
err = db.Exec("INSERT INTO user VALUES ?", &User{
ID: int64(i),
Name: fmt.Sprintf("foo%d", i),
Age: uint32(i * 10),
Address: struct {
City string
ZipCode string
}{
City: "Lyon",
ZipCode: fmt.Sprintf("69%03d", i),
},
})
if err != nil {
log.Fatal(err)
}
}

result, err := db.Query(`SELECT id, name, age, address FROM user WHERE age >= 18`)
if err != nil {
panic(err)
}
defer result.Close()

err = result.Iterate(func(d document.Document) error {
// Scan into a struct
var u User
err = document.StructScan(d, &u)
if err != nil {
return err
}

fmt.Println(u)

// Or scan individual variables
// Types of variables don't have to exactly match with the types stored
var id uint64
var name []byte
var age uint8
var address map[string]string

err = document.Scan(d, &id, &name, &age, &address)
if err != nil {
return err
}

fmt.Println(id, string(name), age, address)
return nil
})
if err != nil {
panic(err)
}

// Output: {2 foo2 20 {Lyon 69002}}
// 2 foo2 20 map[city:Lyon zipcode:69002]
// {3 foo3 30 {Lyon 69003}}
// 3 foo3 30 map[city:Lyon zipcode:69003]
// {4 foo4 40 {Lyon 69004}}
// 4 foo4 40 map[city:Lyon zipcode:69004]
// {5 foo5 50 {Lyon 69005}}
// 5 foo5 50 map[city:Lyon zipcode:69005]
// {6 foo6 60 {Lyon 69006}}
// 6 foo6 60 map[city:Lyon zipcode:69006]
// {7 foo7 70 {Lyon 69007}}
// 7 foo7 70 map[city:Lyon zipcode:69007]
// {8 foo8 80 {Lyon 69008}}
// 8 foo8 80 map[city:Lyon zipcode:69008]
// {9 foo9 90 {Lyon 69009}}
// 9 foo9 90 map[city:Lyon zipcode:69009]
// {10 foo10 100 {Lyon 69010}}
// 10 foo10 100 map[city:Lyon zipcode:69010]
}

func TestIteratorToJSONArray(t *testing.T) {
var docs []document.Document
for i := 0; i < 3; i++ {
Expand Down
34 changes: 6 additions & 28 deletions document/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import (
)

var (
boolZeroValue = NewZeroValue(BoolValue)
integerZeroValue = NewZeroValue(IntegerValue)
doubleZeroValue = NewZeroValue(DoubleValue)
blobZeroValue = NewZeroValue(BlobValue)
textZeroValue = NewZeroValue(TextValue)
arrayZeroValue = NewZeroValue(ArrayValue)
documentZeroValue = NewZeroValue(DocumentValue)
boolZeroValue = NewZeroValue(BoolValue)
integerZeroValue = NewZeroValue(IntegerValue)
doubleZeroValue = NewZeroValue(DoubleValue)
blobZeroValue = NewZeroValue(BlobValue)
textZeroValue = NewZeroValue(TextValue)
)

// ErrUnsupportedType is used to skip struct or array fields that are not supported.
Expand Down Expand Up @@ -211,7 +209,7 @@ func (v Value) IsZeroValue() (bool, error) {
case DoubleValue:
return v.V == doubleZeroValue.V, nil
case BlobValue:
return bytes.Compare(v.V.([]byte), blobZeroValue.V.([]byte)) == 0, nil
return bytes.Equal(v.V.([]byte), blobZeroValue.V.([]byte)), nil
case TextValue:
return v.V == textZeroValue.V, nil
case ArrayValue:
Expand Down Expand Up @@ -464,26 +462,6 @@ func calculateValues(a, b Value, operator byte) (res Value, err error) {
return NewNullValue(), nil
}

func convertNumberToInt64(v Value) (int64, error) {
var i int64

switch v.Type {
case IntegerValue:
return v.V.(int64), nil
case DoubleValue:
f := v.V.(float64)
if f > math.MaxInt64 {
return i, errors.New(`cannot convert "double" to "integer" without overflowing`)
}
if math.Trunc(f) != f {
return 0, errors.New(`cannot convert "double" value to "integer" without loss of precision`)
}
i = int64(f)
}

return i, nil
}

func calculateIntegers(a, b Value, operator byte) (res Value, err error) {
var xa, xb int64

Expand Down
5 changes: 0 additions & 5 deletions document/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ func TestValueString(t *testing.T) {
}

func TestNewValue(t *testing.T) {
type st struct {
A int
B string
}
type myBytes []byte
type myString string
type myBool bool
type myUint uint
type myUint16 uint16
type myUint32 uint32
Expand Down
1 change: 0 additions & 1 deletion engine/boltengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

const (
separator byte = 0x1F
// name of the bucket used to mark keys for deletion
binBucket = "__bin"
)
Expand Down
4 changes: 2 additions & 2 deletions engine/boltengine/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ func Example() {
defer os.RemoveAll(dir)

db, err := genji.Open(filepath.Join(dir, "my.db"))
defer db.Close()
if err != nil {
log.Fatal(err)
}
defer db.Close()
}

func ExampleNewEngine() {
Expand All @@ -38,8 +38,8 @@ func ExampleNewEngine() {
}

db, err := genji.New(context.Background(), ng)
defer db.Close()
if err != nil {
log.Fatal(err)
}
defer db.Close()
}
4 changes: 2 additions & 2 deletions expr/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (m *MinAggregator) Aggregate(env *Environment) error {
return nil
}

if m.Min.Type == v.Type || m.Min.Type.IsNumber() && m.Min.Type.IsNumber() {
if m.Min.Type == v.Type || m.Min.Type.IsNumber() && v.Type.IsNumber() {
ok, err := m.Min.IsGreaterThan(v)
if err != nil {
return err
Expand Down Expand Up @@ -408,7 +408,7 @@ func (m *MaxAggregator) Aggregate(env *Environment) error {
return nil
}

if m.Max.Type == v.Type || m.Max.Type.IsNumber() && m.Max.Type.IsNumber() {
if m.Max.Type == v.Type || m.Max.Type.IsNumber() && v.Type.IsNumber() {
ok, err := m.Max.IsLesserThan(v)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 7853532

Please sign in to comment.