-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
424 additions
and
745 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package providers | ||
package xtemplate | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package xtemplate | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
func WithDB(name string, db *sql.DB, opt *sql.TxOptions) Option { | ||
return func(c *Config) error { | ||
if db == nil { | ||
return fmt.Errorf("cannot create database provider with nil sql.DB. name: %s", name) | ||
} | ||
c.Databases = append(c.Databases, DotDBConfig{Name: name, DB: db, TxOptions: opt}) | ||
return nil | ||
} | ||
} | ||
|
||
type DotDBConfig struct { | ||
*sql.DB `json:"-"` | ||
*sql.TxOptions `json:"-"` | ||
Name string `json:"name"` | ||
Driver string `json:"driver"` | ||
Connstr string `json:"connstr"` | ||
MaxOpenConns int `json:"max_open_conns"` | ||
} | ||
|
||
var _ CleanupDotProvider = &DotDBConfig{} | ||
|
||
func (d *DotDBConfig) FieldName() string { return d.Name } | ||
func (d *DotDBConfig) Init(ctx context.Context) error { | ||
if d.DB != nil { | ||
return nil | ||
} | ||
db, err := sql.Open(d.Driver, d.Connstr) | ||
if err != nil { | ||
return fmt.Errorf("failed to open database with driver name '%s': %w", d.Driver, err) | ||
} | ||
db.SetMaxOpenConns(d.MaxOpenConns) | ||
if err := db.Ping(); err != nil { | ||
return fmt.Errorf("failed to ping database on open: %w", err) | ||
} | ||
d.DB = db | ||
return nil | ||
} | ||
func (d *DotDBConfig) Value(r Request) (any, error) { | ||
return &DotDB{d.DB, GetLogger(r.R.Context()), r.R.Context(), d.TxOptions, nil}, nil | ||
} | ||
func (dp *DotDBConfig) Cleanup(v any, err error) error { | ||
d := v.(*DotDB) | ||
if err != nil { | ||
return errors.Join(err, d.rollback()) | ||
} else { | ||
return errors.Join(err, d.commit()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package xtemplate | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type DotFlags struct { | ||
m map[string]string | ||
} | ||
|
||
func (d DotFlags) Value(key string) string { | ||
return d.m[key] | ||
} | ||
|
||
func WithFlags(name string, flags map[string]string) Option { | ||
return func(c *Config) error { | ||
if flags == nil { | ||
return fmt.Errorf("cannot create DotKVProvider with null map with name %s", name) | ||
} | ||
c.Flags = append(c.Flags, DotFlagsConfig{name, flags}) | ||
return nil | ||
} | ||
} | ||
|
||
type DotFlagsConfig struct { | ||
Name string `json:"name"` | ||
Values map[string]string `json:"values"` | ||
} | ||
|
||
var _ DotConfig = &DotFlagsConfig{} | ||
|
||
func (d *DotFlagsConfig) FieldName() string { return d.Name } | ||
func (d *DotFlagsConfig) Init(_ context.Context) error { return nil } | ||
func (d *DotFlagsConfig) Value(_ Request) (any, error) { return DotFlags{d.Values}, nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.