manager

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileRenderState

type FileRenderState struct {
	FileName    string
	PackageName string
	Buffer      *bytes.Buffer
	Imports     *ImportsManager
}

type ImplementationItem

type ImplementationItem struct {
	Object    common.ImplementationObject
	Directory string // Evaluated template expression
}

type ImportItem

type ImportItem struct {
	Alias       string
	PackageName string
	PackagePath string
}

ImportItem represents an import in the generated Go file.

type ImportsManager

type ImportsManager struct {
	// contains filtered or unexported fields
}

ImportsManager manages the imports in the generated Go file.

func (*ImportsManager) AddImport

func (s *ImportsManager) AddImport(importPath string, pkgName string) string

AddImport adds a new import path to list. Prevents appearing the duplicated, conflicted or invalid imports in list.

The import path to add is passed in the first argument, the package name is extracted from the last part of import path. But if the package name argument is set, then it will be used instead.

If import path was already added, the function does nothing.

If the package name conflicts with the package name of the already added import, the function generates a new alias for this import. For example, the import text/template gets the alias “template2” if we already added the import html/template before.

If the package name is not a valid Go identifier, the function generates a new alias for this import as well. For example, the import github.com/bdragon300/go-asyncapi gets the alias “goAsyncapi”, because “go-asyncapi” is not a valid Go identifier.

Function returns the imported name, used to access that package in Go code. It can be the package name (for “import "net/url"” returns “url”) or the alias (for “import goAsyncapi "github.com/bdragon300/go-asyncapi"” returns “goAsyncapi”).

func (*ImportsManager) Clone

func (s *ImportsManager) Clone() *ImportsManager

func (*ImportsManager) Imports

func (s *ImportsManager) Imports() []ImportItem

Imports returns the sorted list of imports.

func (*ImportsManager) String

func (s *ImportsManager) String() string

type NamespaceArtifactItem added in v0.3.0

type NamespaceArtifactItem struct {
	Object      common.Artifact
	Layout      common.ConfigLayoutItem
	FileName    string
	PackageName string
	Rendered    bool
}

type NamespaceManager

type NamespaceManager struct {
	// contains filtered or unexported fields
}

NamespaceManager manages the template namespace, that is used for conditional rendering functionality in the templates. It keeps the rendered declarations of artifacts and objects that was explicitly defined in the template code. This functionality could remind the preprocessor namespace in C/C++, but for Go templates.

func (*NamespaceManager) AddObject added in v0.3.0

func (s *NamespaceManager) AddObject(o any)

AddObject adds any object to the namespace.

func (*NamespaceManager) Clone

func (s *NamespaceManager) Clone() *NamespaceManager

func (*NamespaceManager) ContainsObject added in v0.3.0

func (s *NamespaceManager) ContainsObject(o any) bool

ContainsObject checks if an object has been added to the namespace.

func (*NamespaceManager) DeclareArtifact added in v0.3.0

func (s *NamespaceManager) DeclareArtifact(a common.Artifact, renderManager *TemplateRenderManager, rendered bool)

DeclareArtifact adds an artifact declaration to the namespace.

Rendered object declaration flag is cumulative, meaning that once it becomes true, it will remain true in next calls, ignoring the passed value.

If rendered is true, it indicates that the artifact (e.g. if it is common.GolangType) has been rendered in the template.

func (*NamespaceManager) FindArtifact added in v0.3.0

func (s *NamespaceManager) FindArtifact(obj common.Artifact) (NamespaceArtifactItem, bool)

FindArtifact looks the namespace for the artifact declaration. If found, returns the declaration and true.

func (*NamespaceManager) String

func (s *NamespaceManager) String() string

type TemplateRenderManager

type TemplateRenderManager struct {
	// RenderOpts keeps the rendering options
	RenderOpts common.RenderOpts

	// CurrentObject is an object being currently rendered
	CurrentObject common.Artifact
	// CurrentLayoutItem is a layout item that was used to select the CurrentObject
	CurrentLayoutItem common.ConfigLayoutItem
	TemplateLoader    templateLoader

	// File state. The following fields are restored from committed
	FileName    string
	PackageName string
	// Buffer is write-only file contents buffer. When Commit is called, it appends to the committed file contents.
	Buffer *bytes.Buffer
	// ImportsManager keeps the imports list for the current file
	ImportsManager *ImportsManager
	// NamespaceManager keeps the template definitions namespace for the current file
	NamespaceManager *NamespaceManager
	// Implementations keeps the current list of implementations
	Implementations []ImplementationItem
	// contains filtered or unexported fields
}

TemplateRenderManager provides the transactional model for the rendering the files. It provides a way to make changes in the files with the ability to roll back to the last committed state.

File state includes the file name, package name, contents buffer, imports, and template namespace.

The typical workflow is load-write-commit cycle:

  1. Call BeginFile to load the committed state (or creates a new one) of given file. After that the file state starts to be exposed using the manager methods and fields.
  2. Write the content to file buffer or make other changes in the its state. These changes are considered as not committed yet and will be rolled back on next BeginFile call.
  3. Call Commit to commit file changes.

After all files are rendered, the committed states can be retrieved using Committed* methods.

TODO: Refactor, split in smaller parts. Separate the transactions as generic and separate implementations, current state expose using methods.

func NewTemplateRenderManager

func NewTemplateRenderManager(opts common.RenderOpts) *TemplateRenderManager

NewTemplateRenderManager returns a new instance of TemplateRenderManager with given rendering options.

func (*TemplateRenderManager) AddImplementation

func (r *TemplateRenderManager) AddImplementation(obj common.ImplementationObject, directory string)

AddImplementation adds a new implementation to the list, making uncommitted changes.

func (*TemplateRenderManager) BeginFile

func (r *TemplateRenderManager) BeginFile(fileName, packageName string)

BeginFile loads the committed state of given file into the manager fields, discarding any uncommitted changes. If the file is not loaded yet, it creates a new state.

func (*TemplateRenderManager) Commit

func (r *TemplateRenderManager) Commit()

Commit saves the current state to the committed state.

func (*TemplateRenderManager) CommittedImplementations

func (r *TemplateRenderManager) CommittedImplementations() []ImplementationItem

CommittedImplementations returns the committed implementations list.

func (*TemplateRenderManager) CommittedStates

func (r *TemplateRenderManager) CommittedStates() map[string]FileRenderState

CommittedStates returns the committed states of all files.

func (*TemplateRenderManager) SetCodeObject

func (r *TemplateRenderManager) SetCodeObject(obj common.Artifact, layoutItem common.ConfigLayoutItem)

SetCodeObject is helper that just sets the CurrentObject and CurrentLayoutItem fields.

Jump to

Keyboard shortcuts

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