ast

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ast provides a thin façade over the syntetic/model AST helpers so you can use a consistent "builder" entrypoint for generating declarations and rendered files. It intentionally delegates to model.Type/GoFile/Package methods and does not duplicate AST construction logic.

Example (DeclNameOverride)

Example: declaration-name override without mutating the model.Type.

package main

import (
	"fmt"
	"go/ast"

	bast "github.com/viant/x/builder/ast"
	mdl "github.com/viant/x/syntetic/model"

	trf "github.com/viant/x/syntetic/model/transform"
)

func main() {
	t := &mdl.Type{PkgPath: "example.com/p", Name: "Orig", TypeSpec: &ast.TypeSpec{Name: ast.NewIdent("Orig"), Type: ast.NewIdent("int")}}
	b := bast.New(bast.WithTransforms(trf.DeclNameOverride(func(tp *mdl.Type) (string, bool) {
		if tp.Name == "Orig" {
			return "NewName", true
		}
		return "", false
	})))
	spec := b.TypeSpec(t, nil)
	fmt.Println(spec.Name.Name)
}
Output:
NewName

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder is a minimal façade that delegates to the syntetic/model package for AST generation and rendering. It exists to provide a uniform "builder" surface alongside builder/xreflect.

func New

func New(opts ...Option) *Builder

New returns a new AST builder. The builder is stateless; all options are provided per call via parameters.

func (*Builder) GenDecl

func (b *Builder) GenDecl(t *model.Type, aliases map[string]string) *ast.GenDecl

GenDecl builds a go/ast.GenDecl (type declaration) for t.

func (*Builder) RenderFile

func (b *Builder) RenderFile(f *model.GoFile, opts model.RenderOptions) (string, error)

RenderFile renders a single GoFile with the provided options.

func (*Builder) RenderPackage

func (b *Builder) RenderPackage(p *model.Package, opts model.RenderOptions, includeFreeFunctions bool) (map[string]string, error)

RenderPackage renders all files for p with the provided options. When includeFreeFunctions is true, free-standing function stubs are appended per file.

func (*Builder) TypeSpec

func (b *Builder) TypeSpec(t *model.Type, aliases map[string]string) *ast.TypeSpec

TypeSpec builds a go/ast.TypeSpec for t using its TypeParams and PkgPath. The aliases map optionally overrides import aliases when t references external packages (as used by TypeParams constraints); pass nil to use defaults.

func (*Builder) WritePackage

func (b *Builder) WritePackage(p *model.Package, dir string, opts model.RenderOptions, includeFreeFunctions bool) error

WritePackage writes all files for p into dir using the provided options.

type Option

type Option func(*Builder)

Option configures the AST builder.

func WithTransforms

func WithTransforms(t trf.Transformer) Option

WithTransforms configures the builder to consult the provided transformer for declaration-name overrides and, when used via helper methods, to apply transforms to packages prior to rendering.

Jump to

Keyboard shortcuts

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