Skip to content

Commit

Permalink
Add an example usage. Close #9
Browse files Browse the repository at this point in the history
  • Loading branch information
magic003 committed Apr 17, 2017
1 parent c741b2c commit fcd0dcf
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 0 deletions.
23 changes: 23 additions & 0 deletions example/business/web_page_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package business

import (
"github.com/magic003/alice/example/client"
"github.com/magic003/alice/example/persist"
)

// WebPageManager manages web pages.
type WebPageManager struct {
httpClient client.HTTPClient
webPageDao persist.WebPageDao
}

// NewWebPageManager returns a new instance of WebPageManager.
func NewWebPageManager(httpClient client.HTTPClient, webPageDao persist.WebPageDao) *WebPageManager {
if httpClient == nil || webPageDao == nil {
panic("httpClient or webPageDao cannot be nil")
}
return &WebPageManager{
httpClient: httpClient,
webPageDao: webPageDao,
}
}
26 changes: 26 additions & 0 deletions example/client/http_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client

// HTTPClient is an interface for fetching web page.
type HTTPClient interface {
// Fetch fetches a web page by url.
Fetch(url string) string
}

// NewHTTPClient returns an instance of HTTPClient.
func NewHTTPClient(retries int) HTTPClient {
if retries <= 0 {
panic("retries should be greater than 0")
}
return &httpClient{
retries: retries,
}
}

// httpClient is a dummy implementation of HTTPClient.
type httpClient struct {
retries int
}

func (client *httpClient) Fetch(url string) string {
return ""
}
28 changes: 28 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package example

import (
"reflect"
"testing"

"github.com/magic003/alice"
"github.com/magic003/alice/example/business"
"github.com/magic003/alice/example/module"
)

func TestExample(t *testing.T) {
c := alice.CreateContainer(
&module.ConfigModule{}, &module.PersistModule{}, &module.ClientModule{}, &module.BusinessModule{})

retries := c.InstanceByName("Retries")
if retries != 3 {
t.Errorf("bad retries: got %v, expected %v", retries, 3)
}

table := c.InstanceByName("Table")
if table != "example_table" {
t.Errorf("bad table: got %s, expected %s", table, "example_table")
}

// should not panic
c.Instance(reflect.TypeOf((*business.WebPageManager)(nil)))
}
20 changes: 20 additions & 0 deletions example/module/business_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package module

import (
"github.com/magic003/alice"
"github.com/magic003/alice/example/business"
"github.com/magic003/alice/example/client"
"github.com/magic003/alice/example/persist"
)

// BusinessModule is the module for business objects.
type BusinessModule struct {
alice.BaseModule
WebPageDao persist.WebPageDao `alice:""`
HTTPClient client.HTTPClient `alice:"HTTPClient"`
}

// WebPageManager returns an instance of WebPageManager.
func (m *BusinessModule) WebPageManager() *business.WebPageManager {
return business.NewWebPageManager(m.HTTPClient, m.WebPageDao)
}
17 changes: 17 additions & 0 deletions example/module/client_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package module

import (
"github.com/magic003/alice"
"github.com/magic003/alice/example/client"
)

// ClientModule is the module for clients.
type ClientModule struct {
alice.BaseModule
Retries int `alice:"Retries"`
}

// HTTPClient returns an instance fo HTTPClient.
func (m *ClientModule) HTTPClient() client.HTTPClient {
return client.NewHTTPClient(m.Retries)
}
20 changes: 20 additions & 0 deletions example/module/config_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package module

import (
"github.com/magic003/alice"
)

// ConfigModule is the module for configurations.
type ConfigModule struct {
alice.BaseModule
}

// Retries returns the client retry attempts.
func (m *ConfigModule) Retries() int {
return 3
}

// Table is the table name.
func (m *ConfigModule) Table() string {
return "example_table"
}
17 changes: 17 additions & 0 deletions example/module/persist_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package module

import (
"github.com/magic003/alice"
"github.com/magic003/alice/example/persist"
)

// PersistModule is the module for persistent APIs.
type PersistModule struct {
alice.BaseModule
Table string `alice:"Table"`
}

// WebPageDao returns the WebPageDao.
func (m *PersistModule) WebPageDao() persist.WebPageDao {
return persist.NewWebPageDao(m.Table)
}
32 changes: 32 additions & 0 deletions example/persist/web_page_dao.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package persist

// WebPageDao defines the interface to access web pages saved in database.
type WebPageDao interface {
// Find looks up a web page by url.
Find(url string) string
// Save saves a web page.
Save(url string, content string) error
}

// NewWebPageDao returns an instance of WebPageDao.
func NewWebPageDao(table string) WebPageDao {
if table == "" {
panic("table cannot be empty")
}
return &webPageDao{
table: table,
}
}

// webPageDao is a dummy implementation for WebPageDao.
type webPageDao struct {
table string
}

func (dao *webPageDao) Find(url string) string {
return ""
}

func (dao *webPageDao) Save(url string, content string) error {
return nil
}

0 comments on commit fcd0dcf

Please sign in to comment.