Skip to content

Commit

Permalink
add go.mod and example
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianccm committed Mar 13, 2019
1 parent 9d613ea commit de66523
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 67 deletions.
33 changes: 0 additions & 33 deletions Gopkg.lock

This file was deleted.

34 changes: 0 additions & 34 deletions Gopkg.toml

This file was deleted.

2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ or `nil` and cast it to a pointer to the interface we want to specify: `(*Someth
Dingo then knowns how to dereference it properly and derive the correct type `Something`.
This is not necessary for structs, where we can just use the null value via `Something{}`.

See the example folder for a complete example.

```go
package example

Expand Down
54 changes: 54 additions & 0 deletions example/application/service.go
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
}
45 changes: 45 additions & 0 deletions example/main.go
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)
}
}
14 changes: 14 additions & 0 deletions example/paypal/module.go
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))
}
21 changes: 21 additions & 0 deletions example/paypal/processor.go
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
}
9 changes: 9 additions & 0 deletions go.mod
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
)
6 changes: 6 additions & 0 deletions go.sum
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=

0 comments on commit de66523

Please sign in to comment.