Skip to content

Commit

Permalink
feat(kafka_quota): added support for kafka quota
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 23, 2024
1 parent b6b98ba commit 13df156
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions internal/acctest/template_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package acctest

import (
"regexp"
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -427,18 +427,51 @@ resource "aiven_kafka_user" "example_service_user" {

// normalizeHCL function remains the same
func normalizeHCL(s string) string {
// Remove all whitespace between blocks
s = regexp.MustCompile(`}\s+resource`).ReplaceAllString(s, "}\nresource")
s = regexp.MustCompile(`}\s+data`).ReplaceAllString(s, "}\ndata")
// Split into lines for processing
lines := strings.Split(s, "\n")
var normalized []string

Check failure on line 432 in internal/acctest/template_test.go

View workflow job for this annotation

GitHub Actions / make_lint

Consider pre-allocating `normalized` (prealloc)

// Remove all empty lines
s = regexp.MustCompile(`(?m)^\s*$`).ReplaceAllString(s, "")
for _, line := range lines {
// Trim spaces from both ends
line = strings.TrimSpace(line)

// Remove leading/trailing whitespace
s = strings.TrimSpace(s)
// Skip empty lines
if line == "" {
continue
}

// Handle lines with just closing braces
if line == "}" {
normalized = append(normalized, line)
continue
}

// For lines with content, normalize internal spacing
if strings.Contains(line, "=") {
// Split by = and trim spaces
parts := strings.Split(line, "=")
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
// Reconstruct with consistent spacing
line = fmt.Sprintf(" %s = %s", key, value)
}
} else if strings.HasPrefix(line, "resource") || strings.HasPrefix(line, "data") {
// Handle resource and data block declarations
line = strings.TrimSpace(line)
} else if !strings.HasPrefix(line, "}") {
// Add consistent indentation for other non-empty lines
line = " " + strings.TrimSpace(line)
}

normalized = append(normalized, line)
}

// Join lines with newlines
result := strings.Join(normalized, "\n")

// Normalize line endings
s = strings.ReplaceAll(s, "\r\n", "\n")
result = strings.ReplaceAll(result, "\r\n", "\n")

return s
return result
}

0 comments on commit 13df156

Please sign in to comment.