Skip to content

Commit

Permalink
Merge pull request #1549 from estroz/refactor/plugin-config-ergonomics
Browse files Browse the repository at this point in the history
pkg/model/config: wrap plugin config in types
  • Loading branch information
k8s-ci-robot committed Jun 4, 2020
2 parents 977eb88 + 49c89ed commit cd5eed9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ version: "2"
Version: config.Version2,
Repo: "github.com/example/project",
Domain: "example.com",
Plugins: map[string]interface{}{
Plugins: config.PluginConfigs{
"plugin-x": map[string]interface{}{
"data-1": "single plugin datum",
},
Expand Down Expand Up @@ -144,7 +144,7 @@ plugins:
Version: config.Version2,
Repo: "github.com/example/project",
Domain: "example.com",
Plugins: map[string]interface{}{
Plugins: config.PluginConfigs{
"plugin-x": map[string]interface{}{
"data-1": "single plugin datum",
},
Expand Down
14 changes: 10 additions & 4 deletions pkg/model/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ type Config struct {
// Layout contains a key specifying which plugin created a project.
Layout string `json:"layout,omitempty"`

// Plugins is an arbitrary YAML blob that can be used by external
// plugins for plugin-specific configuration.
Plugins map[string]interface{} `json:"plugins,omitempty"`
// Plugins holds plugin-specific configs mapped by plugin key. These configs should be
// encoded/decoded using EncodePluginConfig/DecodePluginConfig, respectively.
Plugins PluginConfigs `json:"plugins,omitempty"`
}

// PluginConfigs holds a set of arbitrary plugin configuration objects mapped by plugin key.
type PluginConfigs map[string]pluginConfig

// pluginConfig is an arbitrary plugin configuration object.
type pluginConfig interface{}

// IsV1 returns true if it is a v1 project
func (c Config) IsV1() bool {
return c.Version == Version1
Expand Down Expand Up @@ -186,7 +192,7 @@ func (c *Config) EncodePluginConfig(key string, configObj interface{}) error {
return fmt.Errorf("failed to unmarshal %T object bytes: %s", configObj, err)
}
if c.Plugins == nil {
c.Plugins = make(map[string]interface{})
c.Plugins = make(map[string]pluginConfig)
}
c.Plugins[key] = fields
return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/model/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ = Describe("Config", func() {
pluginConfig = PluginConfig{}
expectedConfig = Config{
Version: Version2,
Plugins: map[string]interface{}{
Plugins: PluginConfigs{
"plugin-x": map[string]interface{}{
"data-1": "",
"data-2": "",
Expand All @@ -72,7 +72,7 @@ var _ = Describe("Config", func() {
}
expectedConfig = Config{
Version: Version2,
Plugins: map[string]interface{}{
Plugins: PluginConfigs{
"plugin-x": map[string]interface{}{
"data-1": "plugin value 1",
"data-2": "plugin value 2",
Expand Down Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Config", func() {
By("Using empty config version 2")
config = Config{
Version: Version2,
Plugins: map[string]interface{}{
Plugins: PluginConfigs{
"plugin-x": map[string]interface{}{},
},
}
Expand All @@ -125,7 +125,7 @@ var _ = Describe("Config", func() {
By("Using config version 2 with extra fields as struct")
config = Config{
Version: Version2,
Plugins: map[string]interface{}{
Plugins: PluginConfigs{
"plugin-x": map[string]interface{}{
"data-1": "plugin value 1",
"data-2": "plugin value 2",
Expand Down

0 comments on commit cd5eed9

Please sign in to comment.