Skip to content

Commit

Permalink
fix: fill record array config defaults (#309)
Browse files Browse the repository at this point in the history
Co-authored-by: Nishant H <[email protected]>
Co-authored-by: Patryk Małek <[email protected]>
  • Loading branch information
3 people authored Apr 7, 2023
1 parent 6745223 commit 2f79c1f
Show file tree
Hide file tree
Showing 2 changed files with 546 additions and 1 deletion.
40 changes: 39 additions & 1 deletion kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,19 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
// an arbitrary map, field is already set.
return true
}
case []interface{}:
if value.Get(fname).Get("elements.type").String() != "record" &&
config[fname] != nil &&
len(config[fname].([]interface{})) > 0 {
// Non empty array with elements which are not of type record
// this means field is already set
return true
}
default:
// not a map, field is already set.
return true
if v != nil {
return true
}
}
}
ftype := value.Get(fname + ".type")
Expand All @@ -249,6 +259,34 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
res[fname] = map[string]interface{}(newSubConfig)
return true
}

// Check if field is of type array of records (in Schema)
// If this array is non-nil and non-empty (in Config), go through all the records in this array and add defaults
// If the array has only primitives like string/number/boolean then the value is already set
// If the array is empty or nil, then no defaults need to be set for its elements
if ftype.String() == "array" {
if value.Get(fname).Get("elements.type").String() == "record" {
if config[fname] != nil {
// Check sub config is of type array and it is non-empty
if subConfigArray, ok := config[fname].([]interface{}); ok && len(subConfigArray) > 0 {
processedSubConfigArray := make([]interface{}, len(subConfigArray))

for i, configRecord := range subConfigArray {
// Check if element is of type record, if it is, set default values by recursively calling `fillConfigRecord`
if configRecordMap, ok := configRecord.(map[string]interface{}); ok {
processedConfigRecord := fillConfigRecord(value.Get(fname).Get("elements"), configRecordMap)
processedSubConfigArray[i] = processedConfigRecord
continue
}
// Element not of type record, keep the value as is
processedSubConfigArray[i] = configRecord
}
res[fname] = processedSubConfigArray
return true
}
}
}
}
value = value.Get(fname + ".default")
if value.Exists() {
res[fname] = value.Value()
Expand Down
Loading

0 comments on commit 2f79c1f

Please sign in to comment.