Skip to content

Commit

Permalink
refactor: simplify FillPluginsDefaults signature (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele authored Jan 23, 2022
1 parent bfe90b4 commit e965de2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions kong/plugin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,14 @@ func TestFillPluginDefaults(T *testing.T) {

for _, tc := range tests {
T.Run(tc.name, func(t *testing.T) {
fullSchema, err := client.Plugins.GetFullSchema(defaultCtx, tc.plugin.Name)
p := tc.plugin
fullSchema, err := client.Plugins.GetFullSchema(defaultCtx, p.Name)
assert.Nil(err)
assert.NotNil(fullSchema)
got, err := FillPluginsDefaults(tc.plugin, fullSchema)
if err != nil {
if err := FillPluginsDefaults(p, fullSchema); err != nil {
t.Errorf(err.Error())
}
if diff := cmp.Diff(got, tc.expected); diff != "" {
if diff := cmp.Diff(p, tc.expected); diff != "" {
t.Errorf(diff)
}
})
Expand Down
9 changes: 5 additions & 4 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,12 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
return res
}

// FillPluginsDefaults ingests plugin's defaults from its schema
func FillPluginsDefaults(plugin *Plugin, schema map[string]interface{}) (*Plugin, error) {
// FillPluginsDefaults ingests plugin's defaults from its schema.
// Takes in a plugin struct and mutate it in place.
func FillPluginsDefaults(plugin *Plugin, schema map[string]interface{}) error {
jsonb, err := json.Marshal(&schema)
if err != nil {
return nil, err
return err
}
gjsonSchema := gjson.ParseBytes((jsonb))
if plugin.Config == nil {
Expand All @@ -225,5 +226,5 @@ func FillPluginsDefaults(plugin *Plugin, schema map[string]interface{}) (*Plugin
if plugin.Enabled == nil {
plugin.Enabled = Bool(true)
}
return plugin, nil
return nil
}

0 comments on commit e965de2

Please sign in to comment.