-
Notifications
You must be signed in to change notification settings - Fork 14
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
1 parent
9d613ea
commit de66523
Showing
9 changed files
with
151 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
package application | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"strconv" | ||
) | ||
|
||
// TransactionLog logs information with a unique id and a message | ||
type TransactionLog interface { | ||
Log(id, message string) | ||
} | ||
|
||
// CreditCardProcessor allows to auth and eventually capture an amount | ||
// float64 is used as an example here | ||
type CreditCardProcessor interface { | ||
Auth(amount float64) error | ||
Capture(amount float64) error | ||
} | ||
|
||
// Service defines our example application service | ||
type Service struct { | ||
logger TransactionLog | ||
processor CreditCardProcessor | ||
} | ||
|
||
// Inject dependencies for our service | ||
func (s *Service) Inject(logger TransactionLog, processor CreditCardProcessor) *Service { | ||
s.logger = logger | ||
s.processor = processor | ||
return s | ||
} | ||
|
||
// MakeTransaction tries to authorize and capture an amount, and logs these steps. | ||
func (s *Service) MakeTransaction(amount float64, message string) error { | ||
id := strconv.Itoa(rand.Int()) | ||
|
||
s.logger.Log(id, fmt.Sprintf("Start transaction %q", message)) | ||
|
||
s.logger.Log(id, "Try to Auth") | ||
if err := s.processor.Auth(amount); err != nil { | ||
s.logger.Log(id, "Auth failed") | ||
return err | ||
} | ||
|
||
s.logger.Log(id, "Try to Capture") | ||
if err := s.processor.Capture(amount); err != nil { | ||
s.logger.Log(id, "Capture failed") | ||
return err | ||
} | ||
|
||
s.logger.Log(id, "Transaction successful") | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"flamingo.me/dingo" | ||
"flamingo.me/dingo/example/application" | ||
"flamingo.me/dingo/example/paypal" | ||
) | ||
|
||
type stdloggerTransactionLog struct { | ||
prefix string | ||
} | ||
|
||
var _ application.TransactionLog = new(stdloggerTransactionLog) | ||
|
||
// Log a message with the configure prefix | ||
func (s *stdloggerTransactionLog) Log(id, message string) { | ||
log.Println(s.prefix, id, message) | ||
} | ||
|
||
type defaultModule struct{} | ||
|
||
// Configure the dingo injector | ||
func (*defaultModule) Configure(injector *dingo.Injector) { | ||
injector.Bind(new(application.TransactionLog)).ToInstance(&stdloggerTransactionLog{ | ||
prefix: "example", | ||
}) | ||
} | ||
|
||
func main() { | ||
// create a new injector and load modules | ||
injector := dingo.NewInjector( | ||
new(paypal.Module), | ||
new(defaultModule), | ||
) | ||
|
||
// instantiate the application service | ||
service := injector.GetInstance(application.Service{}).(*application.Service) | ||
|
||
// make a transaction | ||
if err := service.MakeTransaction(99.95, "test transaction"); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,14 @@ | ||
package paypal | ||
|
||
import ( | ||
"flamingo.me/dingo" | ||
"flamingo.me/dingo/example/application" | ||
) | ||
|
||
// Module configures an application to use the paypalCCProcessor for CreditCardProcessing | ||
type Module struct{} | ||
|
||
// Configure dependency injection | ||
func (m *Module) Configure(injector *dingo.Injector) { | ||
injector.Bind(new(application.CreditCardProcessor)).To(new(paypalCCProcessor)) | ||
} |
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,21 @@ | ||
package paypal | ||
|
||
import ( | ||
"log" | ||
|
||
"flamingo.me/dingo/example/application" | ||
) | ||
|
||
type paypalCCProcessor struct{} | ||
|
||
var _ application.CreditCardProcessor = new(paypalCCProcessor) | ||
|
||
func (*paypalCCProcessor) Auth(amount float64) error { | ||
log.Printf("Paypal: Auth: %v", amount) | ||
return nil | ||
} | ||
|
||
func (*paypalCCProcessor) Capture(amount float64) error { | ||
log.Printf("Paypal: Capture: %v", amount) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module flamingo.me/dingo | ||
|
||
go 1.12 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.2.2 | ||
) |
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,6 @@ | ||
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= |