mirror of https://github.com/VinGarcia/ksql.git
parent
a7f12b34a5
commit
dd8a45c5d5
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
"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
|
||||
|
@ -17,8 +17,8 @@ type AttrScanWrapper struct {
|
|||
|
||||
AttrPtr interface{}
|
||||
|
||||
ScanFn kmodifiers.AttrScanner
|
||||
OpInfo kmodifiers.OpInfo
|
||||
ScanFn ksqlmodifiers.AttrScanner
|
||||
OpInfo ksqlmodifiers.OpInfo
|
||||
}
|
||||
|
||||
// Scan implements the sql.Scanner interface
|
||||
|
@ -36,8 +36,8 @@ type AttrValueWrapper struct {
|
|||
|
||||
Attr interface{}
|
||||
|
||||
ValueFn kmodifiers.AttrValuer
|
||||
OpInfo kmodifiers.OpInfo
|
||||
ValueFn ksqlmodifiers.AttrValuer
|
||||
OpInfo ksqlmodifiers.OpInfo
|
||||
}
|
||||
|
||||
// Value implements the sql.Valuer interface
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"testing"
|
||||
|
||||
tt "github.com/vingarcia/ksql/internal/testtools"
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
func TestAttrScanWrapper(t *testing.T) {
|
||||
|
@ -16,7 +16,7 @@ func TestAttrScanWrapper(t *testing.T) {
|
|||
wrapper := AttrScanWrapper{
|
||||
Ctx: ctx,
|
||||
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{}{
|
||||
"opInfo": opInfo,
|
||||
"attrPtr": attrPtr,
|
||||
|
@ -24,7 +24,7 @@ func TestAttrScanWrapper(t *testing.T) {
|
|||
}
|
||||
return errors.New("fakeScanErrMsg")
|
||||
},
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
Method: "fakeMethod",
|
||||
DriverName: "fakeDriverName",
|
||||
},
|
||||
|
@ -33,7 +33,7 @@ func TestAttrScanWrapper(t *testing.T) {
|
|||
err := wrapper.Scan("fakeDbValue")
|
||||
tt.AssertErrContains(t, err, "fakeScanErrMsg")
|
||||
tt.AssertEqual(t, scanArgs, map[string]interface{}{
|
||||
"opInfo": kmodifiers.OpInfo{
|
||||
"opInfo": ksqlmodifiers.OpInfo{
|
||||
Method: "fakeMethod",
|
||||
DriverName: "fakeDriverName",
|
||||
},
|
||||
|
@ -49,14 +49,14 @@ func TestAttrWrapper(t *testing.T) {
|
|||
wrapper := AttrValueWrapper{
|
||||
Ctx: ctx,
|
||||
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{}{
|
||||
"opInfo": opInfo,
|
||||
"inputValue": inputValue,
|
||||
}
|
||||
return "fakeOutputValue", errors.New("fakeValueErrMsg")
|
||||
},
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
Method: "fakeMethod",
|
||||
DriverName: "fakeDriverName",
|
||||
},
|
||||
|
@ -65,7 +65,7 @@ func TestAttrWrapper(t *testing.T) {
|
|||
value, err := wrapper.Value()
|
||||
tt.AssertErrContains(t, err, "fakeValueErrMsg")
|
||||
tt.AssertEqual(t, valueArgs, map[string]interface{}{
|
||||
"opInfo": kmodifiers.OpInfo{
|
||||
"opInfo": ksqlmodifiers.OpInfo{
|
||||
Method: "fakeMethod",
|
||||
DriverName: "fakeDriverName",
|
||||
},
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
// Here we keep all the registered modifiers
|
||||
|
@ -13,7 +13,7 @@ var modifiers sync.Map
|
|||
func init() {
|
||||
// Here we expose the registration function in a public package,
|
||||
// so users can use it:
|
||||
kmodifiers.RegisterAttrModifier = RegisterAttrModifier
|
||||
ksqlmodifiers.RegisterAttrModifier = RegisterAttrModifier
|
||||
|
||||
// These are the builtin modifiers:
|
||||
|
||||
|
@ -33,7 +33,7 @@ func init() {
|
|||
|
||||
// RegisterAttrModifier allow users to add custom modifiers on startup
|
||||
// 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)
|
||||
if found {
|
||||
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
|
||||
// modifiers during runtime.
|
||||
func LoadGlobalModifier(key string) (kmodifiers.AttrModifier, error) {
|
||||
func LoadGlobalModifier(key string) (ksqlmodifiers.AttrModifier, error) {
|
||||
rawModifier, _ := modifiers.Load(key)
|
||||
modifier, ok := rawModifier.(kmodifiers.AttrModifier)
|
||||
modifier, ok := rawModifier.(ksqlmodifiers.AttrModifier)
|
||||
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
|
||||
|
|
|
@ -4,15 +4,15 @@ import (
|
|||
"testing"
|
||||
|
||||
tt "github.com/vingarcia/ksql/internal/testtools"
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
func TestRegisterAttrModifier(t *testing.T) {
|
||||
t.Run("should register new modifiers correctly", func(t *testing.T) {
|
||||
modifier1 := kmodifiers.AttrModifier{
|
||||
modifier1 := ksqlmodifiers.AttrModifier{
|
||||
SkipOnUpdate: true,
|
||||
}
|
||||
modifier2 := kmodifiers.AttrModifier{
|
||||
modifier2 := ksqlmodifiers.AttrModifier{
|
||||
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) {
|
||||
modifier1 := kmodifiers.AttrModifier{
|
||||
modifier1 := ksqlmodifiers.AttrModifier{
|
||||
SkipOnUpdate: true,
|
||||
}
|
||||
modifier2 := kmodifiers.AttrModifier{
|
||||
modifier2 := ksqlmodifiers.AttrModifier{
|
||||
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) {
|
||||
mod, err := LoadGlobalModifier("nonExistentModifier")
|
||||
tt.AssertErrContains(t, err, "nonExistentModifier")
|
||||
tt.AssertEqual(t, mod, kmodifiers.AttrModifier{})
|
||||
tt.AssertEqual(t, mod, ksqlmodifiers.AttrModifier{})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -5,14 +5,14 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
// This modifier serializes objects as JSON when
|
||||
// sending it to the database and decodes
|
||||
// them when receiving.
|
||||
var jsonModifier = kmodifiers.AttrModifier{
|
||||
Scan: func(ctx context.Context, opInfo kmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error {
|
||||
var jsonModifier = ksqlmodifiers.AttrModifier{
|
||||
Scan: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, attrPtr interface{}, dbValue interface{}) error {
|
||||
if dbValue == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ var jsonModifier = kmodifiers.AttrModifier{
|
|||
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)
|
||||
// SQL server uses the NVARCHAR type to store JSON and
|
||||
// it expects to receive strings not []byte, thus:
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
tt "github.com/vingarcia/ksql/internal/testtools"
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
func TestAttrScan(t *testing.T) {
|
||||
|
@ -54,7 +54,7 @@ func TestAttrScan(t *testing.T) {
|
|||
fakeAttr := FakeAttr{
|
||||
Foo: "notZeroValue",
|
||||
}
|
||||
err := jsonModifier.Scan(ctx, kmodifiers.OpInfo{}, &fakeAttr, test.dbInput)
|
||||
err := jsonModifier.Scan(ctx, ksqlmodifiers.OpInfo{}, &fakeAttr, test.dbInput)
|
||||
if test.expectErrToContain != nil {
|
||||
tt.AssertErrContains(t, err, test.expectErrToContain...)
|
||||
t.Skip()
|
||||
|
@ -76,7 +76,7 @@ func TestAttrValue(t *testing.T) {
|
|||
tests := []struct {
|
||||
desc string
|
||||
dbInput interface{}
|
||||
opInfoInput kmodifiers.OpInfo
|
||||
opInfoInput ksqlmodifiers.OpInfo
|
||||
attrValue interface{}
|
||||
|
||||
expectedOutput interface{}
|
||||
|
@ -85,7 +85,7 @@ func TestAttrValue(t *testing.T) {
|
|||
{
|
||||
desc: "should return a byte array when the driver is not sqlserver",
|
||||
dbInput: []byte(`{"foo":"bar"}`),
|
||||
opInfoInput: kmodifiers.OpInfo{
|
||||
opInfoInput: ksqlmodifiers.OpInfo{
|
||||
DriverName: "notSQLServer",
|
||||
},
|
||||
attrValue: FakeAttr{
|
||||
|
@ -98,7 +98,7 @@ func TestAttrValue(t *testing.T) {
|
|||
{
|
||||
desc: "should return a string when the driver is sqlserver",
|
||||
dbInput: []byte(`{"foo":"bar"}`),
|
||||
opInfoInput: kmodifiers.OpInfo{
|
||||
opInfoInput: ksqlmodifiers.OpInfo{
|
||||
DriverName: "sqlserver",
|
||||
},
|
||||
attrValue: FakeAttr{
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package modifiers
|
||||
|
||||
import "github.com/vingarcia/ksql/kmodifiers"
|
||||
import "github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
|
||||
var skipInsertsModifier = kmodifiers.AttrModifier{
|
||||
var skipInsertsModifier = ksqlmodifiers.AttrModifier{
|
||||
SkipOnInsert: true,
|
||||
}
|
||||
|
||||
var skipUpdatesModifier = kmodifiers.AttrModifier{
|
||||
var skipUpdatesModifier = ksqlmodifiers.AttrModifier{
|
||||
SkipOnUpdate: true,
|
||||
}
|
||||
|
|
|
@ -4,21 +4,21 @@ import (
|
|||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
// This one is useful for updatedAt timestamps
|
||||
var timeNowUTCModifier = kmodifiers.AttrModifier{
|
||||
Value: func(ctx context.Context, opInfo kmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
|
||||
var timeNowUTCModifier = ksqlmodifiers.AttrModifier{
|
||||
Value: func(ctx context.Context, opInfo ksqlmodifiers.OpInfo, inputValue interface{}) (outputValue interface{}, _ error) {
|
||||
return time.Now().UTC(), nil
|
||||
},
|
||||
}
|
||||
|
||||
// This one is useful for createdAt timestamps
|
||||
var timeNowUTCSkipUpdatesModifier = kmodifiers.AttrModifier{
|
||||
var timeNowUTCSkipUpdatesModifier = ksqlmodifiers.AttrModifier{
|
||||
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
|
||||
},
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/vingarcia/ksql/internal/modifiers"
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
)
|
||||
|
||||
// StructInfo stores metainformation of the struct
|
||||
|
@ -26,7 +26,7 @@ type FieldInfo struct {
|
|||
Name string
|
||||
Index int
|
||||
Valid bool
|
||||
Modifier kmodifiers.AttrModifier
|
||||
Modifier ksqlmodifiers.AttrModifier
|
||||
}
|
||||
|
||||
// ByIndex returns either the *FieldInfo of a valid
|
||||
|
@ -252,7 +252,7 @@ func getTagNames(t reflect.Type) (_ StructInfo, err error) {
|
|||
}
|
||||
|
||||
tags := strings.Split(name, ",")
|
||||
var modifier kmodifiers.AttrModifier
|
||||
var modifier ksqlmodifiers.AttrModifier
|
||||
if len(tags) > 1 {
|
||||
name = tags[0]
|
||||
modifier, err = modifiers.LoadGlobalModifier(tags[1])
|
||||
|
|
10
ksql.go
10
ksql.go
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
"github.com/vingarcia/ksql/internal/modifiers"
|
||||
"github.com/vingarcia/ksql/internal/structs"
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqltest"
|
||||
)
|
||||
|
||||
|
@ -729,7 +729,7 @@ func buildInsertQuery(
|
|||
Ctx: ctx,
|
||||
Attr: recordValue,
|
||||
ValueFn: valueFn,
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
DriverName: dialect.DriverName(),
|
||||
Method: "Insert",
|
||||
},
|
||||
|
@ -858,7 +858,7 @@ func buildUpdateQuery(
|
|||
Ctx: ctx,
|
||||
Attr: recordValue,
|
||||
ValueFn: valueFn,
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
DriverName: dialect.DriverName(),
|
||||
Method: "Update",
|
||||
},
|
||||
|
@ -1064,7 +1064,7 @@ func getScanArgsForNestedStructs(
|
|||
Ctx: ctx,
|
||||
AttrPtr: valueScanner,
|
||||
ScanFn: fieldInfo.Modifier.Scan,
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
DriverName: dialect.DriverName(),
|
||||
// We will not differentiate between Query, QueryOne and QueryChunks
|
||||
// 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,
|
||||
AttrPtr: valueScanner,
|
||||
ScanFn: fieldInfo.Modifier.Scan,
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
DriverName: dialect.DriverName(),
|
||||
// We will not differentiate between Query, QueryOne and QueryChunks
|
||||
// if we did this could lead users to make very strange modifiers
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package kmodifiers
|
||||
package ksqlmodifiers
|
||||
|
||||
import "context"
|
||||
|
|
@ -3,4 +3,4 @@
|
|||
//
|
||||
// For understanding internal details of the code
|
||||
// please read the `internal/modifiers` package.
|
||||
package kmodifiers
|
||||
package ksqlmodifiers
|
|
@ -1,4 +1,4 @@
|
|||
package kmodifiers
|
||||
package ksqlmodifiers
|
||||
|
||||
// RegisterAttrModifier allow users to add custom modifiers on startup
|
||||
// it is recommended to do this inside an init() function.
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
"github.com/vingarcia/ksql/internal/modifiers"
|
||||
tt "github.com/vingarcia/ksql/internal/testtools"
|
||||
"github.com/vingarcia/ksql/kmodifiers"
|
||||
"github.com/vingarcia/ksql/ksqlmodifiers"
|
||||
"github.com/vingarcia/ksql/nullable"
|
||||
)
|
||||
|
||||
|
@ -3336,7 +3336,7 @@ func getUserByID(db DBAdapter, dialect Dialect, result *user, id uint) error {
|
|||
Ctx: context.TODO(),
|
||||
AttrPtr: &result.Address,
|
||||
ScanFn: modifier.Scan,
|
||||
OpInfo: kmodifiers.OpInfo{
|
||||
OpInfo: ksqlmodifiers.OpInfo{
|
||||
DriverName: dialect.DriverName(),
|
||||
// We will not differentiate between Query, QueryOne and QueryChunks
|
||||
// if we did this could lead users to make very strange modifiers
|
||||
|
|
Loading…
Reference in New Issue