Skip to content

Commit

Permalink
Raise an error if given a struct with no tags (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-stytch authored Nov 22, 2023
1 parent 3b1ac1d commit 03d4ec9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
12 changes: 12 additions & 0 deletions reexports.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ type Or = sq.Or

// Sqlizer is an interface containing the ToSql method. It is a re-export of squirrel.Sqlizer.
type Sqlizer = sq.Sqlizer

// Gt represents a SQL > expression. It is a re-export of squirrel.Gt.
type Gt = sq.Gt

// GtOrEq represents a SQL >= expression. It is a re-export of squirrel.GtOrEq.
type GtOrEq = sq.GtOrEq

// Lt represents a SQL < expression. It is a re-export of squirrel.Lt.
type Lt = sq.Lt

// LtOrEq represents a SQL <= expression. It is a re-export of squirrel.LtOrEq.
type LtOrEq = sq.LtOrEq
7 changes: 7 additions & 0 deletions toclause.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package sqx

import (
"errors"

scan "github.com/blockloop/scan/v2"
)

var NoDBTagsError = errors.New("No db tags detected")

// Clause stores an Eq result, but also holds an error if one occurred during the conversion. You may think of this
// struct as a (Eq, error) tuple that implements the Sqlizer interface.
type Clause struct {
Expand All @@ -28,6 +32,9 @@ func ToClause(v any, excluded ...string) *Clause {
if err != nil {
return &Clause{contents: nil, err: err}
}
if len(cols) == 0 {
return &Clause{contents: nil, err: NoDBTagsError}
}
vals, err := scan.Values(cols, v)
if err != nil {
return &Clause{contents: nil, err: err}
Expand Down
10 changes: 10 additions & 0 deletions toclause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type thingyGetFilter struct {
IntCol *[]int `db:"int_col"`
}

type thingyGetFilterWithNoTags struct {
StrCol *string
IntCol *[]int
}

func TestToClause(t *testing.T) {
filter := thingyGetFilter{
StrCol: Ptr("i am str"),
Expand Down Expand Up @@ -67,6 +72,11 @@ func TestToClause(t *testing.T) {
clause := ToClause(&thingyGetFilter{})
assert.Equal(t, expected, clause.contents)
})

t.Run("Has an error if given a struct with no db tags", func(t *testing.T) {
clause := ToClause(&thingyGetFilterWithNoTags{})
assert.Error(t, clause.err)
})
}

func TestToClauseAlias(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package sqx

const VERSION = "0.2.0"
const VERSION = "0.3.0"

0 comments on commit 03d4ec9

Please sign in to comment.