Skip to content

Commit

Permalink
Add columns exporter to referencer (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jul 2, 2021
1 parent 25a9770 commit 7b69840
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/builds/releases/download/sccdiff-v0/sccdiff && chmod +x sccdiff
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: golangci/golangci-lint-action@v2.5.2
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.39.0
version: v1.40.1

# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ linters:
- forcetypeassert
- scopelint # deprecated
- ifshort # too many false positives
- golint # deprecated

issues:
exclude-use-default: false
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.39.0" # Optional configuration to pinpoint golangci-lint version.
#GOLANGCI_LINT_VERSION := "v1.40.1" # Optional configuration to pinpoint golangci-lint version.

# The head of Makefile determines location of dev-go to include standard targets.
GO ?= go
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.13
require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Masterminds/squirrel v1.5.0
github.com/bool64/ctxd v0.1.5
github.com/bool64/dev v0.1.32
github.com/bool64/ctxd v1.0.0
github.com/bool64/dev v0.1.35
github.com/jmoiron/sqlx v1.3.4
github.com/stretchr/testify v1.6.1
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20O
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Masterminds/squirrel v1.5.0 h1:JukIZisrUXadA9pl3rMkjhiamxiB0cXiu+HGp/Y8cY8=
github.com/Masterminds/squirrel v1.5.0/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/bool64/ctxd v0.1.5 h1:ilZo2AgQ+NG9NyusoYEdcMcGgleAFDnrju+5KMxwUbk=
github.com/bool64/ctxd v0.1.5/go.mod h1:+rjDVFNOJeO+xlvMqQfG0p53CzuRB7FhPSo5nWSkpQ0=
github.com/bool64/ctxd v1.0.0 h1:Btvo6BU5FulG7H2V9m5Il/2vcqT0nxe86AiEpbG08/I=
github.com/bool64/ctxd v1.0.0/go.mod h1:+rjDVFNOJeO+xlvMqQfG0p53CzuRB7FhPSo5nWSkpQ0=
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.28/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.32 h1:/9dpNGF/73diJzQxZS2hOxOPrsbTyLhr0VrQztylSKY=
github.com/bool64/dev v0.1.32/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.35 h1:uouBAq2kAJ+k9UypYRs118bAYttNQWDyK4IzjfLb5fc=
github.com/bool64/dev v0.1.35/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=
Expand Down
12 changes: 8 additions & 4 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

var (
errFieldNotFound = errors.New("could not find field value in struct")
errNotAPointer = errors.New("can not take address of structure, please pass a pointer")
errNilArgument = errors.New("structPtr and fieldPtr are required")
errUnknownFieldOrRow = errors.New("unknown field or row or not a pointer")
errNotAPointer = errors.New("can not take address of structure, please pass a pointer")
errNilArgument = errors.New("structPtr and fieldPtr are required")
)

// Mapper prepares select, insert and update statements.
Expand Down Expand Up @@ -354,7 +354,7 @@ func (sm *Mapper) FindColumnName(structPtr, fieldPtr interface{}) (string, error
}
}

return "", errFieldNotFound
return "", errUnknownFieldOrRow
}

func (sm *Mapper) typeMap(t reflect.Type) *reflectx.StructMap {
Expand Down Expand Up @@ -424,6 +424,10 @@ func (sm *Mapper) FindColumnNames(structPtr interface{}) (map[interface{}]string

tm := sm.typeMap(t)
for _, fi := range tm.Index {
if fi.Embedded {
continue
}

fv := reflectx.FieldByIndexesReadOnly(v, fi.Index)
res[fv.Addr().Interface()] = fi.Name
}
Expand Down
2 changes: 1 addition & 1 deletion mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func TestMapper_FindColumnName(t *testing.T) {
{&s, &s.B, "b", ""},
{&s, &s.C, "c", ""},
{nil, nil, "", "structPtr and fieldPtr are required"},
{&s, 123, "", "could not find field value in struct"},
{&s, 123, "", "unknown field or row or not a pointer"},
{s, &s.A, "", "can not take address of structure, please pass a pointer"},
} {
tc := tc
Expand Down
37 changes: 35 additions & 2 deletions referencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqluct

import (
"fmt"
"sort"
"strings"
)

Expand Down Expand Up @@ -58,7 +59,8 @@ type Referencer struct {
// Default QuoteNoop.
IdentifierQuoter func(tableAndColumn ...string) string

refs map[interface{}]string
refs map[interface{}]string
columns map[interface{}][]string
}

// ColumnsOf makes a Mapper option to prefix columns with table alias.
Expand Down Expand Up @@ -95,10 +97,32 @@ func (r *Referencer) AddTableAlias(rowStructPtr interface{}, alias string) {
r.refs = make(map[interface{}]string, len(f)+1)
}

if r.columns == nil {
r.columns = make(map[interface{}][]string)
}

if alias != "" {
r.refs[rowStructPtr] = r.Q(alias)
}

columns := make([]string, 0, len(f))

for _, fieldName := range f {
var col string

if alias == "" {
col = r.Q(fieldName)
} else {
col = r.Q(alias, fieldName)
}

columns = append(columns, col)
}

sort.Strings(columns)

r.columns[rowStructPtr] = columns

for ptr, fieldName := range f {
if alias == "" {
r.refs[ptr] = r.Q(fieldName)
Expand All @@ -125,7 +149,7 @@ func (r *Referencer) Ref(ptr interface{}) string {
return ref
}

panic(errFieldNotFound)
panic(errUnknownFieldOrRow)
}

// Fmt formats according to a format specified replacing ptrs with their reference strings where possible.
Expand All @@ -144,3 +168,12 @@ func (r *Referencer) Fmt(format string, ptrs ...interface{}) string {

return fmt.Sprintf(format, args...)
}

// Cols returns column references of a row structure.
func (r *Referencer) Cols(ptr interface{}) []string {
if cols, found := r.columns[ptr]; found {
return cols
}

panic(errUnknownFieldOrRow)
}
24 changes: 24 additions & 0 deletions referencer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ func TestReferencer_Ref(t *testing.T) {
})
}

func TestReferencer_Cols(t *testing.T) {
rf := sqluct.Referencer{}
rf.IdentifierQuoter = sqluct.QuoteBackticks

type r struct {
ID int `db:"id,omitempty"`
Name string `db:"name"`
}

row := &r{}
row2 := &r{}
unknown := &r{}

rf.AddTableAlias(row, "some_table")
rf.AddTableAlias(row2, "")

assert.Equal(t, []string{"`some_table`.`id`", "`some_table`.`name`"}, rf.Cols(row))
assert.Equal(t, []string{"`id`", "`name`"}, rf.Cols(row2))

assert.Panics(t, func() {
rf.Cols(unknown)
})
}

func TestQuoteNoop(t *testing.T) {
assert.Equal(t, "one.two", sqluct.QuoteNoop("one", "two"))
assert.Equal(t, "", sqluct.QuoteNoop())
Expand Down

0 comments on commit 7b69840

Please sign in to comment.