Skip to content

Commit

Permalink
generate doc with indentation: compatable for windows \r\n
Browse files Browse the repository at this point in the history
  • Loading branch information
amyXia1994 committed Jun 29, 2023
1 parent cb95bb6 commit 2186a7f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/swagger/generator/template_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"path"
"path/filepath"
"runtime"
"strings"
"text/template"
"text/template/parse"
Expand Down Expand Up @@ -488,9 +489,17 @@ func padSurround(entry, padWith string, i, ln int) string {
return strings.Join(res, ",")
}

// padDocument indent multi line document with given pad
func padDocument(str string, pad string) string {
// indent multi line document with given pad
lines := strings.Split(str, "\n")
// get the OS name
// set the appropriate line separator
var lineSeparator string
if runtime.GOOS == "windows" {
lineSeparator = "\r\n"
} else {
lineSeparator = "\n"
}
lines := strings.Split(str, lineSeparator)
paddingLines := make([]string, 0, len(lines))
for _, line := range lines {
paddingLine := line
Expand All @@ -500,7 +509,7 @@ func padDocument(str string, pad string) string {
paddingLines = append(paddingLines, paddingLine)
}
// no indenting before cascading empty lines
return strings.Join(paddingLines, "\n")
return strings.Join(paddingLines, lineSeparator)
}

func padComment(str string, pads ...string) string {
Expand Down

0 comments on commit 2186a7f

Please sign in to comment.