Skip to content

Commit

Permalink
Add omitempty field option to skip zero values (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Feb 22, 2021
1 parent 753cad9 commit 953f461
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
bench:
strategy:
matrix:
go-version: [ 1.15.x ]
go-version: [ 1.16.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand Down
25 changes: 17 additions & 8 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2.3.0
uses: golangci/golangci-lint-action@v2.4.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.34.1
version: v1.37.0

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Required: the token is used for fetching a list of releases of golangci-lint.
# The secret `GITHUB_TOKEN` is automatically created by GitHub,
# no need to create it manually.
# https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret
github-token: ${{ secrets.GITHUB_TOKEN }}
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
6 changes: 3 additions & 3 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
strategy:
matrix:
go-version: [ 1.13.x, 1.14.x, 1.15.x ]
go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
run: cp unit.txt unit-base.txt
- name: Comment Test Coverage
if: matrix.go-version == '1.15.x'
if: matrix.go-version == '1.16.x'
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -68,7 +68,7 @@ jobs:
</details>
- name: Upload code coverage
if: matrix.go-version == '1.15.x'
if: matrix.go-version == '1.16.x'
uses: codecov/codecov-action@v1
with:
file: ./unit.coverprofile
Expand Down
43 changes: 43 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# See https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
run:
tests: true

linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
gocyclo:
min-complexity: 20
dupl:
threshold: 100
misspell:
locale: US
unused:
check-exported: false
unparam:
check-exported: true

linters:
enable-all: true
disable:
- lll
- maligned
- gochecknoglobals
- gomnd
- wrapcheck
- paralleltest
- forbidigo
- exhaustivestruct

issues:
exclude-use-default: false
exclude-rules:
- linters:
- gomnd
- goconst
- goerr113
- noctx
- funlen
- dupl
path: "_test.go"

16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This module integrates [`github.com/Masterminds/squirrel`](https://github.com/Ma
and [`github.com/jmoiron/sqlx`](https://github.com/jmoiron/sqlx) to allow seamless operation based on field tags of row
structure.

This library helps to eliminate literal string column references (e.g. `"created_at"`) and use field references
instead (e.g. `s.Col(&row, &row.CreatedAt)` and other mapping functions).

Field tags (`db` by default) act as a source of truth for column names to allow better maintainability and fewer errors.

## Simple CRUD
Expand All @@ -24,9 +27,9 @@ var (
const tableName = "products"

type Product struct {
ID int `db:"id"`
ID int `db:"id,omitempty"`
Title string `db:"title"`
CreatedAt time.Time `db:"created_at"`
CreatedAt time.Time `db:"created_at,omitempty"`
}

// INSERT INTO products (id, title, created_at) VALUES (1, 'Apples', <now>), (2, 'Oranges', <now>)
Expand All @@ -47,8 +50,8 @@ if err != nil {
// UPDATE products SET title = 'Bananas' WHERE id = 2
_, err = s.Exec(
ctx,
s.UpdateStmt(tableName, Product{Title: "Bananas"}, sqluct.SkipZeroValues).
Where(s.WhereEq(Product{ID: 2}, sqluct.SkipZeroValues)),
s.UpdateStmt(tableName, Product{Title: "Bananas"}).
Where(s.WhereEq(Product{ID: 2})),
)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -128,4 +131,7 @@ fmt.Println(args)
// WHERE manager.last_name = employee.last_name AND manager.first_name != ?
//
// [John]
```
```

## Omitting Zero Values

18 changes: 9 additions & 9 deletions example_mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func ExampleMapper_Col() {
sm := sqluct.Mapper{}

type Order struct {
ID int `db:"order_id"`
CreatedAt time.Time `db:"created_at"`
ID int `db:"order_id,omitempty"`
CreatedAt time.Time `db:"created_at,omitempty"`
}

o := Order{
Expand All @@ -36,7 +36,7 @@ func ExampleMapper_Insert() {
sm := sqluct.Mapper{}

type Order struct {
ID int `db:"order_id"`
ID int `db:"order_id,omitempty"`
Amount int `db:"amount"`
UserID int `db:"user_id"`
}
Expand All @@ -45,7 +45,7 @@ func ExampleMapper_Insert() {
o.Amount = 100
o.UserID = 123

q := sm.Insert(squirrel.Insert("orders"), o, sqluct.SkipZeroValues)
q := sm.Insert(squirrel.Insert("orders"), o)

query, args, err := q.ToSql()
fmt.Println(query, args, err)
Expand All @@ -58,11 +58,11 @@ func ExampleMapper_Update() {

type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id"`
UserID int `db:"user_id,omitempty"`
}

type Order struct {
ID int `db:"order_id"`
ID int `db:"order_id,omitempty"`
OrderData
}

Expand All @@ -86,11 +86,11 @@ func ExampleMapper_Select() {

type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id"`
UserID int `db:"user_id,omitempty"`
}

type Order struct {
ID int `db:"order_id"`
ID int `db:"order_id,omitempty"`
OrderData
}

Expand All @@ -112,7 +112,7 @@ func ExampleMapper_WhereEq() {

type OrderData struct {
Amount int `db:"amount"`
UserID int `db:"user_id"`
UserID int `db:"user_id,omitempty"`
}

type Order struct {
Expand Down
9 changes: 3 additions & 6 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func ExampleStorage_Select_oneRow() {

qb := s.SelectStmt("my_table", row)

err := s.Select(ctx, qb, &row)
if err != nil {
if err := s.Select(ctx, qb, &row); err != nil {
log.Fatal(err)
}
}
Expand All @@ -163,8 +162,7 @@ func ExampleStorage_InsertStmt() {

qb := s.InsertStmt("my_table", row)

_, err := s.Exec(ctx, qb)
if err != nil {
if _, err := s.Exec(ctx, qb); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -192,8 +190,7 @@ func ExampleStorage_UpdateStmt() {
qb := s.UpdateStmt("my_table", row).
Where(s.Mapper.WhereEq(MyIdentity{ID: 123}))

_, err := s.Exec(ctx, qb)
if err != nil {
if _, err := s.Exec(ctx, qb); err != nil {
log.Fatal(err)
}
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Masterminds/squirrel v1.5.0
github.com/bool64/ctxd v0.1.3
github.com/bool64/dev v0.1.12
github.com/jmoiron/sqlx v1.2.0
github.com/bool64/dev v0.1.19
github.com/jmoiron/sqlx v1.3.1
github.com/stretchr/testify v1.6.1
google.golang.org/appengine v1.6.7 // indirect
)
29 changes: 10 additions & 19 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ github.com/bool64/ctxd v0.1.3 h1:n+aQ6UdoZXlltETFXqIhZR2DoMxhMYE2CW9BQn8aNBY=
github.com/bool64/ctxd v0.1.3/go.mod h1:rhUkoNE4mKFSJmo9l+78u2j+FVQifRCj0MHRhyZ2GDA=
github.com/bool64/dev v0.1.0/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
github.com/bool64/dev v0.1.10/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
github.com/bool64/dev v0.1.12 h1:mbuWBtCtOGwqt2lN1/9oPmn70XaOHdv2YHzQ31Zf9ks=
github.com/bool64/dev v0.1.12/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
github.com/bool64/dev v0.1.19 h1:SLAOGmJIrbroYjkGuV0MBLtww2NddzPDne1C7ATJdJk=
github.com/bool64/dev v0.1.19/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.1 h1:aLN7YINNZ7cYOPK3QC83dbM6KT0NMqVMw961TqrejlE=
github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -33,14 +32,6 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/swaggest/usecase v0.0.0-20200928062416-27f47131b0f8 h1:XGJJai6ngqYpy/cTMvGreiO+jradIn1uq7Q5hY2OCF4=
github.com/swaggest/usecase v0.0.0-20200928062416-27f47131b0f8/go.mod h1:rcngDv7OaBXZyEXdEtimcDeNon7sq3iqLm9hxT06s3c=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
Loading

0 comments on commit 953f461

Please sign in to comment.