Documentation ¶
Overview ¶
Package github.com/sttk/sabi is a small framework to separate logic parts and data access parts for Golang applications.
The concept of this framework is separation and reintegration of necessary and redundant parts based on the perspectives of the whole and the parts. The separation of logics and data accesses is the most prominent and fundamental part of this concept.
Index ¶
- func Close()
- func GetDaxConn[C DaxConn](dax Dax, name string) (C, errs.Err)
- func Para(runners ...func() errs.Err) errs.Err
- func Para_(runners ...func() errs.Err) func() errs.Err
- func Seq(runners ...func() errs.Err) errs.Err
- func Seq_(runners ...func() errs.Err) func() errs.Err
- func Setup() errs.Err
- func StartApp(app func() errs.Err) errs.Err
- func Txn[D any](base DaxBase, logics ...func(dax D) errs.Err) errs.Err
- func Txn_[D any](base DaxBase, logics ...func(dax D) errs.Err) func() errs.Err
- func Uses(name string, ds DaxSrc) errs.Err
- type AsyncGroup
- type CreatedDaxConnIsNil
- type Dax
- type DaxBase
- type DaxConn
- type DaxSrc
- type DaxSrcIsNotFound
- type FailToCastDaxBase
- type FailToCastDaxConn
- type FailToCommitDaxConn
- type FailToCreateDaxConn
- type FailToRunInParallel
- type FailToSetupGlobalDaxSrcs
- type FailToSetupLocalDaxSrc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶ added in v0.6.0
func Close()
Close is the function that closes and frees each resource of registered global DaxSrc(s). This function should always be called before an application ends.
Example ¶
package main import ( "os" "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type CliArgDaxSrc struct{} func (ds CliArgDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds CliArgDaxSrc) Close() {} func (ds CliArgDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewCliArgDaxSrc(osArgs []string) CliArgDaxSrc { return CliArgDaxSrc{} } func main() { sabi.Uses("cliargs", NewCliArgDaxSrc(os.Args)) err := sabi.Setup() if err.IsNotOk() { // ... } defer sabi.Close() // ... }
Output:
func GetDaxConn ¶
GetDaxConn is the function to cast type of DaxConn instance. If the cast failed, this function returns an errs.Err of the reason: FailToCastDaxConn with the DaxConn name and type names of source and destination.
func Para ¶
Para is the function which runs argument functions in parallel.
Example ¶
package main import ( "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) func NewMyDaxBase() sabi.DaxBase { return sabi.NewDaxBase() } func main() { type FooDax interface { sabi.Dax // ... } type BarDax interface { sabi.Dax // ... } base := NewMyDaxBase() txn1 := sabi.Txn_[FooDax](base, func(dax FooDax) errs.Err { // ... return errs.Ok() }) txn2 := sabi.Txn_[BarDax](base, func(dax BarDax) errs.Err { // ... return errs.Ok() }) err := sabi.Para(txn1, txn2) if err.IsNotOk() { // ... } }
Output:
func Para_ ¶ added in v0.6.0
Para_ is the function which creates a runner function which runs Para function.
func Seq ¶
Seq is the function which runs argument functions sequencially.
Example ¶
package main import ( "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) func NewMyDaxBase() sabi.DaxBase { return sabi.NewDaxBase() } func main() { type FooDax interface { sabi.Dax // ... } type BarDax interface { sabi.Dax // ... } base := NewMyDaxBase() txn1 := sabi.Txn_[FooDax](base, func(dax FooDax) errs.Err { // ... return errs.Ok() }) txn2 := sabi.Txn_[BarDax](base, func(dax BarDax) errs.Err { // ... return errs.Ok() }) err := sabi.Seq(txn1, txn2) if err.IsNotOk() { // ... } }
Output:
func Seq_ ¶ added in v0.6.0
Seq_ is the function which creates a runner function which runs Seq function.
func Setup ¶ added in v0.6.0
Setup is the function that make the globally registered DaxSrc usable.
This function forbids adding more global DaxSrc(s), and calls each Setup method of all registered DaxSrc(s). If one of DaxSrc(s) fails to execute synchronous Setup, this function stops other setting up and returns an errs.Err containing the error reason of that failure.
If one of DaxSrc(s) fails to execute asynchronous Setup, this function continue to other setting up and returns an errs.Err containing the error reason of that failure and other errors if any.
Example ¶
package main import ( "os" "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type CliArgDaxSrc struct{} func (ds CliArgDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds CliArgDaxSrc) Close() {} func (ds CliArgDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewCliArgDaxSrc(osArgs []string) CliArgDaxSrc { return CliArgDaxSrc{} } type DatabaseDaxSrc struct{} func (ds DatabaseDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds DatabaseDaxSrc) Close() {} func (ds DatabaseDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewDatabaseDaxSrc(driverName, dataSourceName string) DatabaseDaxSrc { return DatabaseDaxSrc{} } var ( driverName string dataSourceName string ) func main() { sabi.Uses("cliargs", NewCliArgDaxSrc(os.Args)) sabi.Uses("database", NewDatabaseDaxSrc(driverName, dataSourceName)) err := sabi.Setup() if err.IsNotOk() { // ... } defer sabi.Close() // ... }
Output:
func StartApp ¶ added in v0.6.0
StartApp is the function that calls Setup function, the argument function, and Close function in order. If Setup function or the argument function fails, this function stops calling other functions and return an errs.Err containing the error reaason..
This function is a macro-like function aimed at reducing the amount of coding.
Example ¶
package main import ( "os" "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type CliArgDaxSrc struct{} func (ds CliArgDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds CliArgDaxSrc) Close() {} func (ds CliArgDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewCliArgDaxSrc(osArgs []string) CliArgDaxSrc { return CliArgDaxSrc{} } type DatabaseDaxSrc struct{} func (ds DatabaseDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds DatabaseDaxSrc) Close() {} func (ds DatabaseDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewDatabaseDaxSrc(driverName, dataSourceName string) DatabaseDaxSrc { return DatabaseDaxSrc{} } var ( driverName string dataSourceName string ) func main() { sabi.Uses("cliargs", NewCliArgDaxSrc(os.Args)) sabi.Uses("database", NewDatabaseDaxSrc(driverName, dataSourceName)) app := func() errs.Err { // ... return errs.Ok() } err := sabi.StartApp(app) if err.IsNotOk() { // ... } }
Output:
func Txn ¶
Txn is the function that executes logic functions in a transaction.
First, this function casts the argument DaxBase to the type specified with the function's type parameter. Next, this function begins the transaction, and the argument logic functions are executed.. Then, if no error occurs, this function commits all updates in the transaction, otherwise rollbacks them. If there are commit errors after some DaxConn(s) are commited, or there are DaxConn(s) which don't have rollback mechanism, this function executes ForceBack methods of those DaxConn(s). And after that, this function ends the transaction.
During a transaction, it is denied to add or remove any local DaxSrc(s).
Example ¶
package main import ( "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type CliArgOptionDax struct { sabi.Dax } type DatabaseSetDax struct { sabi.Dax } type HttpReqParamDax struct { sabi.Dax } type HttpRespOutputDax struct { sabi.Dax } func NewMyBase() sabi.DaxBase { base := sabi.NewDaxBase() return &struct { sabi.DaxBase CliArgOptionDax DatabaseSetDax HttpReqParamDax HttpRespOutputDax }{ DaxBase: base, CliArgOptionDax: CliArgOptionDax{Dax: base}, DatabaseSetDax: DatabaseSetDax{Dax: base}, HttpReqParamDax: HttpReqParamDax{Dax: base}, HttpRespOutputDax: HttpRespOutputDax{Dax: base}, } } func main() { base := NewMyBase() defer base.Close() type GetSetDax struct { // ... } GetSetLogic := func(dax GetSetDax) errs.Err { // ... return errs.Ok() } err := sabi.Txn(base, GetSetLogic) if err.IsNotOk() { // ... } }
Output:
func Txn_ ¶ added in v0.6.0
Txn_ is the function that creates a runner function which runs a Txn function.
func Uses ¶ added in v0.6.0
Uses is the method that registers a global DaxSrc with its name to enable to use DaxConn created by the argument DaxSrc in all transactions.
If a DaxSrc is tried to register with a name already registered, it is ignored and a DaxSrc registered with the same name first is used. And this method ignore adding new DaxSrc(s) after Setup or first beginning of Proc or Txn.
Example ¶
package main import ( "os" "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type CliArgDaxSrc struct{} func (ds CliArgDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds CliArgDaxSrc) Close() {} func (ds CliArgDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewCliArgDaxSrc(osArgs []string) CliArgDaxSrc { return CliArgDaxSrc{} } type DatabaseDaxSrc struct{} func (ds DatabaseDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds DatabaseDaxSrc) Close() {} func (ds DatabaseDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewDatabaseDaxSrc(driverName, dataSourceName string) DatabaseDaxSrc { return DatabaseDaxSrc{} } var ( driverName string dataSourceName string ) func main() { sabi.Uses("cliargs", NewCliArgDaxSrc(os.Args)) sabi.Uses("database", NewDatabaseDaxSrc(driverName, dataSourceName)) err := sabi.StartApp(func() errs.Err { // ... return errs.Ok() }) if err.IsNotOk() { // ... } // ... }
Output:
Types ¶
type AsyncGroup ¶ added in v0.6.0
AsyncGroup is the interface to execute added functions asynchronously. The method: Add is to add target functions. This interface is used as an argument of DaxSrc#Setup, DaxConn#Commit, and DaxConn#Rollback.
Example ¶
package main import ( "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type SyncDaxSrc struct{} func (ds SyncDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { // ... return errs.Ok() } func (ds SyncDaxSrc) Close() {} func (ds SyncDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } type AsyncDaxSrc struct{} func (ds AsyncDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { ag.Add(func() errs.Err { // ... return errs.Ok() }) return errs.Ok() } func (ds AsyncDaxSrc) Close() {} func (ds AsyncDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func main() { sabi.Uses("sync", SyncDaxSrc{}) sabi.Uses("async", AsyncDaxSrc{}) err := sabi.Setup() if err.IsNotOk() { return } }
Output:
type CreatedDaxConnIsNil ¶ added in v0.6.0
type CreatedDaxConnIsNil struct {
Name string
}
CreatedDaxConnIsNil is the error reason which indicates that a DaxSrc created a DaxConn instance but it is nil. The field Name is the registered name of the DaxSrc that created a nil DaxConn.
type Dax ¶
type Dax interface {
// contains filtered or unexported methods
}
Dax is the interface for a set of data access methods.
This interface is embedded by Dax implementations for data stores, and each Dax implementation defines data access methods to each data store. In data access methods, DacConn instances connected to data stores can be got with GetConn function.
At the same time, this interface is embedded by Dax interfaces for logics. The each Dax interface declares only methods used by each logic.
type DaxBase ¶
type DaxBase interface { Dax Close() Uses(name string, ds DaxSrc) errs.Err Uses_(name string, ds DaxSrc) func() errs.Err Disuses(name string) Disuses_(name string) func() errs.Err // contains filtered or unexported methods }
DaxBase is the interface that declares the methods to manage DaxSrc(s). And this interface declarees unexported methods to process a transaction.
Close is the method to close and free all local DaxSrc(s). Uses is the method to register and setup a local DaxSrc with an argument name. Uses_ is the method that creates a runner function which runs #Uses method. Disuses is the method to close and remove a local DaxSrc specified by an argument name. Disuses_ is the method that creates a runner function which runs #Disuses method.
Example ¶
package main import ( "os" "github.com/sttk/sabi" "github.com/sttk/sabi/errs" ) type CliArgDaxSrc struct{} func (ds CliArgDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds CliArgDaxSrc) Close() {} func (ds CliArgDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewCliArgDaxSrc(osArgs []string) CliArgDaxSrc { return CliArgDaxSrc{} } type DatabaseDaxSrc struct{} func (ds DatabaseDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds DatabaseDaxSrc) Close() {} func (ds DatabaseDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewDatabaseDaxSrc(driverName, dataSourceName string) DatabaseDaxSrc { return DatabaseDaxSrc{} } var ( driverName string dataSourceName string ) type HttpRequestDaxSrc struct{} func (ds HttpRequestDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds HttpRequestDaxSrc) Close() {} func (ds HttpRequestDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewHttpRequestDaxSrc(req any) HttpRequestDaxSrc { return HttpRequestDaxSrc{} } var req any type HttpResponseDaxSrc struct{} func (ds HttpResponseDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err { return errs.Ok() } func (ds HttpResponseDaxSrc) Close() {} func (ds HttpResponseDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) { return nil, errs.Ok() } func NewHttpResponseDaxSrc(resp any) HttpResponseDaxSrc { return HttpResponseDaxSrc{} } var resp any type CliArgOptionDax struct { sabi.Dax } type DatabaseSetDax struct { sabi.Dax } type HttpReqParamDax struct { sabi.Dax } type HttpRespOutputDax struct { sabi.Dax } func main() { sabi.Uses("cliargs", NewCliArgDaxSrc(os.Args)) sabi.Uses("database", NewDatabaseDaxSrc(driverName, dataSourceName)) err := sabi.Setup() if err.IsNotOk() { // ... } defer sabi.Close() NewMyBase := func() sabi.DaxBase { base := sabi.NewDaxBase() return &struct { sabi.DaxBase CliArgOptionDax DatabaseSetDax HttpReqParamDax HttpRespOutputDax }{ DaxBase: base, CliArgOptionDax: CliArgOptionDax{Dax: base}, DatabaseSetDax: DatabaseSetDax{Dax: base}, HttpReqParamDax: HttpReqParamDax{Dax: base}, HttpRespOutputDax: HttpRespOutputDax{Dax: base}, } } type GetSetDax struct { // ... } GetSetLogic := func(dax GetSetDax) errs.Err { // ... return errs.Ok() } type OutputDax struct { // ... } OutputLogic := func(dax OutputDax) errs.Err { // ... return errs.Ok() } base := NewMyBase() defer base.Close() err = base.Uses("httpReq", NewHttpRequestDaxSrc(req)). IfOk(sabi.Txn_(base, GetSetLogic)). IfOk(base.Disuses_("httpReq")). IfOk(base.Uses_("httpResp", NewHttpResponseDaxSrc(resp))). IfOk(sabi.Txn_(base, OutputLogic)) if err.IsNotOk() { // ... } }
Output:
func NewDaxBase ¶
func NewDaxBase() DaxBase
NewDaxBase is the function that creates a new DaxBase instance.
Example ¶
package main import ( "github.com/sttk/sabi" ) type CliArgOptionDax struct { sabi.Dax } type DatabaseSetDax struct { sabi.Dax } type HttpReqParamDax struct { sabi.Dax } type HttpRespOutputDax struct { sabi.Dax } func main() { base0 := sabi.NewDaxBase() base := &struct { sabi.DaxBase CliArgOptionDax DatabaseSetDax HttpReqParamDax HttpRespOutputDax }{ DaxBase: base0, CliArgOptionDax: CliArgOptionDax{Dax: base0}, DatabaseSetDax: DatabaseSetDax{Dax: base0}, HttpReqParamDax: HttpReqParamDax{Dax: base0}, HttpRespOutputDax: HttpRespOutputDax{Dax: base0}, } _ = base }
Output:
type DaxConn ¶
type DaxConn interface { Commit(ag AsyncGroup) errs.Err IsCommitted() bool Rollback(ag AsyncGroup) ForceBack(ag AsyncGroup) Close() }
DaxConn is the interface that represents a connection to a data store, and defines methods: Commit, Rollback and Close to work in a transaction process.
Commit is the method for commiting updates in a transaction. IsCommitted is the method for check whether updates are already committed. Rollback is the method for rollback updates in a transaction. ForceBack is the method to revert updates forcely even if updates are already commited or this connection ooes not have rollback mechanism. If commting and rollbacking procedures are asynchronous, the argument AsyncGroup(s) are used to process them. Close is the method to close this connecton.
type DaxSrc ¶
DaxSrc is the interface that represents a data source which creates connections to a data store like database, etc. This interface declares three methods: Setup, Close, and CreateDaxConn.
Setup is the method to connect to a data store and to prepare to create DaxConn objects which represents a connection to access data in the data store. If the set up procedure is asynchronous, the Setup method is implemented so as to use AsyncGroup. Close is the method to disconnect to a data store. CreateDaxConn is the method to create a DaxConn object.
type DaxSrcIsNotFound ¶
type DaxSrcIsNotFound struct {
Name string
}
DaxSrcIsNotFound is the error reason which indicates that a specified DaxSrc is not found. The field Name is the registered name of the DaxSrc not found.
type FailToCastDaxBase ¶ added in v0.6.0
type FailToCastDaxBase struct {
FromType, ToType string
}
FailToCastDaxBase is the error reason which indicates that a DaxBase instance failed to cast to the destination type. The field FromType is the type name of the DaxBase instance and the field ToType is the type name of the destination type.
type FailToCastDaxConn ¶
type FailToCastDaxConn struct {
Name, FromType, ToType string
}
FailToCastDaxConn is the error reason which indicates that a DaxConn instance of the specified name failed to cast to the destination type. The field Name and FromType is the name and type name of the DaxConn instance, and the field ToType is the type name of the destination type.
type FailToCommitDaxConn ¶
FailToCommitDaxConn is the error reason which indicates that some connections failed to commit. The field Errors is the map of which keys are registered names of DaxConn which failed to commit, and of which values are errs.Err(s) having their error reasons.
type FailToCreateDaxConn ¶
type FailToCreateDaxConn struct {
Name string
}
FailToCreateDaxConn is the error reason which indicates that it is failed to create a new connection to a data store. The field Name is the registered name of the DaxSrc failed to create a DaxConn.
type FailToRunInParallel ¶
FailToRunInParallel is an error reason which indicates that some of runner functions running in parallel failed.
type FailToSetupGlobalDaxSrcs ¶ added in v0.6.0
FailToSetupGlobalDaxSrcs is the error reason which indicates that some DaxSrc(s) failed to set up. The field Errors is the map of which keys are the registered names of DaxSrc(s) that failed, and of which values are errs.Err having their error reasons.
type FailToSetupLocalDaxSrc ¶ added in v0.6.0
type FailToSetupLocalDaxSrc struct {
Name string
}
FailToSetupLocalDaxSrc is the error reason which indicates that a local DaxSrc failed to set up. The field Name is the registered name of the DaxSrc failed.