generate

package
v0.0.0-...-2fb256d Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package generate contains types and functions that can be used to generate immutable types from existing Go struct types.

Index

Constants

View Source
const (
	ImmutableSuffix     = "_immutable.go"
	ImmutableTestSuffix = "_immutable_test.go"
)

Variables

This section is empty.

Functions

func Generate

func Generate(node *ast.File, fs *token.FileSet, builder *bytes.Buffer, targetStruct string) (bool, error)

Generate generates an immutable version of the target struct from the provided Go file. Returns true if the target struct was found and processed. The ouptut is written to the provided strings.Builder.

Types

type Field

type Field struct {
	PublicName  string // Original public field name
	PrivateName string // Private field name
	Type        string // Field type
	WasPublic   bool   // True if the original field was public
	Comment     string // Comment associated with the field
	IsImmutable bool   // True if the field is immutable.Map or immutable.Slice
	GenericType string // Generic type for immutable.Map or immutable.Slice
}

Field represents metadata about a struct field. Public for templates.

type Method

type Method struct {
	Name            string  // Method name
	Params          string  // Parameters list
	Results         string  // Return types
	Body            string  // Method body
	FullReceiver    string  // The exact receiver (e.g. "*Record[T]")
	NewReceiver     string  // The new receiver (e.g. "*ImRecord[T]")
	ReceiverVar     string  // Receiver var, aka (r *Record[T]) would be "r"
	StructName      string  // Original struct name
	ImmutableStruct string  // Immutable struct name
	GenericUsage    string  // Generic usage (e.g., [T])
	ReceiverComment string  // Comment associated with the receiver
	StructFields    []Field // Fields of the struct
}

Method represents a method to be copied to the immutable struct. Public for templates.

func (Method) ImBody

func (m Method) ImBody() string

ImBody returns the method body with lowercased field references.

type StructData

type StructData struct {
	Package       string   // Package name
	Name          string   // The name of the struct (prepended with Im)
	OriginalName  string   // The original struct name
	Fields        []Field  // The fields of the struct
	Comment       string   // The original struct's comment
	GenericParams string   // Full generic parameter list (e.g., [T any])
	GenericUsage  string   // Generic usage (e.g., [T])
	Methods       []Method // The methods to be copied to the immutable struct
	Imports       []string // The imports needed for the struct
	UsesImmutable bool     // True if any field is an immutable.Map or immutable.Slice, requiring the immutable import
}

StructData holds the data needed to generate a struct and its methods. Public for templates.

Jump to

Keyboard shortcuts

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