From 3eac7770805ac984980e85a28ce936140bf3df89 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Tue, 12 Jul 2022 17:56:06 +0200 Subject: [PATCH] EncodeFunc and DecodeFunc replaced by ScanFunc --- db_test.go | 6 +++--- default_decoders.go | 2 +- func_types.go | 3 +-- insert.go | 2 +- query_many.go | 4 ++-- query_one.go | 4 ++-- update.go | 2 +- 7 files changed, 11 insertions(+), 12 deletions(-) diff --git a/db_test.go b/db_test.go index 3372420..6e9b49a 100644 --- a/db_test.go +++ b/db_test.go @@ -15,15 +15,15 @@ type User struct { Salt string } -func UserDecoder(u *User, decode DecodeFunc) error { +func UserDecoder(u *User, decode ScanFunc) error { return decode(&u.Name, &u.Hash, &u.Salt) } -func InsertUserEncoder(u *User, encode EncodeFunc) error { +func InsertUserEncoder(u *User, encode ScanFunc) error { return encode(u.Name, u.Hash, u.Salt) } -func UpdateUserByNameEncoder(u *User, encode EncodeFunc) error { +func UpdateUserByNameEncoder(u *User, encode ScanFunc) error { return encode(u.Salt, u.Name) } diff --git a/default_decoders.go b/default_decoders.go index 29947e9..d570bcc 100644 --- a/default_decoders.go +++ b/default_decoders.go @@ -7,6 +7,6 @@ type defaultDecoderType interface { } // Decoder provides default decoders for primitive datatypes -func Decoder[T defaultDecoderType](value *T, decode DecodeFunc) error { +func Decoder[T defaultDecoderType](value *T, decode ScanFunc) error { return decode(value) } diff --git a/func_types.go b/func_types.go index 1543634..5f00a7d 100644 --- a/func_types.go +++ b/func_types.go @@ -11,5 +11,4 @@ type QueryOneContextFunc[T any] func(ctx context.Context, args ...interface{}) * type InsertFunc[T any] func(v *T) error type UpdateFunc[T any] func(v *T) error -type DecodeFunc = func(args ...interface{}) error -type EncodeFunc = func(args ...interface{}) error +type ScanFunc = func(args ...interface{}) error diff --git a/insert.go b/insert.go index 5bcf90e..87ed181 100644 --- a/insert.go +++ b/insert.go @@ -1,6 +1,6 @@ package advsql -func Insert[T any](db *Database, query string, encoder func(v *T, encode EncodeFunc) error) InsertFunc[T] { +func Insert[T any](db *Database, query string, encoder func(v *T, encode ScanFunc) error) InsertFunc[T] { s := db.prepare(query) return func(value *T) error { diff --git a/query_many.go b/query_many.go index 51ea7ac..80ceeae 100644 --- a/query_many.go +++ b/query_many.go @@ -2,14 +2,14 @@ package advsql import "context" -func QueryMany[T any](db *Database, query string, decoder func(v *T, decode DecodeFunc) error) QueryManyFunc[T] { +func QueryMany[T any](db *Database, query string, decoder func(v *T, decode ScanFunc) error) QueryManyFunc[T] { ctxfunc := QueryManyContext(db, query, decoder) return func(args ...interface{}) <-chan *T { return ctxfunc(context.Background(), args...) } } -func QueryManyContext[T any](db *Database, query string, decoder func(v *T, decode DecodeFunc) error) QueryManyContextFunc[T] { +func QueryManyContext[T any](db *Database, query string, decoder func(v *T, decode ScanFunc) error) QueryManyContextFunc[T] { s, err := db.db.Prepare(query) if err != nil { return nil diff --git a/query_one.go b/query_one.go index 59d6a11..1a5e932 100644 --- a/query_one.go +++ b/query_one.go @@ -2,14 +2,14 @@ package advsql import "context" -func QueryOne[T any](db *Database, query string, decoder func(v *T, decode DecodeFunc) error) QueryOneFunc[T] { +func QueryOne[T any](db *Database, query string, decoder func(v *T, decode ScanFunc) error) QueryOneFunc[T] { ctxfunc := QueryOneContext(db, query, decoder) return func(args ...interface{}) *T { return ctxfunc(context.Background(), args...) } } -func QueryOneContext[T any](db *Database, query string, decoder func(v *T, decode DecodeFunc) error) QueryOneContextFunc[T] { +func QueryOneContext[T any](db *Database, query string, decoder func(v *T, decode ScanFunc) error) QueryOneContextFunc[T] { manyfunc := QueryManyContext(db, query, decoder) return func(ctx context.Context, args ...interface{}) *T { nctx, cancel := context.WithCancel(ctx) diff --git a/update.go b/update.go index 0ca514c..32ebcac 100644 --- a/update.go +++ b/update.go @@ -1,5 +1,5 @@ package advsql -func Update[T any](db *Database, query string, encoder func(v *T, encode EncodeFunc) error) UpdateFunc[T] { +func Update[T any](db *Database, query string, encoder func(v *T, encode ScanFunc) error) UpdateFunc[T] { return UpdateFunc[T](Insert(db, query, encoder)) }