Skip to content

Commit

Permalink
feat(config): add default transport option
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Dec 13, 2020
1 parent 3c6649e commit d46f68d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
10 changes: 9 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
type Config struct {
transports map[string]Transport
transportFactories map[string]TransportFactory
defaultTransport string
opts []postdog.Option
}

Expand All @@ -44,6 +45,7 @@ type TransportFactory interface {
type TransportFactoryFunc func(context.Context, map[string]interface{}) (postdog.Transport, error)

type rawConfig struct {
Default string `yaml:"default"`
Transports map[string]Transport `yaml:"transports"`
}

Expand Down Expand Up @@ -91,6 +93,7 @@ func (cfg *Config) Parse(raw []byte) error {
return fmt.Errorf("unmarshal yaml: %w", err)
}
cfg.transports = rawCfg.Transports
cfg.defaultTransport = rawCfg.Default
return nil
}

Expand Down Expand Up @@ -133,8 +136,13 @@ func (cfg *Config) Dog(ctx context.Context, opts ...Option) (*postdog.Dog, error
}

dogOpts = append(dogOpts, cfg.opts...)
dog := postdog.New(dogOpts...)

return postdog.New(dogOpts...), nil
if cfg.defaultTransport != "" {
dog.Use(cfg.defaultTransport)
}

return dog, nil
}

// Transport accepts the transport-specific configuration and instantiates a transport from that configuration.
Expand Down
44 changes: 44 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,50 @@ func TestConfig(t *testing.T) {
})
})
}))

Convey("Given a configuration with a default transport", WithParsedConfig("./testdata/with_default.yml", func(cfg *config.Config) {
Convey("When I instantiate postdog.Dog and provide the config.TransportFactories", func() {
factory1 := mock_config.NewMockTransportFactory(ctrl)
mockTransport1 := mock_postdog.NewMockTransport(ctrl)
factory1.EXPECT().
Transport(gomock.Any(), map[string]interface{}{
"key1": 1,
"key2": map[string]interface{}{
"key2.1": "val2.1",
"key2.2": "val2.2",
},
}).
Return(mockTransport1, nil)

factory2 := mock_config.NewMockTransportFactory(ctrl)
mockTransport2 := mock_postdog.NewMockTransport(ctrl)
factory2.EXPECT().
Transport(gomock.Any(), map[string]interface{}{
"key1": map[string]interface{}{
"key1.1": "val1.1",
"key1.2": "val1.2",
},
"key2": 2,
}).
Return(mockTransport2, nil)

dog, err := cfg.Dog(
context.Background(),
config.WithTransportFactory("trans1", factory1),
config.WithTransportFactory("trans2", factory2),
)

Convey("It shouldn't fail", func() {
So(err, ShouldBeNil)
})

Convey("dog should use the specified default transport", func() {
tr, err := dog.Transport("")
So(err, ShouldBeNil)
So(tr, ShouldEqual, mockTransport2)
})
})
}))
})
})
}
Expand Down
17 changes: 17 additions & 0 deletions config/testdata/with_default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
default: test2

transports:
test1:
use: trans1
config:
key1: 1
key2:
key2.1: val2.1
key2.2: val2.2
test2:
use: trans2
config:
key1:
key1.1: val1.1
key1.2: val1.2
key2: 2

0 comments on commit d46f68d

Please sign in to comment.