Skip to content
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

added flattenMap to getCustomFields #655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions netbox/custom_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ var customFieldsSchema = &schema.Schema{
},
}

func flattenMap(m map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})

for key, value := range m {
switch v := value.(type) {
case map[string]interface{}:
// Recursively flatten the nested map
nestedResult := flattenMap(v)
for nestedKey, nestedValue := range nestedResult {
result[key+"__"+nestedKey] = nestedValue
}
case []interface{}:
// Flatten the array by adding each element with an index to the result
for i, element := range v {
if elementMap, ok := element.(map[string]interface{}); ok {
nestedResult := flattenMap(elementMap)
for nestedKey, nestedValue := range nestedResult {
result[key+"__"+fmt.Sprintf("%d", i)+"__"+nestedKey] = nestedValue
}
} else {
result[key+"__"+fmt.Sprintf("%d", i)] = element
}
}
default:
result[key] = value
}
}

return result
}

func getCustomFields(cf interface{}) map[string]interface{} {
cfm, ok := cf.(map[string]interface{})
if !ok || len(cfm) == 0 {
Expand Down