-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add GetFullSchema and FillDefaults methods to plugins #114
Changes from all commits
91478a1
a9cf627
2caa2cf
1fe520f
5742167
7e14314
029bce9
36bf996
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ package kong | |
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/blang/semver/v4" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
const ( | ||
|
@@ -141,3 +143,87 @@ func VersionFromInfo(info map[string]interface{}) string { | |
} | ||
return version.(string) | ||
} | ||
|
||
func getDefaultProtocols(schema gjson.Result) []*string { | ||
var res []*string | ||
fields := schema.Get("fields") | ||
|
||
for _, field := range fields.Array() { | ||
protocols := field.Map()["protocols"] | ||
d := protocols.Get("default") | ||
if d.Exists() { | ||
for _, v := range d.Array() { | ||
res = append(res, String(v.String())) | ||
} | ||
return res | ||
} | ||
} | ||
return res | ||
} | ||
|
||
func fillConfigRecord(schema gjson.Result, config Configuration) Configuration { | ||
res := config.DeepCopy() | ||
value := schema.Get("fields") | ||
|
||
value.ForEach(func(key, value gjson.Result) bool { | ||
// get the key name | ||
ms := value.Map() | ||
fname := "" | ||
for k := range ms { | ||
fname = k | ||
break | ||
} | ||
|
||
if fname == "config" { | ||
newConfig := fillConfigRecord(value.Get(fname), config) | ||
res = newConfig | ||
return true | ||
} | ||
|
||
// check if key is already set in the config | ||
if _, ok := config[fname]; ok { | ||
// yes, don't set it | ||
return true | ||
} | ||
ftype := value.Get(fname + ".type") | ||
if ftype.String() == "record" { | ||
subConfig := config[fname] | ||
if subConfig == nil { | ||
subConfig = make(map[string]interface{}) | ||
} | ||
newSubConfig := fillConfigRecord(value.Get(fname), subConfig.(map[string]interface{})) | ||
res[fname] = map[string]interface{}(newSubConfig) | ||
return true | ||
} | ||
value = value.Get(fname + ".default") | ||
if value.Exists() { | ||
res[fname] = value.Value() | ||
} else { | ||
// if no default exists, set an explicit nil | ||
res[fname] = nil | ||
} | ||
return true | ||
}) | ||
|
||
return res | ||
} | ||
|
||
// FillPluginsDefaults ingests plugin's defaults from its schema | ||
func FillPluginsDefaults(plugin *Plugin, schema map[string]interface{}) (*Plugin, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GGabriele I made a mistake in the review. Either of the following are fine (but together not):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh, my bad, sorry :( |
||
jsonb, err := json.Marshal(&schema) | ||
if err != nil { | ||
return nil, err | ||
} | ||
gjsonSchema := gjson.ParseBytes((jsonb)) | ||
if plugin.Config == nil { | ||
plugin.Config = make(Configuration) | ||
} | ||
plugin.Config = fillConfigRecord(gjsonSchema, plugin.Config) | ||
if plugin.Protocols == nil { | ||
plugin.Protocols = getDefaultProtocols(gjsonSchema) | ||
} | ||
if plugin.Enabled == nil { | ||
plugin.Enabled = Bool(true) | ||
} | ||
return plugin, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should remain as-is: it's more descriptive of the difference between this and GetSchema, and we already know this is plugin stuff since it's part of the plugin service.