From 7fedfbca81e5cae5897b83b6e4657820cbb293bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Mon, 12 Feb 2024 18:17:49 +0100 Subject: [PATCH] v0.0.387 bf --- go.mod | 2 +- go.sum | 2 ++ goextVersion.go | 4 ++-- sq/scanner.go | 20 ++++++++++++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index d580a30..db981e4 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.17.0 // indirect + github.com/go-playground/validator/v10 v10.18.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/uuid v1.5.0 // indirect diff --git a/go.sum b/go.sum index c2175fb..dd3839a 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqR github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-playground/validator/v10 v10.17.0 h1:SmVVlfAOtlZncTxRuinDPomC2DkXJ4E5T9gDA0AIH74= github.com/go-playground/validator/v10 v10.17.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.18.0 h1:BvolUXjp4zuvkZ5YN5t7ebzbhlUtPsPm2S9NAZ5nl9U= +github.com/go-playground/validator/v10 v10.18.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= 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/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= diff --git a/goextVersion.go b/goextVersion.go index c5180b2..93a0a2d 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.386" +const GoextVersion = "0.0.387" -const GoextVersionTimestamp = "2024-02-09T15:58:21+0100" +const GoextVersionTimestamp = "2024-02-12T18:17:49+0100" diff --git a/sq/scanner.go b/sq/scanner.go index 97e61dd..9ff33ef 100644 --- a/sq/scanner.go +++ b/sq/scanner.go @@ -44,7 +44,7 @@ func InsertAndQuerySingle[TData any](ctx context.Context, q Queryable, tableName rval := reflect.ValueOf(v) - idRVal := rval.FieldByName(idColumn) + idRVal := fieldByTag(rval, "db", idColumn) if !idRVal.IsValid() || idRVal.IsZero() { return *new(TData), fmt.Errorf("failed to find idColumn '%s' in %T", idColumn, v) } @@ -67,6 +67,22 @@ func InsertAndQuerySingle[TData any](ctx context.Context, q Queryable, tableName return QuerySingle[TData](ctx, q, sqlstr, pp, mode, sec) } +func fieldByTag(rval reflect.Value, tagkey string, tagval string) reflect.Value { + rtyp := rval.Type() + for i := 0; i < rtyp.NumField(); i++ { + rsfield := rtyp.Field(i) + + if !rsfield.IsExported() { + continue + } + + if rsfield.Tag.Get(tagkey) == tagval { + return rval.Field(i) + } + } + panic(fmt.Sprintf("tag %s = '%s' not found in %s", tagkey, tagval, rtyp.Name())) +} + func InsertMultiple[TData any](ctx context.Context, q Queryable, tableName string, vArr []TData, maxBatch int) ([]sql.Result, error) { if len(vArr) == 0 { @@ -122,7 +138,7 @@ func UpdateAndQuerySingle[TData any](ctx context.Context, q Queryable, tableName rval := reflect.ValueOf(v) - idRVal := rval.FieldByName(idColumn) + idRVal := fieldByTag(rval, "db", idColumn) if !idRVal.IsValid() || idRVal.IsZero() { return *new(TData), fmt.Errorf("failed to find idColumn '%s' in %T", idColumn, v) }