Skip to content

Commit

Permalink
Update comma_space_map_modifier.go
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Dec 16, 2024
1 parent 27beaed commit 3c8e9a9
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions internal/pkg/modifiers/comma_space_map_modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,33 @@ func (m CommaSpaceMapModifier) PlanModifyMap(ctx context.Context, req planmodifi
planElements := req.PlanValue.Elements()
newElements := make(map[string]types.String)

// Check config format if available
var configFormat bool // true = spaces, false = no spaces
// Track config format for each key
configFormats := make(map[string]bool)

// Detect config format for each key
if !req.ConfigValue.IsNull() {
configElements := req.ConfigValue.Elements()
// Look at first value to determine format
for _, v := range configElements {
for key, v := range configElements {
if str, ok := v.(types.String); ok && !str.IsNull() {
configFormat = strings.Contains(str.ValueString(), ", ")
break
configFormats[key] = strings.Contains(str.ValueString(), ", ")
}
}
}

// Fallback to state value if config format not found
if !req.StateValue.IsNull() {
stateElements := req.StateValue.Elements()
for key, v := range stateElements {
if _, exists := configFormats[key]; !exists {
if str, ok := v.(types.String); ok && !str.IsNull() {
configFormats[key] = strings.Contains(str.ValueString(), ", ")
}
}
}
}

for key, value := range planElements {
strValue, ok := value.(types.String)

if !ok {
continue
}
Expand All @@ -53,8 +64,13 @@ func (m CommaSpaceMapModifier) PlanModifyMap(ctx context.Context, req planmodifi
parts[i] = strings.TrimSpace(part)
}

useSpaces, found := configFormats[key]
if !found {
useSpaces = false
}

var formattedValue string
if configFormat {
if useSpaces {
formattedValue = strings.Join(parts, ", ")
} else {
formattedValue = strings.Join(parts, ",")
Expand Down

0 comments on commit 3c8e9a9

Please sign in to comment.