Skip to content

Commit

Permalink
Merge pull request #872 from mesg-foundation/feature/api-require-cont…
Browse files Browse the repository at this point in the history
…ainer

api: make New to receive Container as a required param
  • Loading branch information
antho1404 authored Apr 12, 2019
2 parents 3888a85 + 8d05659 commit c60658d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 31 deletions.
27 changes: 5 additions & 22 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,12 @@ type API struct {
container container.Container
}

// Option is a configuration func for MESG.
type Option func(*API)

// New creates a new API with given options.
func New(db database.ServiceDB, execDB database.ExecutionDB, options ...Option) (*API, error) {
a := &API{db: db, execDB: execDB}
for _, option := range options {
option(a)
}
if a.container == nil {
var err error
a.container, err = container.New()
if err != nil {
return nil, err
}
}
return a, nil
}

// ContainerOption configures underlying container access API.
func ContainerOption(container container.Container) Option {
return func(a *API) {
a.container = container
func New(c container.Container, db database.ServiceDB, execDB database.ExecutionDB) *API {
return &API{
container: c,
db: db,
execDB: execDB,
}
}

Expand Down
3 changes: 1 addition & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func newTesting(t *testing.T) (*API, *apiTesting) {
execDB, err := database.NewExecutionDB(execdbname)
require.NoError(t, err)

a, err := New(db, execDB, ContainerOption(containerMock))
require.NoError(t, err)
a := New(containerMock, db, execDB)

return a, &apiTesting{
T: t,
Expand Down
5 changes: 1 addition & 4 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func initDependencies() (*dependencies, error) {
}

// init api.
api, err := api.New(serviceDB, executionDB, api.ContainerOption(c))
if err != nil {
return nil, err
}
api := api.New(c, serviceDB, executionDB)

return &dependencies{
config: config,
Expand Down
3 changes: 1 addition & 2 deletions interface/grpc/core/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func newServerWithContainer(t *testing.T, c container.Container) (*Server, func(
execDB, err := database.NewExecutionDB(execdbname)
require.NoError(t, err)

a, err := api.New(db, execDB, api.ContainerOption(c))
require.NoError(t, err)
a := api.New(c, db, execDB)

server := NewServer(a)

Expand Down
4 changes: 3 additions & 1 deletion interface/grpc/service/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/docker/docker/pkg/archive"
"github.com/mesg-foundation/core/api"
"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/database"
"github.com/stretchr/testify/require"
)
Expand All @@ -30,9 +31,10 @@ func newServer(t *testing.T) (*Server, func()) {
execDB, err := database.NewExecutionDB(execdbname)
require.NoError(t, err)

a, err := api.New(db, execDB)
c, err := container.New()
require.NoError(t, err)

a := api.New(c, db, execDB)
server := NewServer(a)

closer := func() {
Expand Down

0 comments on commit c60658d

Please sign in to comment.