Rename kmodifiers to ksqlmodifiers

pull/29/head adapters/kmysql/v1.4.9
Vinícius Garcia 2022-10-18 12:57:27 -03:00
parent a7f12b34a5
commit dd8a45c5d5
14 changed files with 54 additions and 54 deletions

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"database/sql/driver" "database/sql/driver"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
// AttrScanWrapper is the wrapper that allow us to intercept the Scan process // AttrScanWrapper is the wrapper that allow us to intercept the Scan process
@ -17,8 +17,8 @@ type AttrScanWrapper struct {
AttrPtr interface{} AttrPtr interface{}
ScanFn kmodifiers.AttrScanner ScanFn ksqlmodifiers.AttrScanner
OpInfo kmodifiers.OpInfo OpInfo ksqlmodifiers.OpInfo
} }
// Scan implements the sql.Scanner interface // Scan implements the sql.Scanner interface
@ -36,8 +36,8 @@ type AttrValueWrapper struct {
Attr interface{} Attr interface{}
ValueFn kmodifiers.AttrValuer ValueFn ksqlmodifiers.AttrValuer
OpInfo kmodifiers.OpInfo OpInfo ksqlmodifiers.OpInfo
} }
// Value implements the sql.Valuer interface // Value implements the sql.Valuer interface

View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
func TestAttrScanWrapper(t *testing.T) { func TestAttrScanWrapper(t *testing.T) {
@ -16,7 +16,7 @@ func TestAttrScanWrapper(t *testing.T) {
wrapper := AttrScanWrapper{ wrapper := AttrScanWrapper{
Ctx: ctx, Ctx: ctx,
AttrPtr: "fakeAttrPtr", AttrPtr: "fakeAttrPtr",
ScanFn: func(ctx context.Context, opInfo kmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error { ScanFn: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error {
scanArgs = map[string]interface{}{ scanArgs = map[string]interface{}{
"opInfo": opInfo, "opInfo": opInfo,
"attrPtr": attrPtr, "attrPtr": attrPtr,
@ -24,7 +24,7 @@ func TestAttrScanWrapper(t *testing.T) {
} }
return errors.New("fakeScanErrMsg") return errors.New("fakeScanErrMsg")
}, },
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },
@ -33,7 +33,7 @@ func TestAttrScanWrapper(t *testing.T) {
err := wrapper.Scan("fakeDbValue") err := wrapper.Scan("fakeDbValue")
tt.AssertErrContains(t, err, "fakeScanErrMsg") tt.AssertErrContains(t, err, "fakeScanErrMsg")
tt.AssertEqual(t, scanArgs, map[string]interface{}{ tt.AssertEqual(t, scanArgs, map[string]interface{}{
"opInfo": kmodifiers.OpInfo{ "opInfo": ksqlmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },
@ -49,14 +49,14 @@ func TestAttrWrapper(t *testing.T) {
wrapper := AttrValueWrapper{ wrapper := AttrValueWrapper{
Ctx: ctx, Ctx: ctx,
Attr: "fakeAttr", Attr: "fakeAttr",
ValueFn: func(ctx context.Context, opInfo kmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { ValueFn: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
valueArgs = map[string]interface{}{ valueArgs = map[string]interface{}{
"opInfo": opInfo, "opInfo": opInfo,
"inputValue": inputValue, "inputValue": inputValue,
} }
return "fakeOutputValue", errors.New("fakeValueErrMsg") return "fakeOutputValue", errors.New("fakeValueErrMsg")
}, },
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },
@ -65,7 +65,7 @@ func TestAttrWrapper(t *testing.T) {
value, err := wrapper.Value() value, err := wrapper.Value()
tt.AssertErrContains(t, err, "fakeValueErrMsg") tt.AssertErrContains(t, err, "fakeValueErrMsg")
tt.AssertEqual(t, valueArgs, map[string]interface{}{ tt.AssertEqual(t, valueArgs, map[string]interface{}{
"opInfo": kmodifiers.OpInfo{ "opInfo": ksqlmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
// Here we keep all the registered modifiers // Here we keep all the registered modifiers
@ -13,7 +13,7 @@ var modifiers sync.Map
func init() { func init() {
// Here we expose the registration function in a public package, // Here we expose the registration function in a public package,
// so users can use it: // so users can use it:
kmodifiers.RegisterAttrModifier = RegisterAttrModifier ksqlmodifiers.RegisterAttrModifier = RegisterAttrModifier
// These are the builtin modifiers: // These are the builtin modifiers:
@ -33,7 +33,7 @@ func init() {
// RegisterAttrModifier allow users to add custom modifiers on startup // RegisterAttrModifier allow users to add custom modifiers on startup
// it is recommended to do this inside an init() function. // it is recommended to do this inside an init() function.
func RegisterAttrModifier(key string, modifier kmodifiers.AttrModifier) { func RegisterAttrModifier(key string, modifier ksqlmodifiers.AttrModifier) {
_, found := modifiers.Load(key) _, found := modifiers.Load(key)
if found { if found {
panic(fmt.Errorf("KSQL: cannot register modifier '%s' name is already in use", key)) panic(fmt.Errorf("KSQL: cannot register modifier '%s' name is already in use", key))
@ -44,11 +44,11 @@ func RegisterAttrModifier(key string, modifier kmodifiers.AttrModifier) {
// LoadGlobalModifier is used internally by KSQL to load // LoadGlobalModifier is used internally by KSQL to load
// modifiers during runtime. // modifiers during runtime.
func LoadGlobalModifier(key string) (kmodifiers.AttrModifier, error) { func LoadGlobalModifier(key string) (ksqlmodifiers.AttrModifier, error) {
rawModifier, _ := modifiers.Load(key) rawModifier, _ := modifiers.Load(key)
modifier, ok := rawModifier.(kmodifiers.AttrModifier) modifier, ok := rawModifier.(ksqlmodifiers.AttrModifier)
if !ok { if !ok {
return kmodifiers.AttrModifier{}, fmt.Errorf("no modifier found with name '%s'", key) return ksqlmodifiers.AttrModifier{}, fmt.Errorf("no modifier found with name '%s'", key)
} }
return modifier, nil return modifier, nil

View File

@ -4,15 +4,15 @@ import (
"testing" "testing"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
func TestRegisterAttrModifier(t *testing.T) { func TestRegisterAttrModifier(t *testing.T) {
t.Run("should register new modifiers correctly", func(t *testing.T) { t.Run("should register new modifiers correctly", func(t *testing.T) {
modifier1 := kmodifiers.AttrModifier{ modifier1 := ksqlmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
} }
modifier2 := kmodifiers.AttrModifier{ modifier2 := ksqlmodifiers.AttrModifier{
SkipOnInsert: true, SkipOnInsert: true,
} }
@ -29,10 +29,10 @@ func TestRegisterAttrModifier(t *testing.T) {
}) })
t.Run("should panic registering a modifier and the name already exists", func(t *testing.T) { t.Run("should panic registering a modifier and the name already exists", func(t *testing.T) {
modifier1 := kmodifiers.AttrModifier{ modifier1 := ksqlmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
} }
modifier2 := kmodifiers.AttrModifier{ modifier2 := ksqlmodifiers.AttrModifier{
SkipOnInsert: true, SkipOnInsert: true,
} }
@ -49,6 +49,6 @@ func TestRegisterAttrModifier(t *testing.T) {
t.Run("should return an error when loading an inexistent modifier", func(t *testing.T) { t.Run("should return an error when loading an inexistent modifier", func(t *testing.T) {
mod, err := LoadGlobalModifier("nonExistentModifier") mod, err := LoadGlobalModifier("nonExistentModifier")
tt.AssertErrContains(t, err, "nonExistentModifier") tt.AssertErrContains(t, err, "nonExistentModifier")
tt.AssertEqual(t, mod, kmodifiers.AttrModifier{}) tt.AssertEqual(t, mod, ksqlmodifiers.AttrModifier{})
}) })
} }

View File

@ -5,14 +5,14 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
// This modifier serializes objects as JSON when // This modifier serializes objects as JSON when
// sending it to the database and decodes // sending it to the database and decodes
// them when receiving. // them when receiving.
var jsonModifier = kmodifiers.AttrModifier{ var jsonModifier = ksqlmodifiers.AttrModifier{
Scan: func(ctx context.Context, opInfo kmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error { Scan: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error {
if dbValue == nil { if dbValue == nil {
return nil return nil
} }
@ -29,7 +29,7 @@ var jsonModifier = kmodifiers.AttrModifier{
return json.Unmarshal(rawJSON, attrPtr) return json.Unmarshal(rawJSON, attrPtr)
}, },
Value: func(ctx context.Context, opInfo kmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { Value: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
b, err := json.Marshal(inputValue) b, err := json.Marshal(inputValue)
// SQL server uses the NVARCHAR type to store JSON and // SQL server uses the NVARCHAR type to store JSON and
// it expects to receive strings not []byte, thus: // it expects to receive strings not []byte, thus:

View File

@ -5,7 +5,7 @@ import (
"testing" "testing"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
func TestAttrScan(t *testing.T) { func TestAttrScan(t *testing.T) {
@ -54,7 +54,7 @@ func TestAttrScan(t *testing.T) {
fakeAttr := FakeAttr{ fakeAttr := FakeAttr{
Foo: "notZeroValue", Foo: "notZeroValue",
} }
err := jsonModifier.Scan(ctx, kmodifiers.OpInfo{}, &fakeAttr, test.dbInput) err := jsonModifier.Scan(ctx, ksqlmodifiers.OpInfo{}, &fakeAttr, test.dbInput)
if test.expectErrToContain != nil { if test.expectErrToContain != nil {
tt.AssertErrContains(t, err, test.expectErrToContain...) tt.AssertErrContains(t, err, test.expectErrToContain...)
t.Skip() t.Skip()
@ -76,7 +76,7 @@ func TestAttrValue(t *testing.T) {
tests := []struct { tests := []struct {
desc string desc string
dbInput interface{} dbInput interface{}
opInfoInput kmodifiers.OpInfo opInfoInput ksqlmodifiers.OpInfo
attrValue interface{} attrValue interface{}
expectedOutput interface{} expectedOutput interface{}
@ -85,7 +85,7 @@ func TestAttrValue(t *testing.T) {
{ {
desc: "should return a byte array when the driver is not sqlserver", desc: "should return a byte array when the driver is not sqlserver",
dbInput: []byte(`{"foo":"bar"}`), dbInput: []byte(`{"foo":"bar"}`),
opInfoInput: kmodifiers.OpInfo{ opInfoInput: ksqlmodifiers.OpInfo{
DriverName: "notSQLServer", DriverName: "notSQLServer",
}, },
attrValue: FakeAttr{ attrValue: FakeAttr{
@ -98,7 +98,7 @@ func TestAttrValue(t *testing.T) {
{ {
desc: "should return a string when the driver is sqlserver", desc: "should return a string when the driver is sqlserver",
dbInput: []byte(`{"foo":"bar"}`), dbInput: []byte(`{"foo":"bar"}`),
opInfoInput: kmodifiers.OpInfo{ opInfoInput: ksqlmodifiers.OpInfo{
DriverName: "sqlserver", DriverName: "sqlserver",
}, },
attrValue: FakeAttr{ attrValue: FakeAttr{

View File

@ -1,11 +1,11 @@
package modifiers package modifiers
import "github.com/vingarcia/ksql/kmodifiers" import "github.com/vingarcia/ksql/ksqlmodifiers"
var skipInsertsModifier = kmodifiers.AttrModifier{ var skipInsertsModifier = ksqlmodifiers.AttrModifier{
SkipOnInsert: true, SkipOnInsert: true,
} }
var skipUpdatesModifier = kmodifiers.AttrModifier{ var skipUpdatesModifier = ksqlmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
} }

View File

@ -4,21 +4,21 @@ import (
"context" "context"
"time" "time"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
// This one is useful for updatedAt timestamps // This one is useful for updatedAt timestamps
var timeNowUTCModifier = kmodifiers.AttrModifier{ var timeNowUTCModifier = ksqlmodifiers.AttrModifier{
Value: func(ctx context.Context, opInfo kmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { Value: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
return time.Now().UTC(), nil return time.Now().UTC(), nil
}, },
} }
// This one is useful for createdAt timestamps // This one is useful for createdAt timestamps
var timeNowUTCSkipUpdatesModifier = kmodifiers.AttrModifier{ var timeNowUTCSkipUpdatesModifier = ksqlmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
Value: func(ctx context.Context, opInfo kmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { Value: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
return time.Now().UTC(), nil return time.Now().UTC(), nil
}, },
} }

View File

@ -7,7 +7,7 @@ import (
"sync" "sync"
"github.com/vingarcia/ksql/internal/modifiers" "github.com/vingarcia/ksql/internal/modifiers"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
) )
// StructInfo stores metainformation of the struct // StructInfo stores metainformation of the struct
@ -26,7 +26,7 @@ type FieldInfo struct {
Name string Name string
Index int Index int
Valid bool Valid bool
Modifier kmodifiers.AttrModifier Modifier ksqlmodifiers.AttrModifier
} }
// ByIndex returns either the *FieldInfo of a valid // ByIndex returns either the *FieldInfo of a valid
@ -252,7 +252,7 @@ func getTagNames(t reflect.Type) (_ StructInfo, err error) {
} }
tags := strings.Split(name, ",") tags := strings.Split(name, ",")
var modifier kmodifiers.AttrModifier var modifier ksqlmodifiers.AttrModifier
if len(tags) > 1 { if len(tags) > 1 {
name = tags[0] name = tags[0]
modifier, err = modifiers.LoadGlobalModifier(tags[1]) modifier, err = modifiers.LoadGlobalModifier(tags[1])

10
ksql.go
View File

@ -12,7 +12,7 @@ import (
"github.com/vingarcia/ksql/internal/modifiers" "github.com/vingarcia/ksql/internal/modifiers"
"github.com/vingarcia/ksql/internal/structs" "github.com/vingarcia/ksql/internal/structs"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
"github.com/vingarcia/ksql/ksqltest" "github.com/vingarcia/ksql/ksqltest"
) )
@ -729,7 +729,7 @@ func buildInsertQuery(
Ctx: ctx, Ctx: ctx,
Attr: recordValue, Attr: recordValue,
ValueFn: valueFn, ValueFn: valueFn,
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
Method: "Insert", Method: "Insert",
}, },
@ -858,7 +858,7 @@ func buildUpdateQuery(
Ctx: ctx, Ctx: ctx,
Attr: recordValue, Attr: recordValue,
ValueFn: valueFn, ValueFn: valueFn,
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
Method: "Update", Method: "Update",
}, },
@ -1064,7 +1064,7 @@ func getScanArgsForNestedStructs(
Ctx: ctx, Ctx: ctx,
AttrPtr: valueScanner, AttrPtr: valueScanner,
ScanFn: fieldInfo.Modifier.Scan, ScanFn: fieldInfo.Modifier.Scan,
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
// We will not differentiate between Query, QueryOne and QueryChunks // We will not differentiate between Query, QueryOne and QueryChunks
// if we did this could lead users to make very strange modifiers // if we did this could lead users to make very strange modifiers
@ -1094,7 +1094,7 @@ func getScanArgsFromNames(ctx context.Context, dialect Dialect, names []string,
Ctx: ctx, Ctx: ctx,
AttrPtr: valueScanner, AttrPtr: valueScanner,
ScanFn: fieldInfo.Modifier.Scan, ScanFn: fieldInfo.Modifier.Scan,
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
// We will not differentiate between Query, QueryOne and QueryChunks // We will not differentiate between Query, QueryOne and QueryChunks
// if we did this could lead users to make very strange modifiers // if we did this could lead users to make very strange modifiers

View File

@ -1,4 +1,4 @@
package kmodifiers package ksqlmodifiers
import "context" import "context"

View File

@ -3,4 +3,4 @@
// //
// For understanding internal details of the code // For understanding internal details of the code
// please read the `internal/modifiers` package. // please read the `internal/modifiers` package.
package kmodifiers package ksqlmodifiers

View File

@ -1,4 +1,4 @@
package kmodifiers package ksqlmodifiers
// RegisterAttrModifier allow users to add custom modifiers on startup // RegisterAttrModifier allow users to add custom modifiers on startup
// it is recommended to do this inside an init() function. // it is recommended to do this inside an init() function.

View File

@ -11,7 +11,7 @@ import (
"github.com/vingarcia/ksql/internal/modifiers" "github.com/vingarcia/ksql/internal/modifiers"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers" "github.com/vingarcia/ksql/ksqlmodifiers"
"github.com/vingarcia/ksql/nullable" "github.com/vingarcia/ksql/nullable"
) )
@ -3336,7 +3336,7 @@ func getUserByID(db DBAdapter, dialect Dialect, result *user, id uint) error {
Ctx: context.TODO(), Ctx: context.TODO(),
AttrPtr: &result.Address, AttrPtr: &result.Address,
ScanFn: modifier.Scan, ScanFn: modifier.Scan,
OpInfo: kmodifiers.OpInfo{ OpInfo: ksqlmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
// We will not differentiate between Query, QueryOne and QueryChunks // We will not differentiate between Query, QueryOne and QueryChunks
// if we did this could lead users to make very strange modifiers // if we did this could lead users to make very strange modifiers