From cd7cffdc24686df5509ee316dc3eff2cf712c0d7 Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Sun, 17 Dec 2023 18:54:15 +0100 Subject: [PATCH 1/8] initial draft for octobe v2 --- README.md | 211 +++++++---------- docker-compose.yaml | 23 ++ docker-compose.yml | 20 -- driver/postgres/postgres.go | 243 ++++++++++++++++++++ driver/postgres/postgres_test.go | 1 + errs.go | 38 --- errs_test.go | 17 -- example/database/products.go | 111 --------- example/integration/integration_test.go | 185 --------------- example/logic.go | 81 ------- example/logic_test.go | 125 ---------- example/postgres/postgres_test.go | 105 +++++++++ go.mod | 25 +- go.sum | 198 +++------------- octobe.go | 231 +++---------------- octobe_test.go | 292 ------------------------ options.go | 29 --- 17 files changed, 533 insertions(+), 1402 deletions(-) create mode 100644 docker-compose.yaml delete mode 100644 docker-compose.yml create mode 100644 driver/postgres/postgres.go create mode 100644 driver/postgres/postgres_test.go delete mode 100644 errs.go delete mode 100644 errs_test.go delete mode 100644 example/database/products.go delete mode 100644 example/integration/integration_test.go delete mode 100644 example/logic.go delete mode 100644 example/logic_test.go create mode 100644 example/postgres/postgres_test.go delete mode 100644 octobe_test.go delete mode 100644 options.go diff --git a/README.md b/README.md index d68ed29..0c060f0 100644 --- a/README.md +++ b/README.md @@ -2,159 +2,118 @@ ![License](https://img.shields.io/github/license/Kansuler/octobe) ![Tag](https://img.shields.io/github/v/tag/Kansuler/octobe) ![Version](https://img.shields.io/github/go-mod/go-version/Kansuler/octobe) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/492e6729782b471788994a72f2359f39)](https://www.codacy.com/gh/Kansuler/octobe/dashboard?utm_source=github.com&utm_medium=referral&utm_content=Kansuler/octobe&utm_campaign=Badge_Grade) [![Go Reference](https://pkg.go.dev/badge/github.com/Kansuler/octobe.svg)](https://pkg.go.dev/github.com/Kansuler/octobe) -A slim golang package for programmers that love to write raw SQL, but has a problem with boilerplate code. This package will help you structure and unify the way you work with your database. +A slim golang package for programmers that love to write raw SQL, but has a problem with boilerplate code. This package +will help you structure and the way you work with your database. -The main advantage with this library is to enable developers to build a predictable and consistent database layer without losing the feeling of freedom. The octobe library draws inspiration from http handlers, but where handlers interface with the database instead. +The main advantage with this library is to enable developers to build a predictable and consistent database layer +without losing the feeling of freedom. The octobe library draws inspiration from http handlers, but where handlers +interface with the database instead. Read package documentation at [https://pkg.go.dev/github.com/Kansuler/octobe](https://pkg.go.dev/github.com/Kansuler/octobe) ## Usage -### WatchTransaction - -This is a method that can watch the whole transaction, and where you don't have to define rollback or commit. - -WatchTransaction will rollback in case error is returned, otherwise it will proceed to commit. +### Postgres Example ```go -func Method(db *sql.DB, ctx context.Context) error { - ob := octobe.New(db) - - // Example of chaining multiple handlers in a transaction - return ob.WatchTransaction(ctx, func(scheme *octobe.Scheme) error { - p1 := Product{Name: "home made baguette"} - err := scheme.Handle(InsertProduct(&p1)) - if err != nil { - return err +package main + +import ( + "context" + "github.com/Kansuler/octobe/v2" + "github.com/Kansuler/octobe/v2/driver/postgres" + "github.com/google/uuid" + "os" +) + +func main() { + ctx := context.Background() + dsn := os.Getenv("DSN") + if dsn == "" { + panic("DSN is not set") } - // Execute other non-database logic that can return an error and rollback the transaction - err = anotherFunctionWithLogic() + // Create a new octobe instance with a postgres driver, insert optional options for configuration that applies to + // every session. + o, err := octobe.New(postgres.Open(ctx, dsn, postgres.WithTransaction(postgres.TxOptions{}))) if err != nil { - return err + panic(err) } - p2 := Product{Name: "another home made baguette"} - // For illustration, you can also suppress specific errors in the scheme handler - err = scheme.Handle(InsertProduct(&p2), octobe.SuppressError(sql.ErrNoRows), octobe.SuppressError(sql.ErrTxDone)) + // Begin a new session, since `postgres.WithTransaction` is set, this will start a postgres transaction. + session, err := o.Begin(context.Background()) if err != nil { - return err + panic(err) } - return nil - }) -} + // WatchRollback will rollback the transaction if var err is not nil when the function returns. + defer session.WatchRollback(func() error { + return err + }) -// InsertProduct will take a pointer of a product, and insert it -// This method could be in a separate package. -func InsertProduct(p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - INSERT INTO - products(name) - VALUES($1) - RETURNING id - `) - - seg.Arguments(p.Name) - - // QueryRow and scan of RETURNING from query. - return seg.QueryRow(&p.ID) - } -} -``` + name := uuid.New().String() -### Transaction + // Insert a new product into the database, and return a Product struct. + product1, err := octobe.Handle(session, AddProduct(name)) + if err != nil { + panic(err) + } -Run a query with transaction, and chain handlers. + // Select the product from the database by name, and return a Product struct. + product2, err := octobe.Handle(session, ProductByName(product1.Name)) + if err != nil { + panic(err) + } -```go -func Method(db *sql.DB, ctx context.Context) error { - ob := octobe.New(db) - scheme, err := ob.BeginTx(ctx) - if err != nil { - return err - } - - // WatchRollback returns error that is defined in the scope of this Method. - // if err is not nil, octobe will perform a rollback. - defer scheme.WatchRollback(func() error { - return err - }) - - p := Product{Name: "home made baguette"} - err = scheme.Handle(InsertProduct(&p)) - if err != nil { - return err - } - - // Finish with a commit - return scheme.Commit() + // Commit the transaction, if err is not nil, the transaction will be rolled back via WatchRollback. + err = session.Commit() + if err != nil { + panic(err) + } } -// InsertProduct will take a pointer of a product, and insert it -// This method could be in a separate package. -func InsertProduct(p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - INSERT INTO - products(name) - VALUES($1) - RETURNING id - `) - - seg.Arguments(p.Name) - - // QueryRow and scan of RETURNING from query. - return seg.QueryRow(&p.ID) - } +// Product is a model that represents a product in the database +type Product struct { + ID int + Name string } -``` - -### Basic Handler - -Run a simple query where you chain database handlers. - -```go -func Method(db *sql.DB, ctx context.Context) error { - // New creates an octobe context around an *sql.DB instance - // SuppressError is option and can be used to ignore specific errors, like sql.ErrNoRows" - ob := octobe.New(db, octobe.SuppressError(sql.ErrNoRows)) - - // Begin is used to start without a database transaction - scheme := ob.Begin(ctx) - - var p1 Product - // Handle a database query in another method, perfect for separating out queries to a database package - err := scheme.Handle(SelectNameHandler(1, &p1)) - if err != nil { - return err - } - - var p2 Product - err := scheme.Handle(SelectNameHandler(2, &p2)) - if err != nil { - return err - } - return +// AddProduct is an octobe handler that will insert a product into the database, and return a product model. +func AddProduct(name string) octobe.Handler[Product, postgres.Builder] { + return func(new postgres.Builder) (Product, error) { + var product Product + query := new(` + INSERT INTO + products (name) + VALUES ($1) + RETURNING id, name; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } } -// Handler func that implements the octobe.Handler -func SelectNameHandler(id string, p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - // A segment is a specific query, you can chain many queries in here, or split chained logic into multiple handler funcs if you'd like. - seg := scheme.Segment(` - SELECT name FROM products WHERE id = $1; - `) - - // Arguments takes any input to the query - seg.Arguments(id) - - // Segment has all the normal methods you expect such as QueryRow, Query and Exec. - return seg.QueryRow(&p.Name) - } +// ProductByName is an octobe handler that will select a product from the database by name, and return a product model. +func ProductByName(name string) octobe.Handler[Product, postgres.Builder] { + return func(new postgres.Builder) (Product, error) { + var product Product + query := new(` + SELECT + id, + name + FROM + products + WHERE + name = $1; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } } ``` diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..6fccdc7 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,23 @@ +version: '3.8' + +services: + postgres: + image: postgres:latest + environment: + POSTGRES_USER: user + POSTGRES_PASSWORD: password + POSTGRES_DB: testdb + ports: + - "5432:5432" + test_postgres: + image: golang:1.21 + environment: + DSN: postgresql://user:password@postgres:5432/testdb?sslmode=disable&connect_timeout=10 + depends_on: + - postgres + volumes: + - type: bind + source: ./ + target: /workspace + working_dir: /workspace + command: ["go", "test", "-count=1", "-v", "./example/postgres"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 54836b3..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,20 +0,0 @@ -version: "3.9" # optional since v1.27.0 -services: - integration: - image: golang:1.18 - command: bash -c "cd /workspace && go install gotest.tools/gotestsum@latest && gotestsum -- -coverprofile=coverage.txt -timeout=10s -v -count=1 -coverpkg=./... -covermode=atomic ./..." - volumes: - - type: bind - source: . - target: /workspace - environment: - - DATABASE_CONNECTION_STRING=postgresql://postgres:mysecretpassword@postgres/development?connect_timeout=10 - depends_on: - - postgres - links: - - postgres - postgres: - image: postgres - environment: - - POSTGRES_DB=development - - POSTGRES_PASSWORD=mysecretpassword diff --git a/driver/postgres/postgres.go b/driver/postgres/postgres.go new file mode 100644 index 0000000..5c9b774 --- /dev/null +++ b/driver/postgres/postgres.go @@ -0,0 +1,243 @@ +package postgres + +import ( + "context" + "errors" + "github.com/Kansuler/octobe/v2" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +// postgres holds the connection pool and default configuration for the postgres driver +type postgres struct { + pool *pgx.Conn + cfg config +} + +// config defined various configurations possible for the postgres driver +type config struct { + txOptions *TxOptions +} + +// TxOptions is a struct that holds the options for a transaction +type TxOptions pgx.TxOptions + +// WithTransaction enables the use of a transaction for the session, enforce the usage of commit and rollback. +func WithTransaction(options TxOptions) octobe.Option[config] { + return func(c *config) { + c.txOptions = &options + } +} + +// WithoutTransaction disables the use of a transaction for the session, this will not enforce the usage of commit and +// rollback. +func WithoutTransaction() octobe.Option[config] { + return func(c *config) { + c.txOptions = nil + } +} + +// Type check to make sure that the postgres driver implements the Octobe Driver interface +var _ octobe.Driver[postgres, config, Builder] = &postgres{} + +// Open is a function that can be used for opening a new database connection, it should always return a driver with set +// signature of types for the local driver. +func Open(ctx context.Context, dsn string, opts ...octobe.Option[config]) octobe.Open[postgres, config, Builder] { + return func() (octobe.Driver[postgres, config, Builder], error) { + pool, err := pgx.Connect(ctx, dsn) + if err != nil { + return nil, err + } + + var cfg config + for _, opt := range opts { + opt(&cfg) + } + + return &postgres{ + pool: pool, + cfg: cfg, + }, nil + } +} + +// OpenWithPool is a function that can be used for opening a new database connection, it should always return a driver +// with set signature of types for the local driver. This function is used when a connection pool is already available. +func OpenWithPool(pool *pgx.Conn, opts ...octobe.Option[config]) octobe.Open[postgres, config, Builder] { + return func() (octobe.Driver[postgres, config, Builder], error) { + if pool == nil { + return nil, errors.New("pool is nil") + } + + var cfg config + for _, opt := range opts { + opt(&cfg) + } + + return &postgres{ + pool: pool, + cfg: cfg, + }, nil + } +} + +// Begin will start a new session with the database, this will return a Session instance that can be used for handling +// queries. Options can be passed to the driver for specific configuration that overwrites the default configuration +// given at instantiation of the Octobe instance. If no options are passed, the default configuration will be used. +// If the default configuration is not set, the session will not be transactional. +func (d *postgres) Begin(ctx context.Context, opts ...octobe.Option[config]) (octobe.Session[Builder], error) { + cfg := d.cfg + for _, opt := range opts { + opt(&cfg) + } + + var tx pgx.Tx + var err error + if cfg.txOptions == nil { + tx, err = d.pool.Begin(ctx) + } else { + tx, err = d.pool.BeginTx(ctx, pgx.TxOptions{ + IsoLevel: cfg.txOptions.IsoLevel, + AccessMode: cfg.txOptions.AccessMode, + DeferrableMode: cfg.txOptions.DeferrableMode, + BeginQuery: cfg.txOptions.BeginQuery, + }) + } + + if err != nil { + return nil, err + } + + return &session{ + ctx: ctx, + cfg: cfg, + tx: tx, + }, nil +} + +// session is a struct that holds session context, a session should be considered a series of queries that are related +// to each other. A session can be transactional or non-transactional, if it is transactional, it will enforce the usage +// of commit and rollback. If it is non-transactional, it will not enforce the usage of commit and rollback. +// A session is not thread safe, it should only be used in one thread at a time. +type session struct { + ctx context.Context + cfg config + tx pgx.Tx + committed bool +} + +// Type check to make sure that the session implements the Octobe Session interface +var _ octobe.Session[Builder] = &session{} + +// Commit will commit a transaction, this will only work if the session is transactional. +func (s *session) Commit() error { + if s.cfg.txOptions == nil { + return errors.New("cannot commit without transaction") + } + defer func() { + s.committed = true + }() + return s.tx.Commit(s.ctx) +} + +// Rollback will rollback a transaction, this will only work if the session is transactional. +func (s *session) Rollback() error { + if s.cfg.txOptions == nil { + return errors.New("cannot rollback without transaction") + } + return s.tx.Rollback(s.ctx) +} + +// WatchRollback will watch for a rollback, if the session is not committed, it will rollback the transaction. +func (s *session) WatchRollback(cb func() error) { + if !s.committed { + _ = s.Rollback() + return + } + + if err := cb(); err != nil { + _ = s.Rollback() + } +} + +// Builder will return a new builder for building queries +func (s *session) Builder() Builder { + return s.New +} + +// Builder is a function that is used for building queries +type Builder func(query string) Segment + +// New will return a new segment for building queries +func (s *session) New(query string) Segment { + return Segment{ + query: query, + args: nil, + used: false, + tx: s.tx, + ctx: s.ctx, + } +} + +// Segment is a specific query that can be run only once it keeps a few fields for keeping track on the Segment +type Segment struct { + // query in SQL that is going to be executed + query string + // args include argument values + args []interface{} + // used specify if this Segment already has been executed + used bool + // tx is the database transaction, initiated by BeginTx + tx pgx.Tx + // ctx is a context that can be used to interrupt a query + ctx context.Context +} + +// use will set used to true after a Segment has been performed +func (s *Segment) use() { + s.used = true +} + +// Arguments receives unknown amount of arguments to use in the query +func (s *Segment) Arguments(args ...interface{}) *Segment { + s.args = args + return s +} + +// Exec will execute a query. Used for inserts or updates +func (s *Segment) Exec() (pgconn.CommandTag, error) { + if s.used { + return pgconn.CommandTag{}, octobe.ErrAlreadyUsed + } + defer s.use() + return s.tx.Exec(s.ctx, s.query, s.args...) +} + +// QueryRow will return one result and put them into destination pointers +func (s *Segment) QueryRow(dest ...interface{}) error { + if s.used { + return octobe.ErrAlreadyUsed + } + defer s.use() + return s.tx.QueryRow(s.ctx, s.query, s.args...).Scan(dest...) +} + +// Query will perform a normal query against database that returns rows +func (s *Segment) Query(cb func(pgx.Rows) error) error { + if s.used { + return octobe.ErrAlreadyUsed + } + defer s.use() + + rows, err := s.tx.Query(s.ctx, s.query, s.args...) + if err != nil { + return err + } + + defer rows.Close() + if err = cb(rows); err != nil { + return err + } + + return nil +} diff --git a/driver/postgres/postgres_test.go b/driver/postgres/postgres_test.go new file mode 100644 index 0000000..bf560be --- /dev/null +++ b/driver/postgres/postgres_test.go @@ -0,0 +1 @@ +package postgres diff --git a/errs.go b/errs.go deleted file mode 100644 index 0b294c7..0000000 --- a/errs.go +++ /dev/null @@ -1,38 +0,0 @@ -package octobe - -import ( - "errors" - "fmt" - "strings" -) - -type errs []error - -func (e errs) Error() string { - var asStr []string - for _, x := range e { - if x == nil { - continue - } - asStr = append(asStr, fmt.Sprintf("%s.", x.Error())) - } - return strings.Join(asStr, " ") -} - -func (e errs) Is(target error) bool { - for _, candidate := range e { - if errors.Is(candidate, target) { - return true - } - } - return false -} - -func (e errs) As(target interface{}) bool { - for _, candidate := range e { - if errors.As(candidate, target) { - return true - } - } - return false -} diff --git a/errs_test.go b/errs_test.go deleted file mode 100644 index 49494cc..0000000 --- a/errs_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package octobe - -import ( - "fmt" - "testing" - - "errors" - - "github.com/stretchr/testify/assert" -) - -func TestErrs(t *testing.T) { - e1 := errs{ErrNeedInput, ErrUsed, fmt.Errorf("test %w", errors.New("test fmt.Errorf")), nil} - assert.ErrorIs(t, e1, ErrNeedInput) - assert.ErrorIs(t, e1, ErrUsed) - assert.Equal(t, "insert method require at least one argument. this query has already executed. test test fmt.Errorf.", e1.Error()) -} diff --git a/example/database/products.go b/example/database/products.go deleted file mode 100644 index a068741..0000000 --- a/example/database/products.go +++ /dev/null @@ -1,111 +0,0 @@ -package database - -import ( - "database/sql" - - "github.com/Kansuler/octobe" -) - -// Database interface that implements the databases, with this you can mock Database. -type Database interface { - InsertProduct(*Product) octobe.Handler - UpdateProduct(*Product) octobe.Handler - ProductByID(*Product) octobe.Handler - Products(*[]Product) octobe.Handler -} - -// Handler implements the database interface -type Handler struct{} - -// Product is an example database model -type Product struct { - ID int64 `json:"id"` - Name string `json:"name"` -} - -// InsertProduct will take a pointer of a product, and insert it -func (Handler) InsertProduct(p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - INSERT INTO - products(name) - VALUES($1) - RETURNING id - `) - - seg.Arguments(p.Name) - - return seg.QueryRow(&p.ID) - } -} - -// UpdateProduct will take a pointer and update the fields -func (Handler) UpdateProduct(p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - UPDATE - products - SET - name = $2 - WHERE - id = $1 - `) - - seg.Arguments(p.ID, p.Name) - - _, err := seg.Exec() - return err - } -} - -// ProductByID will take an id, and a pointer to scan into -func (Handler) ProductByID(id int64, p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - SELECT - id, - name - FROM - products - WHERE - id = $1 - `) - - seg.Arguments(id) - - return seg.QueryRow(&p.ID, &p.Name) - } -} - -// Products will take a pointer to append Products into -func (Handler) Products(result *[]Product) octobe.Handler { - return func(scheme *octobe.Scheme) (err error) { - seg := scheme.Segment(` - SELECT - id, - name - FROM - products - `) - - err = seg.Query(func(rows *sql.Rows) error { - for rows.Next() { - var p Product - err = rows.Scan( - &p.ID, - &p.Name, - ) - - // Will stop function, and return err from seg.Query - if err != nil { - return err - } - - *result = append(*result, p) - } - return nil - }) - - return - } -} diff --git a/example/integration/integration_test.go b/example/integration/integration_test.go deleted file mode 100644 index 7f086d7..0000000 --- a/example/integration/integration_test.go +++ /dev/null @@ -1,185 +0,0 @@ -package integration - -import ( - "context" - "database/sql" - "github.com/Kansuler/octobe" - "github.com/Kansuler/octobe/example/database" - "github.com/stretchr/testify/suite" - "os" - "testing" - - // This import only relates to database connection - _ "github.com/jackc/pgx/v4/stdlib" -) - -// Open will start a database connection and ping to confirm that it works -func open(con string) (db *sql.DB, err error) { - // The `sql.Open` function opens a new `*sql.DB` instance. We specify the driver name - // and the URI for our database. Here, we're using a Postgres URI - db, err = sql.Open("pgx", con) - if err != nil { - return - } - - // To verify the connection to our database instance, we can call the `Ping` - // method. If no error is returned, we can assume a successful connection - err = db.Ping() - return -} - -type PostgresTestSuite struct { - suite.Suite - db *sql.DB -} - -func (suite *PostgresTestSuite) SetupSuite() { - var err error - suite.db, err = open(os.Getenv("DATABASE_CONNECTION_STRING")) - if err != nil { - suite.T().Fatalf("Failed to open database: %s", err) - } - - ob := octobe.New(suite.db) - scheme := ob.Begin(context.Background()) - seg := scheme.Segment(` - CREATE TABLE IF NOT EXISTS products ( - id serial PRIMARY KEY, - name varchar(255) NOT NULL - ); - `) - - _, err = seg.Exec() - if err != nil { - suite.T().Fatalf("Failed to create table: %s", err) - } -} - -func (suite *PostgresTestSuite) Test() { - suite.T().Run("TestBeginInsert", func(t *testing.T) { - h := database.Handler{} - ob := octobe.New(suite.db) - scheme := ob.Begin(context.Background()) - p1 := database.Product{ - Name: "Product 1", - } - err := scheme.Handle(h.InsertProduct(&p1)) - suite.NoError(err) - suite.NotEqual(0, p1.ID) - suite.Equal("Product 1", p1.Name) - - p2 := database.Product{ - Name: "Product 2", - } - err = scheme.Handle(h.InsertProduct(&p2)) - suite.NoError(err) - suite.NotEqual(0, p2.ID) - suite.Equal("Product 2", p2.Name) - }) - - suite.T().Run("TestBeginSelect", func(t *testing.T) { - h := database.Handler{} - ob := octobe.New(suite.db) - scheme := ob.Begin(context.Background()) - - p1 := database.Product{ - Name: "Product To Be Selected", - } - err := scheme.Handle(h.InsertProduct(&p1)) - suite.NoError(err) - suite.NotEqual(0, p1.ID) - suite.Equal("Product To Be Selected", p1.Name) - - var p2 database.Product - err = scheme.Handle(h.ProductByID(p1.ID, &p2)) - suite.NoError(err) - suite.Equal(p1.ID, p2.ID) - suite.Equal(p1.Name, p2.Name) - }) - - suite.T().Run("TestBeginUpdate", func(t *testing.T) { - h := database.Handler{} - ob := octobe.New(suite.db) - scheme := ob.Begin(context.Background()) - - p1 := database.Product{ - Name: "Product Before Update", - } - - err := scheme.Handle(h.InsertProduct(&p1)) - suite.NoError(err) - suite.NotEqual(0, p1.ID) - suite.Equal("Product Before Update", p1.Name) - - p1.Name = "Product After Update" - - err = scheme.Handle(h.UpdateProduct(&p1)) - suite.NoError(err) - suite.NotEqual(0, p1.ID) - suite.Equal("Product After Update", p1.Name) - - var p2 database.Product - err = scheme.Handle(h.ProductByID(p1.ID, &p2)) - suite.NoError(err) - suite.Equal(p1.ID, p2.ID) - suite.Equal(p1.Name, p2.Name) - }) - - suite.T().Run("TestBeginTxInsert", func(t *testing.T) { - h := database.Handler{} - ob := octobe.New(suite.db) - scheme, err := ob.BeginTx(context.Background()) - if err != nil { - suite.T().Fatalf("Failed to begin transaction: %s", err) - } - - p1 := database.Product{ - Name: "Product Before Update", - } - - err = scheme.Handle(h.InsertProduct(&p1)) - suite.NoError(err) - suite.NotEqual(0, p1.ID) - suite.Equal("Product Before Update", p1.Name) - - err = scheme.Commit() - if err != nil { - suite.T().Fatalf("Failed to commit transaction: %s", err) - } - - scheme = ob.Begin(context.Background()) - var p2 database.Product - err = scheme.Handle(h.ProductByID(p1.ID, &p2)) - suite.NoError(err) - suite.Equal(p1.ID, p2.ID) - suite.Equal(p1.Name, p2.Name) - }) - - suite.T().Run("TestWatchTransaction", func(t *testing.T) { - h := database.Handler{} - ob := octobe.New(suite.db) - err := ob.WatchTransaction(context.Background(), func(scheme *octobe.Scheme) error { - p1 := database.Product{ - Name: "Product Before Update", - } - - err := scheme.Handle(h.InsertProduct(&p1)) - suite.NoError(err) - suite.NotEqual(0, p1.ID) - suite.Equal("Product Before Update", p1.Name) - - var p2 database.Product - err = scheme.Handle(h.ProductByID(p1.ID, &p2)) - suite.NoError(err) - suite.Equal(p1.ID, p2.ID) - suite.Equal(p1.Name, p2.Name) - - return nil - }) - suite.NoError(err) - }) -} - -func TestPostgres(t *testing.T) { - suite.Run(t, new(PostgresTestSuite)) -} diff --git a/example/logic.go b/example/logic.go deleted file mode 100644 index 478bb47..0000000 --- a/example/logic.go +++ /dev/null @@ -1,81 +0,0 @@ -package example - -import ( - "context" - "database/sql" - - "github.com/Kansuler/octobe" - "github.com/Kansuler/octobe/example/database" -) - -// RunTx - Retrieve a octobe instance, begin a scheme with a -// database transaction. -func RunTx(ob *octobe.Octobe) error { - ctx := context.Background() - scheme, err := ob.BeginTx(ctx, nil) - if err != nil { - return err - } - - // If err isn't nil, perform rollback at end of function - defer scheme.WatchRollback(func() error { - return err - }) - - product := database.Product{ - Name: "Foo product", - } - - h := database.Handler{} - err = scheme.Handle(h.InsertProduct(&product)) - if err != nil { - return err - } - - return scheme.Commit() -} - -// Run - Retrieve a octobe instance, begin a scheme without a -// database transaction. -func Run(ob *octobe.Octobe) error { - ctx := context.Background() - scheme := ob.Begin(ctx) - - var p database.Product - h := database.Handler{} - err := scheme.Handle(h.ProductByID(1, &p)) - if err != nil { - return err - } - - return nil -} - -// RunSupress - Retrieve a octobe instance, begin a scheme without a -// database transaction, fail it and use the SuppressError option for sql.ErrNoRows -func RunSupress(ob *octobe.Octobe) error { - ctx := context.Background() - scheme := ob.Begin(ctx) - - var p database.Product - h := database.Handler{} - err := scheme.Handle(h.ProductByID(1, &p), octobe.SuppressError(sql.ErrNoRows)) - if err != nil { - return err - } - - return nil -} - -// RunWatchTransaction - Octobe handle most of the transaction logic -// against database. -func RunWatchTransaction(ob *octobe.Octobe) error { - ctx := context.Background() - - h := database.Handler{} - // WatchTransaction will start a transaction against database, if func returns - // error it will perform a rollback of transaction. If err is nil it will do commit - return ob.WatchTransaction(ctx, func(scheme *octobe.Scheme) error { - return scheme.Handle(h.InsertProduct(&database.Product{Name: "test"})) - }) -} diff --git a/example/logic_test.go b/example/logic_test.go deleted file mode 100644 index 4a074a2..0000000 --- a/example/logic_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package example_test - -import ( - "database/sql" - "errors" - "testing" - - "github.com/DATA-DOG/go-sqlmock" - "github.com/Kansuler/octobe" - "github.com/Kansuler/octobe/example" - "github.com/stretchr/testify/assert" -) - -func TestRun(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectQuery("SELECT id, name FROM products").WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow("123", "test product")) - - ob := octobe.New(db) - err = example.Run(&ob) - assert.NoError(t, err) -} - -func TestRunFail(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectQuery("SELECT id, name FROM products").WithArgs(1).WillReturnError(errors.New("an error occurred")) - - ob := octobe.New(db) - err = example.Run(&ob) - assert.Error(t, err) -} - -func TestRunFailSupress(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectQuery("SELECT id, name FROM products").WithArgs(1).WillReturnError(sql.ErrNoRows) - - ob := octobe.New(db) - err = example.RunSupress(&ob) - assert.NoError(t, err) -} - -func TestRunTx(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectBegin() - mock.ExpectQuery("INSERT INTO").WithArgs("Foo product").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) - mock.ExpectCommit() - - ob := octobe.New(db) - err = example.RunTx(&ob) - assert.NoError(t, err) -} - -func TestRunTxFail(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectBegin() - mock.ExpectQuery("INSERT INTO").WithArgs("Foo product").WillReturnError(sql.ErrTxDone) - mock.ExpectRollback() - - ob := octobe.New(db) - err = example.RunTx(&ob) - assert.Error(t, err) -} - -func TestRunWatchTransaction(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectBegin() - mock.ExpectQuery("INSERT INTO").WithArgs("test").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) - mock.ExpectCommit() - - ob := octobe.New(db) - err = example.RunWatchTransaction(&ob) - assert.NoError(t, err) -} - -func TestRunWatchTransactionFail(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectBegin() - mock.ExpectQuery("INSERT INTO").WithArgs("test").WillReturnError(sql.ErrConnDone) - mock.ExpectRollback() - - ob := octobe.New(db) - err = example.RunWatchTransaction(&ob) - assert.Error(t, err) -} diff --git a/example/postgres/postgres_test.go b/example/postgres/postgres_test.go new file mode 100644 index 0000000..2898b09 --- /dev/null +++ b/example/postgres/postgres_test.go @@ -0,0 +1,105 @@ +package test + +import ( + "context" + "github.com/Kansuler/octobe/v2" + "github.com/Kansuler/octobe/v2/driver/postgres" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestPostgres(t *testing.T) { + ctx := context.Background() + dsn := os.Getenv("DSN") + if dsn == "" { + panic("DSN is not set") + } + + ob, err := octobe.New(postgres.Open(ctx, dsn, postgres.WithTransaction(postgres.TxOptions{}))) + if !assert.NoError(t, err) { + t.FailNow() + } + + tx, err := ob.Begin(context.Background()) + if !assert.NoError(t, err) { + t.FailNow() + } + + defer tx.WatchRollback(func() error { + return err + }) + + _, err = octobe.Handle(tx, Migration()) + if !assert.NoError(t, err) { + t.FailNow() + } + + name := uuid.New().String() + product1, err := octobe.Handle(tx, AddProduct(name)) + if !assert.NoError(t, err) { + t.FailNow() + } + + assert.Equal(t, name, product1.Name) + assert.NotZero(t, product1.ID) + + product2, err := octobe.Handle(tx, ProductByName(name)) + if !assert.NoError(t, err) { + t.FailNow() + } + + assert.Equal(t, name, product2.Name) + assert.NotZero(t, product2.ID) + + err = tx.Commit() + if !assert.NoError(t, err) { + t.FailNow() + } +} + +func Migration() octobe.Handler[bool, postgres.Builder] { + return func(new postgres.Builder) (bool, error) { + query := new(` + CREATE TABLE IF NOT EXISTS products ( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL + ); + `) + + result, err := query.Exec() + return result.Insert(), err + } +} + +type Product struct { + ID int + Name string +} + +func AddProduct(name string) octobe.Handler[Product, postgres.Builder] { + return func(new postgres.Builder) (Product, error) { + var product Product + query := new(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } +} + +func ProductByName(name string) octobe.Handler[Product, postgres.Builder] { + return func(new postgres.Builder) (Product, error) { + var product Product + query := new(` + SELECT id, name FROM products WHERE name = $1; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } +} diff --git a/go.mod b/go.mod index 889bf86..c03a2be 100644 --- a/go.mod +++ b/go.mod @@ -1,24 +1,21 @@ -module github.com/Kansuler/octobe +module github.com/Kansuler/octobe/v2 -go 1.18 +go 1.21.5 require ( - github.com/DATA-DOG/go-sqlmock v1.5.0 - github.com/jackc/pgx/v4 v4.15.0 - github.com/stretchr/testify v1.7.1 + github.com/google/uuid v1.5.0 + github.com/jackc/pgx/v5 v5.5.1 + github.com/stretchr/testify v1.8.4 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/jackc/chunkreader/v2 v2.0.1 // indirect - github.com/jackc/pgconn v1.11.0 // indirect - github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgproto3/v2 v2.2.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect - github.com/jackc/pgtype v1.10.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect - golang.org/x/text v0.3.6 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + github.com/rogpeppe/go-internal v1.12.0 // indirect + golang.org/x/crypto v0.9.0 // indirect + golang.org/x/text v0.9.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index eb77cd6..2815540 100644 --- a/go.sum +++ b/go.sum @@ -1,183 +1,39 @@ -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= -github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= -github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= -github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= -github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= -github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= -github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= -github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= -github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= -github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= -github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= -github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= -github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= -github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= -github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= -github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= -github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= -github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= -github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= -github.com/jackc/pgconn v1.11.0 h1:HiHArx4yFbwl91X3qqIHtUFoiIfLNJXCQRsnzkiwwaQ= -github.com/jackc/pgconn v1.11.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= -github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= -github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= -github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= -github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= -github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc= -github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= -github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= -github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= -github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= -github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= -github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= -github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgproto3/v2 v2.2.0 h1:r7JypeP2D3onoQTCxWdTpCtJ4D+qpKr0TxvoyMhZ5ns= -github.com/jackc/pgproto3/v2 v2.2.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= -github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= -github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= -github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= -github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= -github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= -github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= -github.com/jackc/pgtype v1.10.0 h1:ILnBWrRMSXGczYvmkYD6PsYyVFUNLTnIUJHHDLmqk38= -github.com/jackc/pgtype v1.10.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= -github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= -github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= -github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= -github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= -github.com/jackc/pgx/v4 v4.15.0 h1:B7dTkXsdILD3MF987WGGCcg+tvLW6bZJdEcqVFeU//w= -github.com/jackc/pgx/v4 v4.15.0/go.mod h1:D/zyOyXiaM1TmVWnOM18p0xdDtdakRBa0RsVGI3U3bw= -github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= -github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI= +github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= -github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= -github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= -github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= -github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= -github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= -go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= -go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= -go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= -go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= -go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= -go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= -go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= -go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= -go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= -go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= -golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= -golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= +golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= -gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/octobe.go b/octobe.go index 6b5694b..418550d 100644 --- a/octobe.go +++ b/octobe.go @@ -2,218 +2,63 @@ package octobe import ( "context" - "database/sql" "errors" ) -type dblike interface { - ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) - QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row - QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) - BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) -} - -type transactionlike interface { - ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) - QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row - QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) -} - -// Octobe struct that holds the database session -type Octobe struct { - // DB is the database instance - DB dblike -} - -// Option interface that tells what type of option it is -type Option interface { - Type() string -} - -// option is an internal struct for storing various options -type option struct { - // suppressErrs will prevent these errors to surface, it could typically be sql.ErrNoRows that isn't a real error - suppressErrs []error -} - -// New initiates a DB instance and connection. -func New(db dblike) Octobe { - return Octobe{DB: db} -} - -// ErrUsed is an error that emits if used is true on Segment. -var ErrUsed = errors.New("this query has already executed") - -// ErrNeedInput is an error that require inputs for the inser method -var ErrNeedInput = errors.New("insert method require at least one argument") - -// Scheme holds context for the duration of the operation -type Scheme struct { - db transactionlike - // ctx is a context that can be used to interrupt a query - ctx context.Context -} +var ErrAlreadyUsed = errors.New("query already used") -// Handler is a signature that can be used for handling -// database segments in a separate function -type Handler func(scheme *Scheme) error - -// Handle is a method that handle a handler -func (scheme *Scheme) Handle(handler Handler, opts ...Option) error { - opt := convertOptions(opts...) - return suppressErrors(handler(scheme), opt.suppressErrs) -} - -// BeginTx initiates a transaction against database -func (ob Octobe) BeginTx(ctx context.Context, opts ...*sql.TxOptions) (scheme Scheme, err error) { - var opt *sql.TxOptions - if len(opts) > 0 { - opt = opts[0] - } - - scheme.db, err = ob.DB.BeginTx(ctx, opt) - scheme.ctx = ctx - return -} +// Option is a signature that can be used for passing options to a driver +type Option[CONFIG any] func(cfg *CONFIG) -// Begin initiates a query run, but not as a transaction -func (ob Octobe) Begin(ctx context.Context) (scheme Scheme) { - scheme.db = ob.DB - scheme.ctx = ctx - return +// Driver is a signature that holds the specific driver in the Octobe context. +type Driver[DRIVER any, CONFIG any, BUILDER any] interface { + Begin(ctx context.Context, opts ...Option[CONFIG]) (Session[BUILDER], error) } -// Segment is a specific query that can be run only once it keeps a few fields for keeping track on the segment -type Segment struct { - // query in SQL that is going to be executed - query string - // args include argument values - args []interface{} - // used specify if this segment already has been executed - used bool - // tx is the database transaction, initiated by BeginTx - db transactionlike - // ctx is a context that can be used to interrupt a query - ctx context.Context -} - -// use will set used to true after a segment has been performed -func (segment *Segment) use() { - segment.used = true -} - -// Segment created a new query segment -func (scheme *Scheme) Segment(query string) *Segment { - return &Segment{ - query: query, - args: nil, - db: scheme.db, - ctx: scheme.ctx, - } -} - -// Arguments receives unknown amount of arguments to use in the query -func (segment *Segment) Arguments(args ...interface{}) *Segment { - segment.args = args - return segment -} +// Open is a signature that can be used for opening a driver, it should always return a driver with set signature of +// types for the local driver. +type Open[DRIVER any, CONFIG any, BUILDER any] func() (Driver[DRIVER, CONFIG, BUILDER], error) -// Exec will execute a query. Used for inserts or updates -func (segment *Segment) Exec() (result sql.Result, err error) { - if segment.used { - return nil, ErrUsed - } - - defer segment.use() - - result, err = segment.db.ExecContext(segment.ctx, segment.query, segment.args...) - return result, err -} - -// QueryRow will return one result and put them into destination pointers -func (segment *Segment) QueryRow(dest ...interface{}) error { - if segment.used { - return ErrUsed - } - - defer segment.use() - - return segment.db.QueryRowContext(segment.ctx, segment.query, segment.args...).Scan(dest...) +// Octobe struct that holds the database session +type Octobe[DRIVER any, CONFIG any, BUILDER any] struct { + driver Driver[DRIVER, CONFIG, BUILDER] } -// Query will perform a normal query against database that returns rows -func (segment *Segment) Query(cb func(*sql.Rows) error) error { - if segment.used { - return ErrUsed - } - - defer segment.use() - - rows, err := segment.db.QueryContext(segment.ctx, segment.query, segment.args...) +// New creates a new Octobe instance. +func New[DRIVER any, CONFIG any, BUILDER any](init Open[DRIVER, CONFIG, BUILDER]) (*Octobe[DRIVER, CONFIG, BUILDER], error) { + driver, err := init() if err != nil { - return err + return nil, err } - if err := cb(rows); err != nil { - return errs{err, rows.Close()} - } - - return rows.Close() + return &Octobe[DRIVER, CONFIG, BUILDER]{ + driver: driver, + }, nil } -// Commit will commit a transaction -func (scheme *Scheme) Commit() error { - if asCommit, ok := scheme.db.(interface{ Commit() error }); ok { - return asCommit.Commit() - } - return nil +// Begin a new session of queries, this will return a Session instance that can be used for handling queries. Options can be +// passed to the driver for specific configuration that overwrites the default configuration given at instantiation of +// the Octobe instance. +func (ob *Octobe[DRIVER, CONFIG, BUILDER]) Begin(ctx context.Context, opts ...Option[CONFIG]) (Session[BUILDER], error) { + return ob.driver.Begin(ctx, opts...) } -// Rollback will rollback a transaction -func (scheme *Scheme) Rollback() error { - if asRollback, ok := scheme.db.(interface{ Rollback() error }); ok { - return asRollback.Rollback() - } - return nil +// Session is a signature that has a +type Session[BUILDER any] interface { + Commit() error + Rollback() error + WatchRollback(func() error) + Builder() BUILDER } -// WatchRollback will perform a rollback if an error is given -// This method can be used as a defer in the function that performs -// the database operations. -func (scheme *Scheme) WatchRollback(cb func() error) error { - if err := cb(); err != nil { - return errs{err, scheme.Rollback()} - } - return nil -} - -// WatchTransaction will perform the whole transaction, or do rollback if error occurred. -func (ob Octobe) WatchTransaction(ctx context.Context, cb func(scheme *Scheme) error, opts ...*sql.TxOptions) error { - var opt *sql.TxOptions - if len(opts) > 0 { - opt = opts[0] - } - scheme, err := ob.BeginTx(ctx, opt) - if err != nil { - return err - } - - err = cb(&scheme) - - if err != nil { - return errs{err, scheme.Rollback()} - } +// Handler is a signature that can be used for handling +// database segments in a separate function +type Handler[DRIVER any, BUILDER any] func(BUILDER) (DRIVER, error) - return scheme.Commit() +// Handle is a method that handle a query without any options +func Handle[DRIVER any, BUILDER any](s Session[BUILDER], handler Handler[DRIVER, BUILDER]) (DRIVER, error) { + return handler(s.Builder()) } -// suppressErrors is a helper function that suppress sql.ErrNoRows -func suppressErrors(err error, errs []error) error { - for _, suppressErr := range errs { - if err == suppressErr { - return nil - } - } - - return err -} +// Void is a type that can be used for returning nothing from a handler +type Void *struct{} diff --git a/octobe_test.go b/octobe_test.go deleted file mode 100644 index 616c330..0000000 --- a/octobe_test.go +++ /dev/null @@ -1,292 +0,0 @@ -package octobe_test - -import ( - "context" - "database/sql" - "errors" - "fmt" - "testing" - - "github.com/DATA-DOG/go-sqlmock" - "github.com/Kansuler/octobe" - "github.com/stretchr/testify/assert" -) - -type Product struct { - ID string `json:"id"` - Name string `json:"name"` -} - -func TestQuery(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectExec("UPDATE products").WithArgs(1).WillReturnResult(sqlmock.NewResult(1, 1)) - mock.ExpectQuery("SELECT id, name FROM products").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "mirror").AddRow(2, "headset")) - mock.ExpectQuery("SELECT id, name FROM products").WithArgs(3).WillReturnError(sql.ErrNoRows) - - ctx := context.Background() - ob := octobe.New(db) - scheme := ob.Begin(ctx) - seg := scheme.Segment(` - UPDATE - products - WHERE - id = $1 - `) - seg.Arguments(1) - - execResult, err := seg.Exec() - rows, err := execResult.RowsAffected() - id, err := execResult.LastInsertId() - assert.NoError(t, err, "rows affected should not return an error") - assert.Equal(t, rows, int64(1), "rows affected should be one") - assert.NoError(t, err, "last id should not return an error") - assert.Equal(t, id, int64(1), "id should should be one") - assert.NoError(t, err, "execution should not return error") - - // Segment should not be able to be executed twice - _, err = seg.Exec() - assert.Error(t, err, "executing the same seg twice should error") - - var result []Product - - seg = scheme.Segment(` - SELECT - id, - name - FROM - products - `) - err = seg.Query(func(rows *sql.Rows) error { - for rows.Next() { - var product Product - err = rows.Scan( - &product.ID, - &product.Name, - ) - - if err != nil { - return err - } - - result = append(result, product) - } - return nil - }) - - assert.NoError(t, err, "query should not return error") - - var product Product - seg = scheme.Segment(` - SELECT - id, - name - FROM - products - WHERE - id = $1 - `) - seg.Arguments(3) - err = seg.QueryRow( - &product.ID, - &product.Name, - ) - - assert.Equal(t, sql.ErrNoRows, err, "expected an sql.ErrNoRows") - - // we make sure that all expectations were met - if err := mock.ExpectationsWereMet(); err != nil { - t.Errorf("there were unfulfilled expectations: %s", err) - } -} - -func TestTransaction(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - - mock.ExpectBegin() - mock.ExpectQuery("UPDATE products").WithArgs(1, "bar").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) - mock.ExpectQuery("SELECT").WithArgs(1).WillReturnError(sql.ErrNoRows) - mock.ExpectCommit() - - ctx := context.Background() - ob := octobe.New(db) - scheme, err := ob.BeginTx(ctx, nil) - assert.NoError(t, err, "does not expect begin transaction go get error") - var id int - seg := scheme.Segment(` - UPDATE - products - SET - name = $2 - WHERE - id = $1 - RETURNING id - `) - seg.Arguments(1, "bar") - err = seg.QueryRow(&id) - assert.NoError(t, err, "should not return any error") - assert.Equal(t, 1, id, "id should be 1") - - seg = scheme.Segment(`SELECT * FROM products WHERE id = $1`) - seg.Arguments(1) - err = seg.QueryRow(&id) - assert.ErrorIs(t, err, sql.ErrNoRows, "sql.ErrNoRows should now occur") - - err = scheme.Commit() - assert.NoError(t, err, "commit shouldn't return any error") - - // we make sure that all expectations were met - if err := mock.ExpectationsWereMet(); err != nil { - t.Errorf("there were unfulfilled expectations: %s", err) - } -} - -func TestTransaction_WatchRollback(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - mock.ExpectBegin() - mock.ExpectQuery("UPDATE products").WithArgs(1, "bar").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) - mock.ExpectRollback() - - func() { - ctx := context.Background() - ob := octobe.New(db) - - scheme, err := ob.BeginTx(ctx, nil) - assert.NoError(t, err, "does not expect begin transaction go get error") - - defer scheme.WatchRollback(func() error { - return err - }) - - var id int - seg := scheme.Segment(` - UPDATE - products - SET - name = $2 - WHERE - id = $1 - RETURNING - id - `) - seg.Arguments(1, "bar") - err = seg.QueryRow(&id) - assert.NoError(t, err, "should not return any error") - assert.Equal(t, 1, id, "id should be 1") - - err = errors.New("some error occurred, return function before commit happens") - if err != nil { - return - } - - err = scheme.Commit() - assert.NoError(t, err, "commit shouldn't emit error") - }() - - // we make sure that all expectations were met - if err := mock.ExpectationsWereMet(); err != nil { - t.Errorf("there were unfulfilled expectations: %s", err) - } -} - -func TestTransaction_WithHandlers(t *testing.T) { - db, mock, err := sqlmock.New() - if err != nil { - t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) - } - - defer db.Close() - mock.ExpectBegin() - mock.ExpectQuery("SELECT id, name FROM products").WillReturnError(sql.ErrNoRows) - mock.ExpectQuery("SELECT id, name FROM products").WillReturnError(sql.ErrNoRows) - mock.ExpectQuery("INSERT INTO products").WithArgs("Testing").WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) - mock.ExpectQuery("SELECT id, name FROM products").WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow("1", "test1").AddRow("2", "test2")) - mock.ExpectCommit() - - handler := func(p *Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - INSERT INTO - products(name) - VALUES($1) - RETURNING id - `) - - seg.Arguments(p.Name) - - return seg.QueryRow(&p.ID) - } - } - - handler2 := func(result *[]Product) octobe.Handler { - return func(scheme *octobe.Scheme) error { - seg := scheme.Segment(` - SELECT - id, - name - FROM - products - `) - - return seg.Query(func(rows *sql.Rows) error { - for rows.Next() { - var p Product - err = rows.Scan( - &p.ID, - &p.Name, - ) - - // Will stop function, and return err from seg.Query - if err != nil { - return err - } - - *result = append(*result, p) - } - return nil - }) - } - } - - ctx := context.Background() - ob := octobe.New(db) - scheme, err := ob.BeginTx(ctx, nil) - assert.NoError(t, err) - var results []Product - - // Suppressing an error - err = scheme.Handle(handler2(&results), octobe.SuppressError(sql.ErrNoRows)) - assert.NoError(t, err, "handler should not return sql.ErrNoRows") - - // Do not suppress an error - err = scheme.Handle(handler2(&results)) - assert.Error(t, err, "handler should return sql.ErrNoRows") - - err = scheme.Handle(handler(&Product{Name: "Testing"}), octobe.SuppressError(sql.ErrNoRows)) - assert.NoError(t, err, "handler should not return error") - - err = scheme.Handle(handler2(&results)) - assert.NoError(t, err) - for index, result := range results { - assert.Equal(t, fmt.Sprintf("%d", index+1), result.ID) - assert.Equal(t, fmt.Sprintf("test%d", index+1), result.Name) - } - - err = scheme.Commit() - assert.NoError(t, err) -} diff --git a/options.go b/options.go deleted file mode 100644 index eb31bdb..0000000 --- a/options.go +++ /dev/null @@ -1,29 +0,0 @@ -package octobe - -// suppressError -type suppressError struct { - err error -} - -// Type follows the Option interface -func (s suppressError) Type() string { - return "suppressError" -} - -// SuppressError will tell the library to suppress the error if it occurs in this lib or sql libs -func SuppressError(err error) Option { - return suppressError{err: err} -} - -// convertOptions helper function that convert slice of Option into an option struct -func convertOptions(opts ...Option) (opt option) { - for _, option := range opts { - switch option.Type() { - case "suppressError": - err, _ := option.(suppressError) - opt.suppressErrs = append(opt.suppressErrs, err.err) - break - } - } - return -} From 9291ffd4727679886760007e6a4d80aba80b748a Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Sun, 17 Dec 2023 19:57:54 +0100 Subject: [PATCH 2/8] remove handler signature --- README.md | 53 +++++++++-------------- example/postgres/postgres_test.go | 70 ++++++++++++++----------------- octobe.go | 12 ------ 3 files changed, 52 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index 0c060f0..62cf157 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,13 @@ func main() { name := uuid.New().String() // Insert a new product into the database, and return a Product struct. - product1, err := octobe.Handle(session, AddProduct(name)) + product1, err := AddProduct(session.Builder(), name) if err != nil { panic(err) } // Select the product from the database by name, and return a Product struct. - product2, err := octobe.Handle(session, ProductByName(product1.Name)) + product2, err := ProductByName(session.Builder(), product1.Name) if err != nil { panic(err) } @@ -80,40 +80,27 @@ type Product struct { } // AddProduct is an octobe handler that will insert a product into the database, and return a product model. -func AddProduct(name string) octobe.Handler[Product, postgres.Builder] { - return func(new postgres.Builder) (Product, error) { - var product Product - query := new(` - INSERT INTO - products (name) - VALUES ($1) - RETURNING id, name; - `) - - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err - } +func AddProduct(new postgres.Builder, name string) (Product, error) { + var product Product + query := new(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err } // ProductByName is an octobe handler that will select a product from the database by name, and return a product model. -func ProductByName(name string) octobe.Handler[Product, postgres.Builder] { - return func(new postgres.Builder) (Product, error) { - var product Product - query := new(` - SELECT - id, - name - FROM - products - WHERE - name = $1; - `) - - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err - } +func ProductByName(new postgres.Builder, name string) (Product, error) { + var product Product + query := new(` + SELECT id, name FROM products WHERE name = $1; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err } ``` diff --git a/example/postgres/postgres_test.go b/example/postgres/postgres_test.go index 2898b09..0106fc2 100644 --- a/example/postgres/postgres_test.go +++ b/example/postgres/postgres_test.go @@ -22,22 +22,22 @@ func TestPostgres(t *testing.T) { t.FailNow() } - tx, err := ob.Begin(context.Background()) + session, err := ob.Begin(context.Background()) if !assert.NoError(t, err) { t.FailNow() } - defer tx.WatchRollback(func() error { + defer session.WatchRollback(func() error { return err }) - _, err = octobe.Handle(tx, Migration()) + _, err = Migration(session.Builder()) if !assert.NoError(t, err) { t.FailNow() } name := uuid.New().String() - product1, err := octobe.Handle(tx, AddProduct(name)) + product1, err := AddProduct(session.Builder(), name) if !assert.NoError(t, err) { t.FailNow() } @@ -45,7 +45,7 @@ func TestPostgres(t *testing.T) { assert.Equal(t, name, product1.Name) assert.NotZero(t, product1.ID) - product2, err := octobe.Handle(tx, ProductByName(name)) + product2, err := ProductByName(session.Builder(), name) if !assert.NoError(t, err) { t.FailNow() } @@ -53,24 +53,22 @@ func TestPostgres(t *testing.T) { assert.Equal(t, name, product2.Name) assert.NotZero(t, product2.ID) - err = tx.Commit() + err = session.Commit() if !assert.NoError(t, err) { t.FailNow() } } -func Migration() octobe.Handler[bool, postgres.Builder] { - return func(new postgres.Builder) (bool, error) { - query := new(` - CREATE TABLE IF NOT EXISTS products ( - id SERIAL PRIMARY KEY, - name TEXT NOT NULL - ); - `) - - result, err := query.Exec() - return result.Insert(), err - } +func Migration(new postgres.Builder) (bool, error) { + query := new(` + CREATE TABLE IF NOT EXISTS products ( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL + ); + `) + + result, err := query.Exec() + return result.Insert(), err } type Product struct { @@ -78,28 +76,24 @@ type Product struct { Name string } -func AddProduct(name string) octobe.Handler[Product, postgres.Builder] { - return func(new postgres.Builder) (Product, error) { - var product Product - query := new(` - INSERT INTO products (name) VALUES ($1) RETURNING id, name; - `) +func AddProduct(new postgres.Builder, name string) (Product, error) { + var product Product + query := new(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err - } + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err } -func ProductByName(name string) octobe.Handler[Product, postgres.Builder] { - return func(new postgres.Builder) (Product, error) { - var product Product - query := new(` - SELECT id, name FROM products WHERE name = $1; - `) +func ProductByName(new postgres.Builder, name string) (Product, error) { + var product Product + query := new(` + SELECT id, name FROM products WHERE name = $1; + `) - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err - } + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err } diff --git a/octobe.go b/octobe.go index 418550d..8910042 100644 --- a/octobe.go +++ b/octobe.go @@ -50,15 +50,3 @@ type Session[BUILDER any] interface { WatchRollback(func() error) Builder() BUILDER } - -// Handler is a signature that can be used for handling -// database segments in a separate function -type Handler[DRIVER any, BUILDER any] func(BUILDER) (DRIVER, error) - -// Handle is a method that handle a query without any options -func Handle[DRIVER any, BUILDER any](s Session[BUILDER], handler Handler[DRIVER, BUILDER]) (DRIVER, error) { - return handler(s.Builder()) -} - -// Void is a type that can be used for returning nothing from a handler -type Void *struct{} From 31e7a00c6f8333e58403e081a67e37c220915b9c Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Mon, 18 Dec 2023 13:34:23 +0100 Subject: [PATCH 3/8] reworked the handler signature to make it more inuitive to use --- README.md | 47 ++++--- driver/postgres/postgres.go | 27 ++-- example/postgres/postgres_test.go | 63 +++++----- go.mod | 29 ++++- go.sum | 196 ++++++++++++++++++++++++++++++ octobe.go | 13 ++ 6 files changed, 309 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 62cf157..9990baf 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,13 @@ func main() { name := uuid.New().String() // Insert a new product into the database, and return a Product struct. - product1, err := AddProduct(session.Builder(), name) + product1, err := octobe.Execute(session, AddProduct(name)) if err != nil { panic(err) } // Select the product from the database by name, and return a Product struct. - product2, err := ProductByName(session.Builder(), product1.Name) + product2, err := octobe.Execute(session, ProductByName(name)) if err != nil { panic(err) } @@ -80,27 +80,34 @@ type Product struct { } // AddProduct is an octobe handler that will insert a product into the database, and return a product model. -func AddProduct(new postgres.Builder, name string) (Product, error) { - var product Product - query := new(` - INSERT INTO products (name) VALUES ($1) RETURNING id, name; - `) - - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err +// In the octobe.Handler signature the first generic is the type of driver builder, and the second is the returned type. +func AddProduct(name string) octobe.Handler[postgres.Builder, Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } } + // ProductByName is an octobe handler that will select a product from the database by name, and return a product model. -func ProductByName(new postgres.Builder, name string) (Product, error) { - var product Product - query := new(` - SELECT id, name FROM products WHERE name = $1; - `) - - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err +// In the octobe.Handler signature the first generic is the type of driver builder, and the second is the returned type. +func ProductByName(name string) octobe.Handler[postgres.Builder, Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + SELECT id, name FROM products WHERE name = $1; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } } ``` diff --git a/driver/postgres/postgres.go b/driver/postgres/postgres.go index 5c9b774..a0184e0 100644 --- a/driver/postgres/postgres.go +++ b/driver/postgres/postgres.go @@ -160,22 +160,19 @@ func (s *session) WatchRollback(cb func() error) { } } -// Builder will return a new builder for building queries -func (s *session) Builder() Builder { - return s.New -} - -// Builder is a function that is used for building queries +// Builder is a function signature that is used for building queries with postgres type Builder func(query string) Segment -// New will return a new segment for building queries -func (s *session) New(query string) Segment { - return Segment{ - query: query, - args: nil, - used: false, - tx: s.tx, - ctx: s.ctx, +// Builder will return a new builder for building queries +func (s *session) Builder() Builder { + return func(query string) Segment { + return Segment{ + query: query, + args: nil, + used: false, + tx: s.tx, + ctx: s.ctx, + } } } @@ -184,7 +181,7 @@ type Segment struct { // query in SQL that is going to be executed query string // args include argument values - args []interface{} + args []any // used specify if this Segment already has been executed used bool // tx is the database transaction, initiated by BeginTx diff --git a/example/postgres/postgres_test.go b/example/postgres/postgres_test.go index 0106fc2..49b69d7 100644 --- a/example/postgres/postgres_test.go +++ b/example/postgres/postgres_test.go @@ -31,13 +31,13 @@ func TestPostgres(t *testing.T) { return err }) - _, err = Migration(session.Builder()) + _, err = octobe.Execute(session, Migration()) if !assert.NoError(t, err) { t.FailNow() } name := uuid.New().String() - product1, err := AddProduct(session.Builder(), name) + product1, err := octobe.Execute(session, AddProduct(name)) if !assert.NoError(t, err) { t.FailNow() } @@ -45,7 +45,7 @@ func TestPostgres(t *testing.T) { assert.Equal(t, name, product1.Name) assert.NotZero(t, product1.ID) - product2, err := ProductByName(session.Builder(), name) + product2, err := octobe.Execute(session, ProductByName(name)) if !assert.NoError(t, err) { t.FailNow() } @@ -59,16 +59,17 @@ func TestPostgres(t *testing.T) { } } -func Migration(new postgres.Builder) (bool, error) { - query := new(` - CREATE TABLE IF NOT EXISTS products ( - id SERIAL PRIMARY KEY, - name TEXT NOT NULL - ); - `) - - result, err := query.Exec() - return result.Insert(), err +func Migration() octobe.Handler[postgres.Builder, octobe.Void] { + return func(builder postgres.Builder) (octobe.Void, error) { + query := builder(` + CREATE TABLE IF NOT EXISTS products ( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL + ); + `) + _, err := query.Exec() + return nil, err + } } type Product struct { @@ -76,24 +77,28 @@ type Product struct { Name string } -func AddProduct(new postgres.Builder, name string) (Product, error) { - var product Product - query := new(` - INSERT INTO products (name) VALUES ($1) RETURNING id, name; - `) +func AddProduct(name string) octobe.Handler[postgres.Builder, Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } } -func ProductByName(new postgres.Builder, name string) (Product, error) { - var product Product - query := new(` - SELECT id, name FROM products WHERE name = $1; - `) +func ProductByName(name string) octobe.Handler[postgres.Builder, Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + SELECT id, name FROM products WHERE name = $1; + `) - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } } diff --git a/go.mod b/go.mod index c03a2be..79f4986 100644 --- a/go.mod +++ b/go.mod @@ -9,13 +9,38 @@ require ( ) require ( + cloud.google.com/go v0.110.2 // indirect + cloud.google.com/go/compute v1.19.3 // indirect + cloud.google.com/go/compute/metadata v0.2.3 // indirect + cloud.google.com/go/firestore v1.14.0 // indirect + cloud.google.com/go/longrunning v0.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/google/s2a-go v0.1.4 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect + github.com/googleapis/gax-go/v2 v2.12.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect - golang.org/x/crypto v0.9.0 // indirect - golang.org/x/text v0.9.0 // indirect + go.opencensus.io v0.24.0 // indirect + golang.org/x/crypto v0.14.0 // indirect + golang.org/x/net v0.17.0 // indirect + golang.org/x/oauth2 v0.8.0 // indirect + golang.org/x/sync v0.2.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect + golang.org/x/time v0.3.0 // indirect + golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect + google.golang.org/api v0.128.0 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect + google.golang.org/grpc v1.56.1 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2815540..7d7e9c4 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,77 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.110.2 h1:sdFPBr6xG9/wkBbfhmUz/JmZC7X6LavQgcrVINrKiVA= +cloud.google.com/go v0.110.2/go.mod h1:k04UEeEtb6ZBRTv3dZz4CeJC3jKGxyhl0sAiVVquxiw= +cloud.google.com/go/compute v1.19.3 h1:DcTwsFgGev/wV5+q8o2fzgcHOaac+DKGC91ZlvpsQds= +cloud.google.com/go/compute v1.19.3/go.mod h1:qxvISKp/gYnXkSAD1ppcSOveRAmzxicEv/JlizULFrI= +cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= +cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= +cloud.google.com/go/firestore v1.14.0 h1:8aLcKnMPoldYU3YHgu4t2exrKhLQkqaXAGqT0ljrFVw= +cloud.google.com/go/firestore v1.14.0/go.mod h1:96MVaHLsEhbvkBEdZgfN+AS/GIkco1LRpH9Xp9YZfzQ= +cloud.google.com/go/longrunning v0.5.0 h1:DK8BH0+hS+DIvc9a2TPnteUievsTCH4ORMAASSb7JcQ= +cloud.google.com/go/longrunning v0.5.0/go.mod h1:0JNuqRShmscVAhIACGtskSAWtqtOoPkwP0YF1oVEchc= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= +github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= +github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/s2a-go v0.1.4 h1:1kZ/sQM3srePvKs3tXAvQzo66XfcReoqFpIpIccE7Oc= +github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/googleapis/enterprise-certificate-proxy v0.2.4 h1:uGy6JWR/uMIILU8wbf+OkstIrNiMjGpEIyhx8f6W7s4= +github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k= +github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas= +github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -18,22 +86,150 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= +go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8= +golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= +google.golang.org/api v0.128.0 h1:RjPESny5CnQRn9V6siglged+DZCgfu9l6mO9dkX9VOg= +google.golang.org/api v0.128.0/go.mod h1:Y611qgqaE92On/7g65MQgxYul3c0rEB894kniWLY750= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc h1:8DyZCyvI8mE1IdLy/60bS+52xfymkE72wv1asokgtao= +google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:xZnkP7mREFX5MORlOPEzLMr+90PPZQ2QWzrVTWfAq64= +google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc h1:kVKPf/IiYSBWEWtkIn6wZXwWGCnLKcC8oWfZvXjsGnM= +google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ= +google.golang.org/grpc v1.56.1 h1:z0dNfjIl0VpaZ9iSVjA6daGatAYwPGstTjt5vkRMFkQ= +google.golang.org/grpc v1.56.1/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/octobe.go b/octobe.go index 8910042..cad8499 100644 --- a/octobe.go +++ b/octobe.go @@ -50,3 +50,16 @@ type Session[BUILDER any] interface { WatchRollback(func() error) Builder() BUILDER } + +// Handler is a signature type for a handler. The handler receives a builder of the specific driver and returns a result +// and an error. +type Handler[BUILDER any, RESULT any] func(BUILDER) (RESULT, error) + +// Execute is a function that can be used for executing a handler with a session builder. This function injects the +// builder of the driver into the handler. +func Execute[BUILDER any, RESULT any](session Session[BUILDER], f Handler[BUILDER, RESULT]) (RESULT, error) { + return f(session.Builder()) +} + +// Void is a type that can be used for returning nothing from a handler. +type Void *struct{} From 5a4840494eaf54d3fc3f1bf7d3296452da1141b0 Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Fri, 22 Dec 2023 13:34:41 +0100 Subject: [PATCH 4/8] changed the signatures for handlers and execute to be driver specific --- README.md | 8 ++++---- driver/postgres/postgres.go | 10 ++++++++++ example/postgres/postgres_test.go | 12 ++++++------ octobe.go | 17 +++++++---------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9990baf..e927f03 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,13 @@ func main() { name := uuid.New().String() // Insert a new product into the database, and return a Product struct. - product1, err := octobe.Execute(session, AddProduct(name)) + product1, err := postgres.Execute(session, AddProduct(name)) if err != nil { panic(err) } // Select the product from the database by name, and return a Product struct. - product2, err := octobe.Execute(session, ProductByName(name)) + product2, err := postgres.Execute(session, ProductByName(name)) if err != nil { panic(err) } @@ -81,7 +81,7 @@ type Product struct { // AddProduct is an octobe handler that will insert a product into the database, and return a product model. // In the octobe.Handler signature the first generic is the type of driver builder, and the second is the returned type. -func AddProduct(name string) octobe.Handler[postgres.Builder, Product] { +func AddProduct(name string) postgres.Handler[Product] { return func(builder postgres.Builder) (Product, error) { var product Product query := builder(` @@ -97,7 +97,7 @@ func AddProduct(name string) octobe.Handler[postgres.Builder, Product] { // ProductByName is an octobe handler that will select a product from the database by name, and return a product model. // In the octobe.Handler signature the first generic is the type of driver builder, and the second is the returned type. -func ProductByName(name string) octobe.Handler[postgres.Builder, Product] { +func ProductByName(name string) postgres.Handler[Product] { return func(builder postgres.Builder) (Product, error) { var product Product query := builder(` diff --git a/driver/postgres/postgres.go b/driver/postgres/postgres.go index a0184e0..98390f5 100644 --- a/driver/postgres/postgres.go +++ b/driver/postgres/postgres.go @@ -176,6 +176,16 @@ func (s *session) Builder() Builder { } } +// Handler is a signature type for a handler. The handler receives a builder of the specific driver and returns a result +// and an error. +type Handler[RESULT any] func(Builder) (RESULT, error) + +// Execute is a function that can be used for executing a handler with a session builder. This function injects the +// builder of the driver into the handler. +func Execute[RESULT any](session octobe.Session[Builder], f Handler[RESULT]) (RESULT, error) { + return f(session.Builder()) +} + // Segment is a specific query that can be run only once it keeps a few fields for keeping track on the Segment type Segment struct { // query in SQL that is going to be executed diff --git a/example/postgres/postgres_test.go b/example/postgres/postgres_test.go index 49b69d7..6c087d0 100644 --- a/example/postgres/postgres_test.go +++ b/example/postgres/postgres_test.go @@ -31,13 +31,13 @@ func TestPostgres(t *testing.T) { return err }) - _, err = octobe.Execute(session, Migration()) + _, err = postgres.Execute(session, Migration()) if !assert.NoError(t, err) { t.FailNow() } name := uuid.New().String() - product1, err := octobe.Execute(session, AddProduct(name)) + product1, err := postgres.Execute(session, AddProduct(name)) if !assert.NoError(t, err) { t.FailNow() } @@ -45,7 +45,7 @@ func TestPostgres(t *testing.T) { assert.Equal(t, name, product1.Name) assert.NotZero(t, product1.ID) - product2, err := octobe.Execute(session, ProductByName(name)) + product2, err := postgres.Execute(session, ProductByName(name)) if !assert.NoError(t, err) { t.FailNow() } @@ -59,7 +59,7 @@ func TestPostgres(t *testing.T) { } } -func Migration() octobe.Handler[postgres.Builder, octobe.Void] { +func Migration() postgres.Handler[octobe.Void] { return func(builder postgres.Builder) (octobe.Void, error) { query := builder(` CREATE TABLE IF NOT EXISTS products ( @@ -77,7 +77,7 @@ type Product struct { Name string } -func AddProduct(name string) octobe.Handler[postgres.Builder, Product] { +func AddProduct(name string) postgres.Handler[Product] { return func(builder postgres.Builder) (Product, error) { var product Product query := builder(` @@ -90,7 +90,7 @@ func AddProduct(name string) octobe.Handler[postgres.Builder, Product] { } } -func ProductByName(name string) octobe.Handler[postgres.Builder, Product] { +func ProductByName(name string) postgres.Handler[Product] { return func(builder postgres.Builder) (Product, error) { var product Product query := builder(` diff --git a/octobe.go b/octobe.go index cad8499..77d39a5 100644 --- a/octobe.go +++ b/octobe.go @@ -45,20 +45,17 @@ func (ob *Octobe[DRIVER, CONFIG, BUILDER]) Begin(ctx context.Context, opts ...Op // Session is a signature that has a type Session[BUILDER any] interface { + // Commit will commit the transaction. Commit() error + + // Rollback will rollback the transaction. Rollback() error - WatchRollback(func() error) - Builder() BUILDER -} -// Handler is a signature type for a handler. The handler receives a builder of the specific driver and returns a result -// and an error. -type Handler[BUILDER any, RESULT any] func(BUILDER) (RESULT, error) + // WatchRollback will watch for error within the function, if it's set WatchRollback will rollback the transaction. + WatchRollback(func() error) -// Execute is a function that can be used for executing a handler with a session builder. This function injects the -// builder of the driver into the handler. -func Execute[BUILDER any, RESULT any](session Session[BUILDER], f Handler[BUILDER, RESULT]) (RESULT, error) { - return f(session.Builder()) + // Builder returns a new builder from the driver that is used to build queries for that specific driver. + Builder() BUILDER } // Void is a type that can be used for returning nothing from a handler. From bf4e293f9b77899a48daf8b6b7f39a513c52510a Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Fri, 22 Dec 2023 14:51:35 +0100 Subject: [PATCH 5/8] update go version in github action --- .github/workflows/testing.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml index 38c38ce..1b50783 100644 --- a/.github/workflows/testing.yaml +++ b/.github/workflows/testing.yaml @@ -15,7 +15,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.18 + go-version: 1.21 - name: Vendor deps run: go mod vendor From 80258178b4297659df57aa249f1bfac1fd48fd9b Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Fri, 22 Dec 2023 14:52:50 +0100 Subject: [PATCH 6/8] added postgres tests into the postgres driver --- docker-compose.yaml | 4 +- driver/postgres/postgres.go | 2 + driver/postgres/postgres_test.go | 105 ++++++++++++++++++++++- go.mod | 49 ++++++----- go.sum | 139 +++++++++++++------------------ 5 files changed, 192 insertions(+), 107 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 6fccdc7..514adb3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,7 +9,7 @@ services: POSTGRES_DB: testdb ports: - "5432:5432" - test_postgres: + test: image: golang:1.21 environment: DSN: postgresql://user:password@postgres:5432/testdb?sslmode=disable&connect_timeout=10 @@ -20,4 +20,4 @@ services: source: ./ target: /workspace working_dir: /workspace - command: ["go", "test", "-count=1", "-v", "./example/postgres"] \ No newline at end of file + command: ["go", "test", "-count=1", "-v", "./driver/..."] \ No newline at end of file diff --git a/driver/postgres/postgres.go b/driver/postgres/postgres.go index 98390f5..919f9a3 100644 --- a/driver/postgres/postgres.go +++ b/driver/postgres/postgres.go @@ -8,6 +8,8 @@ import ( "github.com/jackc/pgx/v5/pgconn" ) +type Driver octobe.Driver[postgres, config, Builder] + // postgres holds the connection pool and default configuration for the postgres driver type postgres struct { pool *pgx.Conn diff --git a/driver/postgres/postgres_test.go b/driver/postgres/postgres_test.go index bf560be..f6bc7aa 100644 --- a/driver/postgres/postgres_test.go +++ b/driver/postgres/postgres_test.go @@ -1 +1,104 @@ -package postgres +package postgres_test + +import ( + "context" + "github.com/Kansuler/octobe/v2" + "github.com/Kansuler/octobe/v2/driver/postgres" + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestPostgres(t *testing.T) { + ctx := context.Background() + dsn := os.Getenv("DSN") + if dsn == "" { + panic("DSN is not set") + } + + ob, err := octobe.New(postgres.Open(ctx, dsn, postgres.WithTransaction(postgres.TxOptions{}))) + if !assert.NoError(t, err) { + t.FailNow() + } + + session, err := ob.Begin(context.Background()) + if !assert.NoError(t, err) { + t.FailNow() + } + + defer session.WatchRollback(func() error { + return err + }) + + _, err = postgres.Execute(session, Migration()) + if !assert.NoError(t, err) { + t.FailNow() + } + + name := uuid.New().String() + product1, err := postgres.Execute(session, AddProduct(name)) + if !assert.NoError(t, err) { + t.FailNow() + } + + assert.Equal(t, name, product1.Name) + assert.NotZero(t, product1.ID) + + product2, err := postgres.Execute(session, ProductByName(name)) + if !assert.NoError(t, err) { + t.FailNow() + } + + assert.Equal(t, name, product2.Name) + assert.NotZero(t, product2.ID) + + err = session.Commit() + if !assert.NoError(t, err) { + t.FailNow() + } +} + +func Migration() postgres.Handler[octobe.Void] { + return func(builder postgres.Builder) (octobe.Void, error) { + query := builder(` + CREATE TABLE IF NOT EXISTS products ( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL + ); + `) + _, err := query.Exec() + return nil, err + } +} + +type Product struct { + ID int + Name string +} + +func AddProduct(name string) postgres.Handler[Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } +} + +func ProductByName(name string) postgres.Handler[Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + SELECT id, name FROM products WHERE name = $1; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } +} diff --git a/go.mod b/go.mod index 79f4986..d025f13 100644 --- a/go.mod +++ b/go.mod @@ -3,44 +3,49 @@ module github.com/Kansuler/octobe/v2 go 1.21.5 require ( + cloud.google.com/go/firestore v1.14.0 github.com/google/uuid v1.5.0 + github.com/googleapis/gax-go/v2 v2.12.0 github.com/jackc/pgx/v5 v5.5.1 github.com/stretchr/testify v1.8.4 + google.golang.org/api v0.154.0 + google.golang.org/grpc v1.60.1 ) require ( - cloud.google.com/go v0.110.2 // indirect - cloud.google.com/go/compute v1.19.3 // indirect + cloud.google.com/go/compute v1.23.3 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect - cloud.google.com/go/firestore v1.14.0 // indirect - cloud.google.com/go/longrunning v0.5.0 // indirect + cloud.google.com/go/longrunning v0.5.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.3.0 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/google/go-cmp v0.5.9 // indirect - github.com/google/s2a-go v0.1.4 // indirect - github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect - github.com/googleapis/gax-go/v2 v2.12.0 // indirect + github.com/google/s2a-go v0.1.7 // indirect + github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/net v0.17.0 // indirect - golang.org/x/oauth2 v0.8.0 // indirect - golang.org/x/sync v0.2.0 // indirect - golang.org/x/sys v0.13.0 // indirect - golang.org/x/text v0.13.0 // indirect - golang.org/x/time v0.3.0 // indirect - golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect - google.golang.org/api v0.128.0 // indirect - google.golang.org/appengine v1.6.7 // indirect - google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect - google.golang.org/grpc v1.56.1 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect + go.opentelemetry.io/otel v1.21.0 // indirect + go.opentelemetry.io/otel/metric v1.21.0 // indirect + go.opentelemetry.io/otel/trace v1.21.0 // indirect + golang.org/x/crypto v0.16.0 // indirect + golang.org/x/net v0.19.0 // indirect + golang.org/x/oauth2 v0.15.0 // indirect + golang.org/x/sync v0.5.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/time v0.5.0 // indirect + google.golang.org/appengine v1.6.8 // indirect + google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 7d7e9c4..6295f95 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,18 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.110.2 h1:sdFPBr6xG9/wkBbfhmUz/JmZC7X6LavQgcrVINrKiVA= -cloud.google.com/go v0.110.2/go.mod h1:k04UEeEtb6ZBRTv3dZz4CeJC3jKGxyhl0sAiVVquxiw= -cloud.google.com/go/compute v1.19.3 h1:DcTwsFgGev/wV5+q8o2fzgcHOaac+DKGC91ZlvpsQds= -cloud.google.com/go/compute v1.19.3/go.mod h1:qxvISKp/gYnXkSAD1ppcSOveRAmzxicEv/JlizULFrI= +cloud.google.com/go/compute v1.23.3 h1:6sVlXXBmbd7jNX0Ipq0trII3e4n1/MsADLK6a+aiVlk= +cloud.google.com/go/compute v1.23.3/go.mod h1:VCgBUoMnIVIR0CscqQiPJLAG25E3ZRZMzcFZeQ+h8CI= cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA= cloud.google.com/go/firestore v1.14.0 h1:8aLcKnMPoldYU3YHgu4t2exrKhLQkqaXAGqT0ljrFVw= cloud.google.com/go/firestore v1.14.0/go.mod h1:96MVaHLsEhbvkBEdZgfN+AS/GIkco1LRpH9Xp9YZfzQ= -cloud.google.com/go/longrunning v0.5.0 h1:DK8BH0+hS+DIvc9a2TPnteUievsTCH4ORMAASSb7JcQ= -cloud.google.com/go/longrunning v0.5.0/go.mod h1:0JNuqRShmscVAhIACGtskSAWtqtOoPkwP0YF1oVEchc= +cloud.google.com/go/longrunning v0.5.4 h1:w8xEcbZodnA2BbW6sVirkkoC+1gP8wS57EUUgGS0GVg= +cloud.google.com/go/longrunning v0.5.4/go.mod h1:zqNVncI0BOP8ST6XQD1+VcvuShMmq7+xFSzOL++V0dI= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= -github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= -github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= -github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k= +github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -28,26 +20,29 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= -github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= -github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBFApVqftFV6k087DA= +github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= +github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= @@ -60,18 +55,17 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/s2a-go v0.1.4 h1:1kZ/sQM3srePvKs3tXAvQzo66XfcReoqFpIpIccE7Oc= -github.com/google/s2a-go v0.1.4/go.mod h1:Ej+mSEMGRnqRzjc7VtF+jdBwYG5fuJfiZ8ELkjEwM0A= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o= +github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/googleapis/enterprise-certificate-proxy v0.2.4 h1:uGy6JWR/uMIILU8wbf+OkstIrNiMjGpEIyhx8f6W7s4= -github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k= +github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs= +github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0= github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas= github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU= -github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -87,14 +81,12 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -104,15 +96,21 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= -go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 h1:SpGay3w+nEwMpfVnbqOLH5gY52/foP8RE8UzTZ1pdSE= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1/go.mod h1:4UoMYEZOC0yN/sPGH76KPkkU7zgiEWYWL9vwmbnTJPE= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo= +go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc= +go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo= +go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4= +go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM= +go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc= +go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220314234659-1baeb1ce4c0b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= -golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= +golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -120,58 +118,44 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= +golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8= -golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE= +golang.org/x/oauth2 v0.15.0 h1:s8pnnxNVzjWyrvYdFUQq5llS1PX2zhPXmccZv99h7uQ= +golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= -golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= -golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -181,35 +165,28 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= -google.golang.org/api v0.128.0 h1:RjPESny5CnQRn9V6siglged+DZCgfu9l6mO9dkX9VOg= -google.golang.org/api v0.128.0/go.mod h1:Y611qgqaE92On/7g65MQgxYul3c0rEB894kniWLY750= +google.golang.org/api v0.154.0 h1:X7QkVKZBskztmpPKWQXgjJRPA2dJYrL6r+sYPRLj050= +google.golang.org/api v0.154.0/go.mod h1:qhSMkM85hgqiokIYsrRyKxrjfBeIhgl4Z2JmeRkYylc= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= +google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= +google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc h1:8DyZCyvI8mE1IdLy/60bS+52xfymkE72wv1asokgtao= -google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:xZnkP7mREFX5MORlOPEzLMr+90PPZQ2QWzrVTWfAq64= -google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc h1:kVKPf/IiYSBWEWtkIn6wZXwWGCnLKcC8oWfZvXjsGnM= -google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= +google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f h1:Vn+VyHU5guc9KjB5KrjI2q0wCOWEOIh0OEsleqakHJg= +google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f/go.mod h1:nWSwAFPb+qfNJXsoeO3Io7zf4tMSfN8EA8RlDA04GhY= +google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f h1:2yNACc1O40tTnrsbk9Cv6oxiW8pxI/pXj0wRtdlYmgY= +google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f/go.mod h1:Uy9bTZJqmfrw2rIBxgGLnamc78euZULUBrLZ9XTITKI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ= -google.golang.org/grpc v1.56.1 h1:z0dNfjIl0VpaZ9iSVjA6daGatAYwPGstTjt5vkRMFkQ= -google.golang.org/grpc v1.56.1/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s= +google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU= +google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -226,8 +203,6 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 207428489d6cc9840c43ed0306bbd0dba8156294 Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Fri, 22 Dec 2023 14:59:25 +0100 Subject: [PATCH 7/8] added proper example --- example/go.mod | 8 +++ example/go.sum | 6 ++ example/main.go | 110 ++++++++++++++++++++++++++++++ example/postgres/postgres_test.go | 104 ---------------------------- example/query/postgres.go | 50 ++++++++++++++ 5 files changed, 174 insertions(+), 104 deletions(-) create mode 100644 example/go.mod create mode 100644 example/go.sum create mode 100644 example/main.go delete mode 100644 example/postgres/postgres_test.go create mode 100644 example/query/postgres.go diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..5e11e95 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,8 @@ +module github.com/Kansuler/octobe/example + +go 1.21.5 + +require ( + github.com/Kansuler/octobe/v2 v2.0.0-20231222135250-80258178b429 // indirect + github.com/go-chi/chi/v5 v5.0.11 // indirect +) diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 0000000..48f8579 --- /dev/null +++ b/example/go.sum @@ -0,0 +1,6 @@ +github.com/Kansuler/octobe/v2 v2.0.0-20231222123441-5a4840494eaf h1:CLY87hdUnWdSttmalq8rPmLx0V6p5ZVGsy/nczx70Hg= +github.com/Kansuler/octobe/v2 v2.0.0-20231222123441-5a4840494eaf/go.mod h1:eJtv4nmpMFBDun61Tmk/cYsCgCIqpRhbuCCzGL0P3Ig= +github.com/Kansuler/octobe/v2 v2.0.0-20231222135250-80258178b429 h1:Ib6vxuOKU98tHCBWN8GNp8+nFAkT9v61ogGsew3cu2Y= +github.com/Kansuler/octobe/v2 v2.0.0-20231222135250-80258178b429/go.mod h1:Jjj4AriFqMIDTlLZc/tSNu7mhXV5I0jEC/YT11WkWNc= +github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= +github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..18b78f4 --- /dev/null +++ b/example/main.go @@ -0,0 +1,110 @@ +package main + +import ( + "context" + "github.com/Kansuler/octobe/example/query" + "github.com/Kansuler/octobe/v2" + "github.com/Kansuler/octobe/v2/driver/postgres" + "github.com/go-chi/chi/v5" + "net/http" + "os" +) + +type Container struct { + Postgres postgres.Driver +} + +func main() { + ctx := context.Background() + cnt := Container{} + var err error + dsn := os.Getenv("DSN") + if dsn == "" { + panic("DSN is not set") + } + + cnt.Postgres, err = octobe.New(postgres.Open(ctx, dsn)) + if err != nil { + panic(err) + } + + session, err := cnt.Postgres.Begin(ctx) + if err != nil { + panic(err) + } + + _, err = postgres.Execute(session, query.Migration()) + if err != nil { + panic(err) + } + + err = router(cnt) + if err != nil { + panic(err) + } +} + +func router(cnt Container) error { + r := chi.NewRouter() + + // Postgres + r.Get("/postgres/product/{name}", func(w http.ResponseWriter, r *http.Request) { + name := chi.URLParam(r, "name") + session, err := cnt.Postgres.Begin(r.Context()) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + product, err := postgres.Execute(session, query.ProductByName(name)) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + _, err = w.Write([]byte(product.Name)) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + }) + r.Post("/postgres/product/{name}", func(w http.ResponseWriter, r *http.Request) { + name := chi.URLParam(r, "name") + session, err := cnt.Postgres.Begin(r.Context(), postgres.WithTransaction(postgres.TxOptions{})) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + defer session.WatchRollback(func() error { + return err + }) + + product, err := postgres.Execute(session, query.AddProduct(name)) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + err = session.Commit() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + _, err = w.Write([]byte(product.Name)) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + }) + + return http.ListenAndServe(":8080", r) +} diff --git a/example/postgres/postgres_test.go b/example/postgres/postgres_test.go deleted file mode 100644 index 6c087d0..0000000 --- a/example/postgres/postgres_test.go +++ /dev/null @@ -1,104 +0,0 @@ -package test - -import ( - "context" - "github.com/Kansuler/octobe/v2" - "github.com/Kansuler/octobe/v2/driver/postgres" - "github.com/google/uuid" - "github.com/stretchr/testify/assert" - "os" - "testing" -) - -func TestPostgres(t *testing.T) { - ctx := context.Background() - dsn := os.Getenv("DSN") - if dsn == "" { - panic("DSN is not set") - } - - ob, err := octobe.New(postgres.Open(ctx, dsn, postgres.WithTransaction(postgres.TxOptions{}))) - if !assert.NoError(t, err) { - t.FailNow() - } - - session, err := ob.Begin(context.Background()) - if !assert.NoError(t, err) { - t.FailNow() - } - - defer session.WatchRollback(func() error { - return err - }) - - _, err = postgres.Execute(session, Migration()) - if !assert.NoError(t, err) { - t.FailNow() - } - - name := uuid.New().String() - product1, err := postgres.Execute(session, AddProduct(name)) - if !assert.NoError(t, err) { - t.FailNow() - } - - assert.Equal(t, name, product1.Name) - assert.NotZero(t, product1.ID) - - product2, err := postgres.Execute(session, ProductByName(name)) - if !assert.NoError(t, err) { - t.FailNow() - } - - assert.Equal(t, name, product2.Name) - assert.NotZero(t, product2.ID) - - err = session.Commit() - if !assert.NoError(t, err) { - t.FailNow() - } -} - -func Migration() postgres.Handler[octobe.Void] { - return func(builder postgres.Builder) (octobe.Void, error) { - query := builder(` - CREATE TABLE IF NOT EXISTS products ( - id SERIAL PRIMARY KEY, - name TEXT NOT NULL - ); - `) - _, err := query.Exec() - return nil, err - } -} - -type Product struct { - ID int - Name string -} - -func AddProduct(name string) postgres.Handler[Product] { - return func(builder postgres.Builder) (Product, error) { - var product Product - query := builder(` - INSERT INTO products (name) VALUES ($1) RETURNING id, name; - `) - - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err - } -} - -func ProductByName(name string) postgres.Handler[Product] { - return func(builder postgres.Builder) (Product, error) { - var product Product - query := builder(` - SELECT id, name FROM products WHERE name = $1; - `) - - query.Arguments(name) - err := query.QueryRow(&product.ID, &product.Name) - return product, err - } -} diff --git a/example/query/postgres.go b/example/query/postgres.go new file mode 100644 index 0000000..5afe61b --- /dev/null +++ b/example/query/postgres.go @@ -0,0 +1,50 @@ +package query + +import ( + "github.com/Kansuler/octobe/v2" + "github.com/Kansuler/octobe/v2/driver/postgres" +) + +func Migration() postgres.Handler[octobe.Void] { + return func(builder postgres.Builder) (octobe.Void, error) { + query := builder(` + CREATE TABLE IF NOT EXISTS products ( + id SERIAL PRIMARY KEY, + name TEXT NOT NULL + ); + `) + _, err := query.Exec() + return nil, err + } +} + +type Product struct { + ID int + Name string +} + +func AddProduct(name string) postgres.Handler[Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + INSERT INTO products (name) VALUES ($1) RETURNING id, name; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } +} + +func ProductByName(name string) postgres.Handler[Product] { + return func(builder postgres.Builder) (Product, error) { + var product Product + query := builder(` + SELECT id, name FROM products WHERE name = $1; + `) + + query.Arguments(name) + err := query.QueryRow(&product.ID, &product.Name) + return product, err + } +} From 68240aaf52ac6efd638dc2ba2781ee64f976f65a Mon Sep 17 00:00:00 2001 From: Simon Wikstrand Date: Fri, 22 Dec 2023 15:09:52 +0100 Subject: [PATCH 8/8] improved testing command --- docker-compose.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 514adb3..c75d251 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -20,4 +20,8 @@ services: source: ./ target: /workspace working_dir: /workspace - command: ["go", "test", "-count=1", "-v", "./driver/..."] \ No newline at end of file + command: > + bash -c " + go install gotest.tools/gotestsum@latest && + gotestsum -- -coverprofile=coverage.txt -timeout=10s -v -count=1 -coverpkg=./... -covermode=atomic ./driver/... + " \ No newline at end of file