Expose some public types and functions so users can register new modifiers

pull/29/head
Vinícius Garcia 2022-10-18 12:53:25 -03:00
parent ad516d5e1f
commit 4890563c27
13 changed files with 70 additions and 43 deletions

View File

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

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers"
) )
func TestAttrScanWrapper(t *testing.T) { func TestAttrScanWrapper(t *testing.T) {
@ -15,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 OpInfo, attrPtr interface{}, dbValue interface{}) error { ScanFn: func(ctx context.Context, opInfo kmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error {
scanArgs = map[string]interface{}{ scanArgs = map[string]interface{}{
"opInfo": opInfo, "opInfo": opInfo,
"attrPtr": attrPtr, "attrPtr": attrPtr,
@ -23,7 +24,7 @@ func TestAttrScanWrapper(t *testing.T) {
} }
return errors.New("fakeScanErrMsg") return errors.New("fakeScanErrMsg")
}, },
OpInfo: OpInfo{ OpInfo: kmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },
@ -32,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": OpInfo{ "opInfo": kmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },
@ -48,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 OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { ValueFn: func(ctx context.Context, opInfo kmodifiers.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: OpInfo{ OpInfo: kmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },
@ -64,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": OpInfo{ "opInfo": kmodifiers.OpInfo{
Method: "fakeMethod", Method: "fakeMethod",
DriverName: "fakeDriverName", DriverName: "fakeDriverName",
}, },

View File

@ -3,12 +3,18 @@ package modifiers
import ( import (
"fmt" "fmt"
"sync" "sync"
"github.com/vingarcia/ksql/kmodifiers"
) )
// Here we keep all the registered modifiers // Here we keep all the registered modifiers
var modifiers sync.Map var modifiers sync.Map
func init() { func init() {
// Here we expose the registration function in a public package,
// so users can use it:
kmodifiers.RegisterAttrModifier = RegisterAttrModifier
// These are the builtin modifiers: // These are the builtin modifiers:
// This one is useful for serializing/desserializing structs: // This one is useful for serializing/desserializing structs:
@ -27,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 AttrModifier) { func RegisterAttrModifier(key string, modifier kmodifiers.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))
@ -38,11 +44,11 @@ func RegisterAttrModifier(key string, modifier 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) (AttrModifier, error) { func LoadGlobalModifier(key string) (kmodifiers.AttrModifier, error) {
rawModifier, _ := modifiers.Load(key) rawModifier, _ := modifiers.Load(key)
modifier, ok := rawModifier.(AttrModifier) modifier, ok := rawModifier.(kmodifiers.AttrModifier)
if !ok { if !ok {
return AttrModifier{}, fmt.Errorf("no modifier found with name '%s'", key) return kmodifiers.AttrModifier{}, fmt.Errorf("no modifier found with name '%s'", key)
} }
return modifier, nil return modifier, nil

View File

@ -4,14 +4,15 @@ import (
"testing" "testing"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers"
) )
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 := AttrModifier{ modifier1 := kmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
} }
modifier2 := AttrModifier{ modifier2 := kmodifiers.AttrModifier{
SkipOnInsert: true, SkipOnInsert: true,
} }
@ -28,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 := AttrModifier{ modifier1 := kmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
} }
modifier2 := AttrModifier{ modifier2 := kmodifiers.AttrModifier{
SkipOnInsert: true, SkipOnInsert: true,
} }
@ -48,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, AttrModifier{}) tt.AssertEqual(t, mod, kmodifiers.AttrModifier{})
}) })
} }

View File

@ -4,13 +4,15 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/vingarcia/ksql/kmodifiers"
) )
// 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 = AttrModifier{ var jsonModifier = kmodifiers.AttrModifier{
Scan: func(ctx context.Context, opInfo OpInfo, attrPtr interface{}, dbValue interface{}) error { Scan: func(ctx context.Context, opInfo kmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error {
if dbValue == nil { if dbValue == nil {
return nil return nil
} }
@ -27,7 +29,7 @@ var jsonModifier = AttrModifier{
return json.Unmarshal(rawJSON, attrPtr) return json.Unmarshal(rawJSON, attrPtr)
}, },
Value: func(ctx context.Context, opInfo OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { Value: func(ctx context.Context, opInfo kmodifiers.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,6 +5,7 @@ import (
"testing" "testing"
tt "github.com/vingarcia/ksql/internal/testtools" tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/kmodifiers"
) )
func TestAttrScan(t *testing.T) { func TestAttrScan(t *testing.T) {
@ -53,7 +54,7 @@ func TestAttrScan(t *testing.T) {
fakeAttr := FakeAttr{ fakeAttr := FakeAttr{
Foo: "notZeroValue", Foo: "notZeroValue",
} }
err := jsonModifier.Scan(ctx, OpInfo{}, &fakeAttr, test.dbInput) err := jsonModifier.Scan(ctx, kmodifiers.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()
@ -75,7 +76,7 @@ func TestAttrValue(t *testing.T) {
tests := []struct { tests := []struct {
desc string desc string
dbInput interface{} dbInput interface{}
opInfoInput OpInfo opInfoInput kmodifiers.OpInfo
attrValue interface{} attrValue interface{}
expectedOutput interface{} expectedOutput interface{}
@ -84,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: OpInfo{ opInfoInput: kmodifiers.OpInfo{
DriverName: "notSQLServer", DriverName: "notSQLServer",
}, },
attrValue: FakeAttr{ attrValue: FakeAttr{
@ -97,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: OpInfo{ opInfoInput: kmodifiers.OpInfo{
DriverName: "sqlserver", DriverName: "sqlserver",
}, },
attrValue: FakeAttr{ attrValue: FakeAttr{

View File

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

View File

@ -3,20 +3,22 @@ package modifiers
import ( import (
"context" "context"
"time" "time"
"github.com/vingarcia/ksql/kmodifiers"
) )
// This one is useful for updatedAt timestamps // This one is useful for updatedAt timestamps
var timeNowUTCModifier = AttrModifier{ var timeNowUTCModifier = kmodifiers.AttrModifier{
Value: func(ctx context.Context, opInfo OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { Value: func(ctx context.Context, opInfo kmodifiers.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 = AttrModifier{ var timeNowUTCSkipUpdatesModifier = kmodifiers.AttrModifier{
SkipOnUpdate: true, SkipOnUpdate: true,
Value: func(ctx context.Context, opInfo OpInfo, inputValue interface{}) (outputValue interface{}, _ error) { Value: func(ctx context.Context, opInfo kmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
return time.Now().UTC(), nil return time.Now().UTC(), nil
}, },
} }

View File

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

View File

@ -1,8 +1,6 @@
package modifiers package kmodifiers
import ( import "context"
"context"
)
// AttrModifier informs KSQL how to use this modifier // AttrModifier informs KSQL how to use this modifier
type AttrModifier struct { type AttrModifier struct {

View File

@ -0,0 +1,9 @@
package kmodifiers
// RegisterAttrModifier allow users to add custom modifiers on startup
// it is recommended to do this inside an init() function.
var RegisterAttrModifier func(key string, modifier AttrModifier)
// This method is set at startup by the `internal/modifiers` package.
// It was done that way in order to keep most of the implementation private
// while also avoiding cyclic dependencies.

View File

@ -12,6 +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/ksqltest" "github.com/vingarcia/ksql/ksqltest"
) )
@ -728,7 +729,7 @@ func buildInsertQuery(
Ctx: ctx, Ctx: ctx,
Attr: recordValue, Attr: recordValue,
ValueFn: valueFn, ValueFn: valueFn,
OpInfo: modifiers.OpInfo{ OpInfo: kmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
Method: "Insert", Method: "Insert",
}, },
@ -857,7 +858,7 @@ func buildUpdateQuery(
Ctx: ctx, Ctx: ctx,
Attr: recordValue, Attr: recordValue,
ValueFn: valueFn, ValueFn: valueFn,
OpInfo: modifiers.OpInfo{ OpInfo: kmodifiers.OpInfo{
DriverName: dialect.DriverName(), DriverName: dialect.DriverName(),
Method: "Update", Method: "Update",
}, },
@ -1063,7 +1064,7 @@ func getScanArgsForNestedStructs(
Ctx: ctx, Ctx: ctx,
AttrPtr: valueScanner, AttrPtr: valueScanner,
ScanFn: fieldInfo.Modifier.Scan, ScanFn: fieldInfo.Modifier.Scan,
OpInfo: modifiers.OpInfo{ OpInfo: kmodifiers.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
@ -1093,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: modifiers.OpInfo{ OpInfo: kmodifiers.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

@ -11,6 +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/nullable" "github.com/vingarcia/ksql/nullable"
) )
@ -3335,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: modifiers.OpInfo{ OpInfo: kmodifiers.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