repogen/internal/codegen/base.go
2023-05-24 14:04:29 +03:00

41 lines
871 B
Go

package codegen
import (
"fmt"
"strings"
"git.kmsign.ru/royalcat/repogen/internal/code"
)
const baseTemplate = `// Code generated by {{.Program}}. DO NOT EDIT.
package {{.PackageName}}
import (
{{.GenImports}}
)
`
type baseTemplateData struct {
Program string
PackageName string
Imports [][]code.Import
}
func (data baseTemplateData) GenImports() string {
var sections []string
for _, importSection := range data.Imports {
var section []string
for _, imp := range importSection {
section = append(section, data.generateImportLine(imp))
}
sections = append(sections, strings.Join(section, "\n"))
}
return strings.Join(sections, "\n\n")
}
func (data baseTemplateData) generateImportLine(imp code.Import) string {
if imp.Name == "" {
return fmt.Sprintf("\t\"%s\"", imp.Path)
}
return fmt.Sprintf("\t%s \"%s\"", imp.Name, imp.Path)
}