Skip to content

Commit

Permalink
Merge pull request #18 from bool64/json-container
Browse files Browse the repository at this point in the history
Add JSON marshaler for JSON container
  • Loading branch information
vearutop authored Apr 5, 2024
2 parents 651defe + a7bee2c commit ca130b3
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:
GO111MODULE: "on"
CACHE_BENCHMARK: "off" # Enables benchmark result reuse between runs, may skew latency results.
RUN_BASE_BENCHMARK: "on" # Runs benchmark for PR base in case benchmark result is missing.
GO_VERSION: 1.21.x
GO_VERSION: 1.22.x
jobs:
bench:
runs-on: ubuntu-latest
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.21.x
go-version: 1.22.x
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.7.0
uses: golangci/golangci-lint-action@v4.0.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.54.1
version: v1.56.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gorelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ concurrency:
cancel-in-progress: true

env:
GO_VERSION: 1.21.x
GO_VERSION: 1.22.x
jobs:
gorelease:
runs-on: ubuntu-latest
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ concurrency:
env:
GO111MODULE: "on"
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
COV_GO_VERSION: 1.21.x # Version of Go to collect coverage
COV_GO_VERSION: 1.22.x # Version of Go to collect coverage
TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents
jobs:
test:
strategy:
matrix:
go-version: [ 1.13.x, 1.20.x, 1.21.x ]
go-version: [ 1.13.x, 1.21.x, 1.22.x ]
runs-on: ubuntu-latest
steps:
- name: Install Go stable
Expand Down Expand Up @@ -88,9 +88,9 @@ jobs:
id: annotate
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
run: |
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.4.0/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.4.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz
gocovdiff_hash=$(git hash-object ./gocovdiff)
[ "$gocovdiff_hash" == "f191b45548bb65ec2c7d88909679a57116ff1ba1" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1)
[ "$gocovdiff_hash" == "c37862c73a677e5a9c069470287823ab5bbf0244" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1)
git fetch origin master ${{ github.event.pull_request.base.sha }}
REP=$(./gocovdiff -mod github.com/$GITHUB_REPOSITORY -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
echo "${REP}"
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ linters-settings:
linters:
enable-all: true
disable:
- musttag
- lll
- maligned
- gochecknoglobals
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#GOLANGCI_LINT_VERSION := "v1.54.1" # Optional configuration to pinpoint golangci-lint version.
#GOLANGCI_LINT_VERSION := "v1.56.2" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func ExampleStorage_InTx() {
ctx context.Context
)

err := s.InTx(ctx, func(ctx context.Context) error {
err := s.InTx(ctx, func(_ context.Context) error {
return nil
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/bool64/sqluct
go 1.18

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/Masterminds/squirrel v1.5.4
github.com/bool64/ctxd v1.2.1
github.com/bool64/dev v0.2.31
github.com/bool64/dev v0.2.34
github.com/jmoiron/sqlx v1.3.5
github.com/stretchr/testify v1.8.2
)
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/bool64/ctxd v1.2.1 h1:hARFteq0zdn4bwfmxLhak3fXFuvtJVKDH2X29VV/2ls=
github.com/bool64/ctxd v1.2.1/go.mod h1:ZG6QkeGVLTiUl2mxPpyHmFhDzFZCyocr9hluBV3LYuc=
github.com/bool64/dev v0.2.31 h1:OS57EqYaYe2M/2bw9uhDCIFiZZwywKFS/4qMLN6JUmQ=
github.com/bool64/dev v0.2.31/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.34 h1:P9n315P8LdpxusnYQ0X7MP1CZXwBK5ae5RZrd+GdSZE=
github.com/bool64/dev v0.2.34/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
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.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
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=
Expand Down
36 changes: 19 additions & 17 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Masterminds/squirrel"
"github.com/bool64/sqluct"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type (
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestInsertValue(t *testing.T) {
sm := sqluct.Mapper{}
q := sm.Insert(ps.Insert("sample"), z)
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "INSERT INTO sample (a,meta,e,b,c) VALUES ($1,$2,$3,$4,$5)", query)
assert.Equal(t, []interface{}{1, AnotherRow{SampleEmbedded: SampleEmbedded{B: 0, C: ""}, D: ""}, "e!", 2.2, "3"}, args)
}
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestInsertValue_omitempty(t *testing.T) {
sm := sqluct.Mapper{}
q := sm.Insert(ps.Insert("sample"), z)
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
// a and e are missing for `omitempty`
assert.Equal(t, "INSERT INTO sample (meta,b,c) VALUES ($1,$2,$3)", query)
assert.Equal(t, []interface{}{AnotherRow{SampleEmbedded: SampleEmbedded{B: 0, C: ""}, D: ""}, 0.0, ""}, args)
Expand Down Expand Up @@ -120,7 +121,7 @@ func TestInsertValue_IgnoreOmitEmpty(t *testing.T) {
sm := sqluct.Mapper{}
q := sm.Insert(ps.Insert("sample"), z, sqluct.IgnoreOmitEmpty)
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
// a and e are missing for `omitempty`
assert.Equal(t, "INSERT INTO sample (a,meta,e,b,c) VALUES ($1,$2,$3,$4,$5)", query)
assert.Equal(t, []interface{}{0, AnotherRow{SampleEmbedded: SampleEmbedded{B: 0, C: ""}, D: ""}, "", 0.0, ""}, args)
Expand Down Expand Up @@ -174,7 +175,7 @@ func TestInsertValueSlice_heterogeneous(t *testing.T) {
assert.Equal(t, q, sm.Insert(q, nil))
q = sm.Insert(q, z)
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "INSERT INTO sample (a,meta,e,b,c) VALUES ($1,$2,$3,$4,$5),($6,$7,$8,$9,$10)", query)
assert.Equal(t, []interface{}{
0,
Expand Down Expand Up @@ -216,7 +217,7 @@ func TestInsertValueSlice_homogeneous(t *testing.T) {
assert.Equal(t, q, sm.Insert(q, nil))
q = sm.Insert(q, z)
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "INSERT INTO sample (a,meta,e,b,c) VALUES ($1,$2,$3,$4,$5),($6,$7,$8,$9,$10)", query)
assert.Equal(t, []interface{}{
1,
Expand Down Expand Up @@ -338,7 +339,7 @@ func TestInsertValueSlicePtr(t *testing.T) {
sm := sqluct.Mapper{}
q := sm.Insert(ps.Insert("sample"), z)
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "INSERT INTO sample (a,meta,e,b,c) VALUES ($1,$2,$3,$4,$5),($6,$7,$8,$9,$10)", query)
assert.Equal(t, []interface{}{
1,
Expand Down Expand Up @@ -369,7 +370,7 @@ func TestMapper_Update(t *testing.T) {
q := sm.Update(ps.Update("sample"), z)
q = q.Where(sm.WhereEq(condition))
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "UPDATE sample SET b = $1, c = $2 WHERE a = $3 AND b IN ($4,$5)", query)
assert.Equal(t, []interface{}{2.2, "3", 1, "b1", "b2"}, args)
}
Expand All @@ -382,7 +383,7 @@ func TestMapper_Select_struct(t *testing.T) {
q = q.From("sample")

query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "SELECT a, meta, e, b, c FROM sample", query)
assert.Equal(t, []interface{}(nil), args)
}
Expand Down Expand Up @@ -421,7 +422,7 @@ func TestMapper_Select_slice(t *testing.T) {
q = q.Where(sm.WhereEq(condition))
q = q.From("sample")
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "SELECT b, c FROM sample WHERE a = $1 AND b IN ($2,$3)", query)
assert.Equal(t, []interface{}{1, "b1", "b2"}, args)
}
Expand Down Expand Up @@ -453,7 +454,7 @@ func TestMapper_WhereEq(t *testing.T) {
q = q.Where(sm.WhereEq(filter, sqluct.SkipZeroValues))

query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "SELECT campaign, variation, fk_customer, created_at FROM sample WHERE fk_customer = $1", query)
assert.Equal(t, []interface{}{uint64(123)}, args)

Expand All @@ -462,7 +463,7 @@ func TestMapper_WhereEq(t *testing.T) {
q = sm.Select(q, rows)
q = q.Where(sm.WhereEq(filter, sqluct.SkipZeroValues))
query, args, err = q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "SELECT campaign, variation, fk_customer, created_at FROM sample WHERE campaign IN ($1,$2) AND fk_customer = $3", query)
assert.Equal(t, []interface{}{"k1", "k2", uint64(123)}, args)

Expand All @@ -471,7 +472,7 @@ func TestMapper_WhereEq(t *testing.T) {
q = sm.Select(q, rows)
q = q.Where(sm.WhereEq(filter, sqluct.SkipZeroValues))
query, args, err = q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "SELECT campaign, variation, fk_customer, created_at FROM sample WHERE campaign IN ($1,$2)", query)
assert.Equal(t, []interface{}{"k1", "k2"}, args)

Expand All @@ -480,7 +481,7 @@ func TestMapper_WhereEq(t *testing.T) {
q = sm.Select(q, rows)
q = q.Where(sm.WhereEq(filter, sqluct.SkipZeroValues))
query, args, err = q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "SELECT campaign, variation, fk_customer, created_at FROM sample WHERE (1=1)", query)
assert.Equal(t, []interface{}(nil), args)
}
Expand All @@ -499,7 +500,7 @@ func TestMapper_Delete(t *testing.T) {
sm := sqluct.Mapper{}
q := ps.Delete("sample").Where(sm.WhereEq(condition, sqluct.SkipZeroValues))
query, args, err := q.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "DELETE FROM sample WHERE a = $1 AND b IN ($2,$3)", query)
assert.Equal(t, []interface{}{1, "b1", "b2"}, args)
}
Expand Down Expand Up @@ -541,10 +542,11 @@ func TestMapper_FindColumnName(t *testing.T) {
t.Run("", func(t *testing.T) {
tagValue, err := sm.FindColumnName(tc.structPtr, tc.fieldPtr)
assert.Equal(t, tc.tagValue, tagValue)

if tc.err == "" {
assert.NoError(t, err)
require.NoError(t, err)
} else {
assert.EqualError(t, err, tc.err)
require.EqualError(t, err, tc.err)
}
})
}
Expand Down Expand Up @@ -628,6 +630,6 @@ func assertStatement(t *testing.T, s string, qb sqluct.ToSQL) {
t.Helper()

stmt, _, err := qb.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, s, stmt)
}
3 changes: 2 additions & 1 deletion referencer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Masterminds/squirrel"
"github.com/bool64/sqluct"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReferencer_Fmt(t *testing.T) {
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestReferencer_Fmt(t *testing.T) {
Where(squirrel.NotEq(m.WhereEq(User{FirstName: "Sergey", LastName: "Brin"}, rf.ColumnsOf("manager"))))

stmt, args, err := qb.ToSql()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, `SELECT dr.manager_id, dr.employee_id `+
`FROM users AS manager `+
`INNER JOIN direct_reports AS dr ON dr.manager_id = manager.id AND dr.employee_id = employee.id `+
Expand Down
4 changes: 2 additions & 2 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func (s *Storage) InTx(ctx context.Context, fn func(context.Context) error) (err
ctx = TxToContext(ctx, tx)
} else {
// Do nothing because parent tx is still running and
// this is not the beginner so it can't be the finisher.
finish = func(ctx context.Context, err error) error {
// this is not the beginner, so it can't be the finisher.
finish = func(_ context.Context, err error) error {
return err
}
}
Expand Down
10 changes: 10 additions & 0 deletions storage_go1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ type JSON[V any] struct {
Val V
}

// UnmarshalJSON decodes JSON into container.
func (s *JSON[V]) UnmarshalJSON(bytes []byte) error {
return json.Unmarshal(bytes, &s.Val)
}

// MarshalJSON encodes container value as JSON.
func (s JSON[V]) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Val)
}

// Scan decodes json value from a db column.
func (s *JSON[V]) Scan(src any) error {
if src == nil {
Expand Down
Loading

0 comments on commit ca130b3

Please sign in to comment.