modify

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 23 Imported by: 17

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v1.17.0

func Add(expression string) modify.Action

Add adds an expression to the matched specified function.

func AddCommand added in v1.17.0

func AddCommand(pkg, command string) error

AddCommand adds command to the foundation.Setup() chain in the Boot function.

func AddConfig

func AddConfig(name, expression string, annotations ...string) modify.Action

AddConfig adds a configuration key with the given expression to the config file.

func AddFilter added in v1.17.0

func AddFilter(pkg, filter string) error

AddFilter adds filter to the foundation.Setup() chain in the Boot function.

func AddImport

func AddImport(path string, name ...string) modify.Action

AddImport adds an import statement to the file.

func AddJob added in v1.17.0

func AddJob(pkg, job string) error

AddJob adds job to the foundation.Setup() chain in the Boot function.

func AddMiddleware added in v1.17.0

func AddMiddleware(pkg, middleware string) error

AddMiddleware adds middleware to the foundation.Setup() chain in the Boot function.

func AddMigration added in v1.17.0

func AddMigration(pkg, migration string) error

AddMigration adds migration to the foundation.Setup() chain in the Boot function.

func AddProvider added in v1.17.0

func AddProvider(pkg, provider string) error

AddProvider adds service provider to the foundation.Setup() chain in the Boot function.

func AddRoute added in v1.17.0

func AddRoute(pkg, route string) error

AddRoute adds route to the foundation.Setup() chain in the Boot function. Add WithRouting(func()) to foundation.Setup() if not exists. Add pkg to this file imports, and add route to the function body in WithRouting. the pkg is the package path of the route file, e.g., "goravel/routes" the route will be like "routes.Web()"

func AddRule added in v1.17.0

func AddRule(pkg, rule string) error

AddRule adds rule to the foundation.Setup() chain in the Boot function.

func AddSeeder added in v1.17.0

func AddSeeder(pkg, seeder string) error

AddSeeder adds seeder to the foundation.Setup() chain in the Boot function.

func Call added in v1.17.0

func Call(fn func(options []modify.Option) error) modify.Apply

func ExprExists

func ExprExists(x []dst.Expr, y dst.Expr) bool

ExprExists checks if an expression exists in a slice of expressions. It uses structural equality comparison via ExprIndex.

Parameters:

  • x: Slice of expressions to search in
  • y: Expression to search for

Returns true if the expression exists in the slice, false otherwise.

Example:

exprs := []dst.Expr{
	&dst.Ident{Name: "foo"},
	&dst.Ident{Name: "bar"},
}
target := &dst.Ident{Name: "foo"}
if ExprExists(exprs, target) {
	fmt.Println("Expression found")
}

func ExprIndex

func ExprIndex(x []dst.Expr, y dst.Expr) int

ExprIndex returns the index of the first occurrence of an expression in a slice. It uses structural equality comparison to match expressions.

Parameters:

  • x: Slice of expressions to search in
  • y: Expression to search for

Returns the index of the first occurrence, or -1 if not found.

Example:

exprs := []dst.Expr{
	&dst.Ident{Name: "foo"},
	&dst.Ident{Name: "bar"},
	&dst.Ident{Name: "baz"},
}
target := &dst.Ident{Name: "bar"}
index := ExprIndex(exprs, target) // returns 1

func File

func File(path string) modify.File

func GoFile

func GoFile(file string) modify.GoFile

func IsUsingImport

func IsUsingImport(df *dst.File, path string, name ...string) bool

IsUsingImport checks if an imported package is actually used in the file. It inspects the AST for selector expressions that reference the package.

Parameters:

  • df: The parsed Go file to inspect
  • path: Import path of the package (e.g., "github.com/goravel/framework/contracts/console")
  • name: Optional package name. If not provided, uses the last segment of the path

Returns true if the package is used anywhere in the file, false otherwise.

Example:

file, _ := decorator.Parse(src)
// Check if "console" package from "github.com/goravel/framework/contracts/console" is used
if IsUsingImport(file, "github.com/goravel/framework/contracts/console") {
	fmt.Println("console package is being used")
}
// Or specify a custom name
if IsUsingImport(file, "github.com/goravel/framework/contracts/console", "customName") {
	fmt.Println("Package with alias 'customName' is being used")
}

func KeyExists

func KeyExists(kvs []dst.Expr, key dst.Expr) bool

KeyExists checks if a key exists in a slice of key-value expressions. It uses structural equality comparison via KeyIndex.

Parameters:

  • kvs: Slice of expressions (expected to contain KeyValueExpr)
  • key: Key expression to search for

Returns true if the key exists in any KeyValueExpr, false otherwise.

Example:

kvExprs := []dst.Expr{
	&dst.KeyValueExpr{
		Key:   &dst.Ident{Name: "name"},
		Value: &dst.BasicLit{Value: `"John"`},
	},
	&dst.KeyValueExpr{
		Key:   &dst.Ident{Name: "age"},
		Value: &dst.BasicLit{Value: "30"},
	},
}
targetKey := &dst.Ident{Name: "name"}
if KeyExists(kvExprs, targetKey) {
	fmt.Println("Key found")
}

func KeyIndex

func KeyIndex(kvs []dst.Expr, key dst.Expr) int

KeyIndex returns the index of a key in a slice of key-value expressions. It searches for KeyValueExpr nodes and compares their keys using structural equality.

Parameters:

  • kvs: Slice of expressions (expected to contain KeyValueExpr)
  • key: Key expression to search for

Returns the index of the first KeyValueExpr with matching key, or -1 if not found.

Example:

kvExprs := []dst.Expr{
	&dst.KeyValueExpr{
		Key:   &dst.Ident{Name: "name"},
		Value: &dst.BasicLit{Value: `"John"`},
	},
	&dst.KeyValueExpr{
		Key:   &dst.Ident{Name: "age"},
		Value: &dst.BasicLit{Value: "30"},
	},
}
targetKey := &dst.Ident{Name: "age"}
index := KeyIndex(kvExprs, targetKey) // returns 1

func MustParseExpr

func MustParseExpr(x string) (node dst.Node)

MustParseExpr parses a Go expression from a string and returns its AST node. It wraps the expression in a minimal valid Go program to parse it, then extracts and returns the expression node with proper decorations and newlines.

Parameters:

  • x: String representation of a Go expression

Returns the parsed expression as a dst.Node, with decorations preserved. Panics if the expression cannot be parsed.

Example:

// Parse a simple expression
node := MustParseExpr("&commands.ExampleCommand{}")
// Returns a UnaryExpr node representing the address-of operation

// Parse a composite literal
node := MustParseExpr(`map[string]interface{}{"key": "value"}`)
// Returns a CompositeLit node

// Parse a function call
node := MustParseExpr("fmt.Println(\"hello\")")
// Returns a CallExpr node

func Register

func Register(expression string, before ...string) modify.Action

Register adds a registration to the matched specified array.

func RegisterMigration added in v1.17.0

func RegisterMigration(pkg, migration string) modify.Apply

func RegisterProvider added in v1.17.0

func RegisterProvider(pkg, provider string) modify.Apply

func RegisterRoute added in v1.17.0

func RegisterRoute(pkg, route string) modify.Apply

func Remove added in v1.17.0

func Remove(expression string) modify.Action

Remove removes an expression from the matched specified function.

func RemoveConfig

func RemoveConfig(name string) modify.Action

RemoveConfig removes a configuration key from the config file.

func RemoveImport

func RemoveImport(path string, name ...string) modify.Action

RemoveImport removes an import statement from the file.

func RemoveMigration added in v1.17.0

func RemoveMigration(pkg, migration string) error

func RemoveProvider added in v1.17.0

func RemoveProvider(pkg, provider string) error

RemoveProvider removes a service provider from the foundation.Setup() chain in the Boot function.

func RemoveRoute added in v1.17.0

func RemoveRoute(pkg, route string) error

RemoveRoute removes a route from the foundation.Setup() chain in the Boot function.

func ReplaceConfig

func ReplaceConfig(name, expression string) modify.Action

ReplaceConfig replaces a configuration key with the given expression in the config file.

func Unregister

func Unregister(expression string) modify.Action

Unregister remove a registration from the matched specified array.

func UnregisterMigration added in v1.17.0

func UnregisterMigration(pkg, migration string) modify.Apply

func UnregisterProvider added in v1.17.0

func UnregisterProvider(pkg, provider string) modify.Apply

func UnregisterRoute added in v1.17.0

func UnregisterRoute(pkg, route string) modify.Apply

func When added in v1.17.0

func When(fn func(options map[string]any) bool, applies ...modify.Apply) modify.Apply

func WhenDriver added in v1.17.0

func WhenDriver(driver string, applies ...modify.Apply) modify.Apply

func WhenFacade

func WhenFacade(facade string, applies ...modify.Apply) modify.Apply

func WhenFileContains added in v1.17.0

func WhenFileContains(file, content string, applies ...modify.Apply) modify.Apply

func WhenFileExists added in v1.17.0

func WhenFileExists(file string, applies ...modify.Apply) modify.Apply

func WhenFileNotContains added in v1.17.0

func WhenFileNotContains(file, content string, applies ...modify.Apply) modify.Apply

func WhenFileNotExists added in v1.17.0

func WhenFileNotExists(file string, applies ...modify.Apply) modify.Apply

func WhenNoFacades

func WhenNoFacades(facades []string, applies ...modify.Apply) modify.Apply

func WrapNewline

func WrapNewline[T dst.Node](node T) T

WrapNewline adds newline decorations to specific AST nodes for better formatting. It traverses the AST and adds Before/After newlines to KeyValueExpr, UnaryExpr, and FuncType result nodes to improve code readability.

Parameters:

  • node: Any dst.Node to process

Returns the same node with newline decorations applied.

Example:

// Parse and wrap an expression
expr := MustParseExpr("&commands.ExampleCommand{}")
// The UnaryExpr will have newlines before and after

// For a composite literal with key-value pairs:
node := MustParseExpr(`map[string]int{"a": 1, "b": 2}`)
wrapped := WrapNewline(node)
// Each KeyValueExpr will have newlines for better formatting:
// map[string]int{
//     "a": 1,
//     "b": 2,
// }

Types

This section is empty.

Jump to

Keyboard shortcuts

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