Skip to content

Commit

Permalink
fix: adjust multiline string formatting in resource generation (#1482)
Browse files Browse the repository at this point in the history
* fix: adjust multiline string formatting in resource generation

* fix: simplify multiline string formatting in resource generation tests
  • Loading branch information
battlebyte authored Jan 6, 2025
1 parent f73569d commit caaf75c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion kong2tf/generate_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func quote(input interface{}) string {
return fmt.Sprintf("%v", v)
case string:
if strings.Contains(v, "\n") {
return fmt.Sprintf("<<EOF\n%s\nEOF", strings.TrimRight(v, "\n"))
return fmt.Sprintf("\n<<EOF\n%s\nEOF\n", strings.TrimRight(v, "\n"))
}
return fmt.Sprintf("\"%s\"", strings.ReplaceAll(v, "\"", "\\\""))
default:
Expand Down
45 changes: 33 additions & 12 deletions kong2tf/generate_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func TestGenerateComplexLayout(t *testing.T) {
}
}
},
"field4": []
"field4": [],
"field5": "Multi\nline\nstring",
"field6": ["Multi\nline\nstring", "Multi\nline\nstring"]
}`

var entity map[string]any
Expand All @@ -34,20 +36,39 @@ func TestGenerateComplexLayout(t *testing.T) {
field1 = "basic_string"
field2 = ["list", "of", "strings"]
field3 = {
nested_field1 = "nested_string"
nested_field2 = ["list", "of", "nested", "strings"]
nested_field3 = {
nested_nested_field1 = "nested_nested_string"
nested_nested_field2 = ["list", "of", "nested", "nested", "strings"]
nested_nested_field3 = {
nested_nested_nested_field1 = "nested_nested_nested_string"
}
}
nested_field1 = "nested_string"
nested_field2 = ["list", "of", "nested", "strings"]
nested_field3 = {
nested_nested_field1 = "nested_nested_string"
nested_nested_field2 = ["list", "of", "nested", "nested", "strings"]
nested_nested_field3 = {
nested_nested_nested_field1 = "nested_nested_nested_string"
}
}
}
field4 = []
field4 = []
field5 =
<<EOF
Multi
line
string
EOF
field6 = [
<<EOF
Multi
line
string
EOF
,
<<EOF
Multi
line
string
EOF
]
service = {
id = konnect_gateway_service.some_service.id
id = konnect_gateway_service.some_service.id
}
control_plane_id = var.control_plane_id
}
Expand Down

0 comments on commit caaf75c

Please sign in to comment.