Skip to content

Commit

Permalink
Contextが公開されていないためにパッケージ外からコンテキストを渡せなくなっていたバグを修正.また、init関数は複数書くことができる…
Browse files Browse the repository at this point in the history
…ようなので、登録漏れのないよう, 各 .goファイルに分割
  • Loading branch information
tukeJonny committed Aug 27, 2017
1 parent 6c7cef0 commit 590f928
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
13 changes: 4 additions & 9 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type apiClient interface {
GetAPIData() interface{}
}

type apiContext map[string]interface{}
type apiClientFactory func(ctx apiContext) apiClient
type APIContext map[string]interface{}
type apiClientFactory func(ctx APIContext) apiClient

var apiClientFactories = make(map[string]apiClientFactory)

Expand All @@ -27,12 +27,7 @@ func registerAPIClient(name string, factory apiClientFactory) {
apiClientFactories[name] = factory
}

// この関数はパッケージがimportされた時に呼び出されます
func init() {
registerAPIClient("events", newEventsAPIClient)
}

func newAPIClient(name string, ctx apiContext) apiClient {
func newAPIClient(name string, ctx APIContext) apiClient {
clientFactory, ok := apiClientFactories[name]
if !ok {
log.Panicf("Invalid API Client name!")
Expand All @@ -41,7 +36,7 @@ func newAPIClient(name string, ctx apiContext) apiClient {
return clientFactory(ctx)
}

func GetUrl(name string, ctx apiContext) string {
func GetUrl(name string, ctx APIContext) string {
client := newAPIClient(name, ctx)
return client.GetUrl()
}
Expand Down
8 changes: 6 additions & 2 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import (
)

type eventsAPIClient struct {
Ctx apiContext
Ctx APIContext
}

func newEventsAPIClient(ctx apiContext) apiClient {
func newEventsAPIClient(ctx APIContext) apiClient {
return &eventsAPIClient{
Ctx: ctx,
}
}

func init() {
registerAPIClient("events", newEventsAPIClient)
}

func (client *eventsAPIClient) GetUrl() string {
now := time.Now().Unix()

Expand Down

0 comments on commit 590f928

Please sign in to comment.