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

Utils: fix serialization cases in MapToString util #65

Merged
merged 1 commit into from
Jul 30, 2024
Merged
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
19 changes: 14 additions & 5 deletions internal/utils/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,28 @@ func MapSlice[T, K any](in []T, mapper func(item T) K) []K {
return out
}

// maptostring serializes the provided map into a a string.
// MapToString serializes the provided map into a a string.
func MapToString[K, T ~string](m map[K]T, keyValueSeperator, itemSeperator string) string {
if len(m) == 0 {
return ""
}

// An item separator is added before each item. For the first item we want
// the separator to be an empty string
itemSep := ""
bldr := strings.Builder{}
bldr.WriteString("--kernelmountoptions=")
for key, value := range m {
bldr.WriteString(itemSep)
bldr.WriteString(string(key))
bldr.WriteString(keyValueSeperator)
bldr.WriteString(string(value))
bldr.WriteString(itemSeperator)

// Skip value serialization if it evaluates to an empty string
valAsString := string(value)
if valAsString != "" {
bldr.WriteString(keyValueSeperator)
bldr.WriteString(valAsString)
}

itemSep = itemSeperator
}
return bldr.String()
}
Expand Down
Loading