templateapi

package
v0.0.0-...-293b089 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTemplateNotFound             = errors.New("template with the given ID is not found")
	ErrFailedToParseTemplate        = errors.New("failed to parse the template")
	ErrFailedToRenderTemplate       = errors.New("failed to render the template")
	ErrFailedToCopyTemplateFile     = errors.New("failed to copy the template file")
	ErrFailedToDownloadTemplateFile = errors.New("failed to download the template file")
	ErrTemplateFileOrUrlIsRequired  = errors.New("template has no source template or source url")
)
View Source
var DefaultSnippets = []string{"global-layout.gohtml"}
View Source
var TemplateFunctions = template.FuncMap{
	"hasPrefix": func(s, prefix string) bool {
		return strings.HasPrefix(s, prefix)
	},
	"hasSuffix": func(s, suffix string) bool {
		return strings.HasSuffix(s, suffix)
	},
	"firstNonEmpty": func(values ...string) string {
		return util.FirstNonEmptyString(values...)
	},
	"join": func(values []string, sep string) string {
		return strings.Join(values, sep)
	},
	"lowerCase": func(input string) string {
		return strings.ToLower(input)
	},
	"upperCase": func(input string) string {
		return strings.ToUpper(input)
	},
	"lowerCaseFirstLetter": func(input string) string {
		return util.LowerCaseFirstLetter(input)
	},
	"upperCaseFirstLetter": func(input string) string {
		return util.UpperCaseFirstLetter(input)
	},
	"upperCaseFirstLetterOnly": func(input string) string {
		return util.UpperCaseFirstLetterOnly(input)
	},
	"trimNonASCII": func(input string) string {
		return util.TrimNonASCII(input)
	},
	"pascalCase": func(input string) string {
		return util.ToPascalCase(input)
	},
	"snakeCase": func(input string) string {
		return util.ToSnakeCase(input)
	},
	"kebabCase": func(input string) string {
		return util.ToKebabCase(input)
	},
	"camelCase": func(input string) string {
		return util.ToCamelCase(input)
	},
	"slug": func(input string) string {
		return util.ToSlug(input)
	},
	"commentSingleLine": func(input string) string {
		return util.CommentSingleLine(input)
	},
	"commentMultiLine": func(prefix, input string) string {
		return util.CommentMultiLine(input, prefix)
	},
	"escapeJavadoc": func(input string) string {

		for entity, placeholder := range javadocPlaceholders {
			input = strings.ReplaceAll(input, entity, placeholder)
		}

		input = strings.ReplaceAll(input, "&", "&")

		input = strings.ReplaceAll(input, "<", "&lt;")
		input = strings.ReplaceAll(input, ">", "&gt;")

		input = strings.ReplaceAll(input, "/*", "/&#42;")
		input = strings.ReplaceAll(input, "*/", "&#42;/")

		for entity, placeholder := range javadocPlaceholders {
			input = strings.ReplaceAll(input, placeholder, entity)
		}

		return input
	},
	"escapeStringValue": func(input string) string {
		escaped := strconv.Quote(input)
		return escaped[1 : len(escaped)-1]
	},
	"wrapIn": func(left string, right string, input string) string {
		return left + input + right
	},
	"conditionalValue": func(condition bool, trueValue, falseValue interface{}) interface{} {
		return util.Ternary(condition, trueValue, falseValue)
	},
	"marshalJSON": func(input interface{}) string {
		a, _ := json.Marshal(input)
		return string(a)
	},
	"marshalYAML": func(input interface{}) string {
		a, _ := yaml.Marshal(input)
		return string(a)
	},
	"isEmpty": func(input string) bool {
		return input == ""
	},
	"isNotEmpty": func(input string) bool {
		return input != ""
	},

	"toFilePath": func(input string) string {
		return strings.ReplaceAll(input, ".", string(os.PathSeparator))
	},

	"notLast": func(data interface{}, idx interface{}) bool {
		val := reflect.ValueOf(data)
		if val.Kind() == reflect.Slice {
			idxInt, ok := idx.(int)
			if !ok {
				return false
			}

			return idxInt < val.Len()-1
		} else if val.Kind() == reflect.Map {
			idxStr, ok := idx.(string)
			if !ok {
				return false
			}

			return idxStr != val.MapKeys()[val.Len()-1].String()
		}

		return false
	},
}

Functions

This section is empty.

Types

type Config

type Config struct {
	ID          string // ID is a unique identifier for the template, should be a combination of the spec type, generator and template name (openapi-go-client, asyncapi-java-client, etc.)
	Description string // Description is a human-readable description, only used to list available templates
	Files       []File // Files is a list of files that will be rendered
}

func (Config) FilesByType

func (c Config) FilesByType(t Type) []File

type File

type File struct {
	Description     string   // Description is a human-readable description of the template
	SourceTemplate  string   // SourceTemplate is the path to the template file
	SourceFile      string   // SourceFile is the path to a file that will be copied as is
	SourceUrl       string   // SourceUrl is the URL where the template or binary file can be downloaded from
	Snippets        []string // Snippets is a list of paths to files that contain snippets that can be used in the template
	TargetDirectory string   // TargetDirectory is the directory where the rendered file will be saved
	TargetFileName  string   // TargetFileName contains the template for the file name
	Type            Type     // Type is the type of the template
	Kind            Kind     // Kind is the kind of the template, can be used to filter which templates to render
	Category        []string // Category is a list of categories that the template belongs to, can be used to filter which templates to render

}

type FileState

type FileState string
const (
	FileDryRun       FileState = "dry-run"
	FileSkippedName  FileState = "skipped-by-name"
	FileSkippedScope FileState = "skipped-by-scope"
	FileRendered     FileState = "rendered"
)

type Kind

type Kind string
const (
	KindAPI           Kind = "api"
	KindModel         Kind = "model"
	KindDocumentation Kind = "documentation"
	KindBuildSystem   Kind = "buildsystem"
)

type RenderOpts

type RenderOpts struct {
	DryRun               bool                                     // DryRun will not write any files to disk
	Types                []Type                                   // Types can be used to only render files of specific types
	IgnoreFiles          []string                                 // IgnoreFiles is a list of file names that should not be rendered
	IgnoreFileCategories []string                                 // IgnoreFileCategories is a list of file categories that should not be rendered
	Properties           map[string]string                        // User-defined properties that can be used in the templates
	PostProcess          func(name string, content []byte) []byte // PostProcess is a function that can be used to post-process file output
	TemplateFunctions    template.FuncMap                         // TemplateFunctions is a map of additional functions that can be used in the templates
}

type RenderedFile

type RenderedFile struct {
	File         string
	TemplateFile string
	State        FileState
}

type Type

type Type string
const (
	TypeAPIOnce       Type = "api_once"
	TypeAPIEach       Type = "api_each"
	TypeOperationEach Type = "operation_each"
	TypeModelEach     Type = "model_each"
	TypeEnumEach      Type = "enum_each"
	TypeSupportOnce   Type = "support_once"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL