-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge: added examples and added descriptions to README.md (#17)
- Loading branch information
Showing
6 changed files
with
486 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package sabi_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sttk-go/sabi" | ||
"reflect" | ||
) | ||
|
||
func unused(v interface{}) {} | ||
|
||
type FooConn struct { | ||
} | ||
|
||
func (conn *FooConn) Commit() sabi.Err { | ||
return sabi.Ok() | ||
} | ||
|
||
func (conn *FooConn) Rollback() { | ||
} | ||
|
||
func (conn *FooConn) Close() { | ||
} | ||
|
||
type FooConnCfg struct { | ||
} | ||
|
||
func (cfg FooConnCfg) CreateConn() (sabi.Conn, sabi.Err) { | ||
return &FooConn{}, sabi.Ok() | ||
} | ||
|
||
type BarConn struct { | ||
} | ||
|
||
func (conn *BarConn) Commit() sabi.Err { | ||
return sabi.Ok() | ||
} | ||
|
||
func (conn *BarConn) Rollback() { | ||
} | ||
|
||
func (conn *BarConn) Close() { | ||
} | ||
|
||
type BarConnCfg struct { | ||
} | ||
|
||
func (cfg BarConnCfg) CreateConn() (sabi.Conn, sabi.Err) { | ||
return &BarConn{}, sabi.Ok() | ||
} | ||
|
||
func ExampleAddGlobalConnCfg() { | ||
sabi.AddGlobalConnCfg("foo", FooConnCfg{}) | ||
sabi.AddGlobalConnCfg("bar", BarConnCfg{}) | ||
|
||
base := sabi.NewConnBase() | ||
|
||
type FooBarDax struct { | ||
sabi.Dax | ||
} | ||
|
||
dax := FooBarDax{Dax: base} | ||
|
||
conn, err := dax.GetConn("foo") | ||
fmt.Printf("conn = %v\n", reflect.TypeOf(conn)) | ||
fmt.Printf("err.IsOk() = %v\n", err.IsOk()) | ||
|
||
conn, err = dax.GetConn("bar") | ||
fmt.Printf("conn = %v\n", reflect.TypeOf(conn)) | ||
fmt.Printf("err.IsOk() = %v\n", err.IsOk()) | ||
|
||
// Output: | ||
// conn = *sabi_test.FooConn | ||
// err.IsOk() = true | ||
// conn = *sabi_test.BarConn | ||
// err.IsOk() = true | ||
|
||
sabi.Clear() | ||
} | ||
|
||
func ExampleSealGlobalConnCfgs() { | ||
sabi.AddGlobalConnCfg("foo", FooConnCfg{}) | ||
sabi.SealGlobalConnCfgs() | ||
sabi.AddGlobalConnCfg("bar", BarConnCfg{}) // Bad example | ||
|
||
base := sabi.NewConnBase() | ||
|
||
type FooBarDax struct { | ||
sabi.Dax | ||
} | ||
|
||
dax := FooBarDax{Dax: base} | ||
|
||
conn, err := dax.GetConn("foo") | ||
fmt.Printf("conn = %v\n", reflect.TypeOf(conn)) | ||
fmt.Printf("err.IsOk() = %v\n", err.IsOk()) | ||
|
||
conn, err = dax.GetConn("bar") | ||
fmt.Printf("conn = %v\n", reflect.TypeOf(conn)) | ||
fmt.Printf("err.IsOk() = %v\n", err.IsOk()) | ||
fmt.Printf("err.Error() = %v\n", err.Error()) | ||
|
||
// Output: | ||
// conn = *sabi_test.FooConn | ||
// err.IsOk() = true | ||
// conn = <nil> | ||
// err.IsOk() = false | ||
// err.Error() = {reason=ConnCfgIsNotFound, Name=bar} | ||
|
||
sabi.Clear() | ||
} | ||
|
||
func ExampleNewConnBase() { | ||
base := sabi.NewConnBase() | ||
|
||
// Output: | ||
unused(base) | ||
} | ||
|
||
func ExampleConnBase_AddLocalConnCfg() { | ||
base := sabi.NewConnBase() | ||
base.AddLocalConnCfg("foo", FooConnCfg{}) | ||
|
||
type FooBarDax struct { | ||
sabi.Dax | ||
} | ||
|
||
dax := FooBarDax{Dax: base} | ||
|
||
conn, err := dax.GetConn("foo") | ||
fmt.Printf("conn = %v\n", reflect.TypeOf(conn)) | ||
fmt.Printf("err.IsOk() = %v\n", err.IsOk()) | ||
|
||
// Output: | ||
// conn = *sabi_test.FooConn | ||
// err.IsOk() = true | ||
|
||
sabi.Clear() | ||
} |
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,33 @@ | ||
package sabi_test | ||
|
||
import ( | ||
"github.com/sttk-go/sabi" | ||
) | ||
|
||
func ExampleDax() { | ||
base := sabi.NewConnBase() | ||
|
||
type MyDax interface { | ||
GetData() string | ||
SetData(data string) | ||
} | ||
|
||
dax := struct { | ||
FooGetterDax | ||
BarSetterDax | ||
}{ | ||
FooGetterDax: FooGetterDax{Dax: base}, | ||
BarSetterDax: BarSetterDax{Dax: base}, | ||
} | ||
|
||
proc := sabi.NewProc[MyDax](base, dax) | ||
proc.RunTxn(func(dax MyDax) sabi.Err { | ||
data := dax.GetData() | ||
dax.SetData(data) | ||
return sabi.Ok() | ||
}) | ||
|
||
// Output: | ||
|
||
sabi.Clear() | ||
} |
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,61 @@ | ||
package sabi_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sttk-go/sabi" | ||
"time" | ||
) | ||
|
||
func ExampleAddAsyncErrHandler() { | ||
sabi.AddAsyncErrHandler(func(err sabi.Err, tm time.Time) { | ||
fmt.Println("Asynchronous error handling: " + err.Error()) | ||
}) | ||
sabi.SealErrCfgs() | ||
|
||
type FailToDoSomething struct{ Name string } | ||
|
||
sabi.ErrBy(FailToDoSomething{Name: "abc"}) | ||
|
||
// Output: | ||
// Asynchronous error handling: {reason=FailToDoSomething, Name=abc} | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
sabi.ClearErrHandlers() | ||
} | ||
|
||
func ExampleAddSyncErrHandler() { | ||
sabi.AddSyncErrHandler(func(err sabi.Err, tm time.Time) { | ||
fmt.Println("Synchronous error handling: " + err.Error()) | ||
}) | ||
sabi.SealErrCfgs() | ||
|
||
type FailToDoSomething struct{ Name string } | ||
|
||
sabi.ErrBy(FailToDoSomething{Name: "abc"}) | ||
|
||
// Output: | ||
// Synchronous error handling: {reason=FailToDoSomething, Name=abc} | ||
|
||
sabi.ClearErrHandlers() | ||
} | ||
|
||
func ExampleSealErrCfgs() { | ||
sabi.AddSyncErrHandler(func(err sabi.Err, tm time.Time) { | ||
fmt.Println("This handler is registered") | ||
}) | ||
|
||
sabi.SealErrCfgs() | ||
|
||
sabi.AddSyncErrHandler(func(err sabi.Err, tm time.Time) { // Bad example | ||
fmt.Println("This handler is not registered") | ||
}) | ||
|
||
type FailToDoSomething struct{ Name string } | ||
|
||
sabi.ErrBy(FailToDoSomething{Name: "abc"}) | ||
|
||
// Output: | ||
// This handler is registered | ||
|
||
sabi.ClearErrHandlers() | ||
} |
Oops, something went wrong.