Skip to content

Commit

Permalink
Merge pull request #48 from flycash/dev
Browse files Browse the repository at this point in the history
Rft: remove NilAsNullFunc
  • Loading branch information
flycash authored Oct 28, 2021
2 parents 665cf67 + c09c9ee commit 5862fe6
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 166 deletions.
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Selector Definition](https://github.com/gotomicro/eql/pull/2)
- [Deleter Definition](https://github.com/gotomicro/eql/pull/4)
- [Updater Definition](https://github.com/gotomicro/eql/pull/8)
- [Rft: remove NilAsNullFunc](https://github.com/gotomicro/eql/pull/48)
- [Metadata API](https://github.com/gotomicro/eql/pull/16)
- [tagMetaRegistry: default implementation of MetaRegistry](https://github.com/gotomicro/eql/pull/25)
- [Rft: remove defaultRegistry](https://github.com/gotomicro/eql/pull/46)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We are not English native speaker, so we use Chinese to write the design documen
Here is a good one: https://www.deepl.com/en/translator

[设计思路](./docs/design.md)

[B站视频](https://space.bilibili.com/324486985)

## Contribution
Expand All @@ -21,3 +22,10 @@ You must follow these rules:
- You must add license header to every new files

[style guide](https://github.com/uber-go/guide/blob/master/style.md)

### Setup Develop Environment

#### install golangci-lint
Please refer [Install golangci-lint](https://golangci-lint.run/usage/install/)
#### setup pre-push github hook
Please move the `.github/pre-push` to your `.git` directory
64 changes: 0 additions & 64 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package eql

import (
"reflect"

"github.com/valyala/bytebufferpool"
)

Expand All @@ -27,15 +25,13 @@ type DBOption func(db *DB)
type DB struct {
metaRegistry MetaRegistry
dialect Dialect
nullAssertFunc NullAssertFunc
}

// New returns DB. It's the entry of EQL
func New(opts ...DBOption) *DB {
db := &DB{
metaRegistry: &tagMetaRegistry{},
dialect: mysql,
nullAssertFunc: NilAsNullFunc,
}
for _, o := range opts {
o(db)
Expand All @@ -62,7 +58,6 @@ func (db *DB) Update(table interface{}) *Updater {
return &Updater{
builder: db.builder(),
table: table,
nullAssertFunc: db.nullAssertFunc,
}
}

Expand All @@ -80,62 +75,3 @@ func (db *DB) builder() builder {
buffer: bytebufferpool.Get(),
}
}

func WithNullAssertFunc(nullable NullAssertFunc) DBOption {
return func(db *DB) {
db.nullAssertFunc = nullable
}
}

// NullAssertFunc determined if the value is NULL.
// As we know, there is a gap between NULL and nil
// There are two kinds of nullAssertFunc
// 1. nil = NULL, see NilAsNullFunc
// 2. zero value = NULL, see ZeroAsNullFunc
type NullAssertFunc func(val interface{}) bool

// NilAsNullFunc use the strict definition of "nullAssertFunc"
// if and only if the val is nil, indicates value is null
func NilAsNullFunc(val interface{}) bool {
return val == nil
}

// ZeroAsNullFunc means "zero value = null"
func ZeroAsNullFunc(val interface{}) bool {
if val == nil {
return true
}
switch v := val.(type) {
case int:
return v == 0
case int8:
return v == 0
case int16:
return v == 0
case int32:
return v == 0
case int64:
return v == 0
case uint:
return v == 0
case uint8:
return v == 0
case uint16:
return v == 0
case uint32:
return v == 0
case uint64:
return v == 0
case float32:
return v == 0
case float64:
return v == 0
case bool:
return v
case string:
return v == ""
default:
valRef := reflect.ValueOf(val)
return valRef.IsZero()
}
}
51 changes: 0 additions & 51 deletions db_test.go

This file was deleted.

11 changes: 10 additions & 1 deletion internal/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@

package internal

import "fmt"
import (
"errors"
"fmt"
)

var errValueNotSet = errors.New("value unset")

// NewInvalidColumnError returns an error represents invalid field name
// TODO(do we need errors pkg?)
func NewInvalidColumnError(field string) error {
return fmt.Errorf("eql: invalid column name %s, " +
"it must be a valid field name of structure", field)
}

func NewValueNotSetError() error {
return errValueNotSet
}
2 changes: 1 addition & 1 deletion model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestTagMetaRegistry(t *testing.T) {
idMetaLastName := meta.fieldMap["LastName"]
assert.Equal(t, "last_name", idMetaLastName.columnName)
assert.Equal(t, "LastName", idMetaLastName.fieldName)
assert.Equal(t, reflect.TypeOf(string("")), idMetaLastName.typ)
assert.Equal(t, reflect.TypeOf((*string)(nil)), idMetaLastName.typ)

idMetaLastAge := meta.fieldMap["Age"]
assert.Equal(t, "age", idMetaLastAge.columnName)
Expand Down
6 changes: 5 additions & 1 deletion predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type TestModel struct {
Id int64 `eql:"auto_increment,primary_key"`
FirstName string
Age int8
LastName string
LastName *string
}

type CommonTestCase struct {
Expand All @@ -112,4 +112,8 @@ type CommonTestCase struct {
wantArgs []interface{}
wantSql string
wantErr error
}

func stringPtr(val string) *string {
return &val
}
Loading

0 comments on commit 5862fe6

Please sign in to comment.