match

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: 7 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyNode

func AnyNode() match.GoNode

AnyNode creates a matcher that matches any node. For example, CallExpr(Ident("print"), GoNodes{AnyNode()}) matches print(x) where x can be any expression.

func AnyOf

func AnyOf(matchers ...match.GoNode) match.GoNode

AnyOf creates a matcher that succeeds if any of the provided matchers match. For example, AnyOf(Ident("foo"), Ident("bar")) matches either an identifier named "foo" or "bar".

func ArrayType

func ArrayType(elt, l match.GoNode) match.GoNode

ArrayType creates a matcher for array type expressions with element and length matchers. For example, ArrayType(Ident("int"), BasicLit("5")) matches [5]int.

func BasicLit

func BasicLit(value string) match.GoNode

BasicLit creates a matcher for basic literal expressions with a specific value. For example, BasicLit("\"hello\"") matches the string literal "hello".

func BootFunc added in v1.17.0

func BootFunc() []match.GoNode

BootFunc matches the Boot() method in service providers. This is used to perform actions after all services are registered.

Example usage:

match.GoFile("providers/custom_provider.go").
    Find(match.BootFunc()).
    Modify(addBootLogic).
    Apply()

This matches:

func (receiver *ServiceProvider) Boot(app foundation.Application) {
    facades.Route().Get("/health", healthController.Check)
    facades.Event().Register(events)
}

func CallExpr

func CallExpr(fun match.GoNode, args GoNodes) match.GoNode

CallExpr creates a matcher for function call expressions with specific function and argument matchers. For example, CallExpr(Ident("fmt.Println"), GoNodes{BasicLit("\"test\"")}) matches fmt.Println("test").

func Commands

func Commands() []match.GoNode

Commands matches the Commands() function that returns a slice of console commands. It looks for a function returning []console.Command composite literals.

Example usage:

match.GoFile("providers/custom_provider.go").
    Find(match.Commands()).
    Modify(addCommandToList(&CustomCommand{})).
    Apply()

This matches:

func (receiver *ServiceProvider) Commands() []console.Command {
    return []console.Command{
        &commands.MigrateCommand{},
        &commands.SeedCommand{},
    }
}

func CompositeLit

func CompositeLit(t match.GoNode) match.GoNode

CompositeLit creates a matcher for composite literal expressions with a type matcher. For example, CompositeLit(Ident("Person")) matches Person{} or Person{Name: "John"}.

func Config

func Config(key string) []match.GoNode

Config matches configuration additions in init functions. It searches for config.Add() or facades.Config().Add() calls with the specified key path. The key parameter uses dot notation to represent nested configuration keys.

Example usage:

match.GoFile("config/custom.go").
    Find(match.Config("database.connections.mysql")).
    Modify(modifyDatabaseConfig).
    Apply()

This matches:

func init() {
    config.Add("database", map[string]any{
        "connections": map[string]any{
            "mysql": map[string]any{
                "host": "localhost",
            },
        },
    })
}

func EqualNode

func EqualNode(n dst.Node) match.GoNode

EqualNode creates a matcher that checks if a node is equal to the provided node. For example, if target is &dst.Ident{Name: "x"}, then EqualNode(target) matches only identifier nodes with the exact name "x".

func Filters added in v1.17.0

func Filters() []match.GoNode

Filters matches the Filters() function that returns a slice of validation filters. It looks for a function returning []validation.Filter composite literals.

Example usage:

match.GoFile("providers/validation_provider.go").
    Find(match.Filters()).
    Modify(addFilterToList(&TrimFilter{})).
    Apply()

This matches:

func (receiver *ValidationServiceProvider) Filters() []validation.Filter {
    return []validation.Filter{
        &filters.TrimFilter{},
        &filters.SanitizeFilter{},
    }
}

func FirstOf

func FirstOf(n match.GoNode) match.GoNode

FirstOf creates a matcher that only matches the first element in a parent slice. For example, FirstOf(TypeOf(&dst.ImportSpec{})) matches only the first import statement in the import declarations.

func FoundationSetup added in v1.17.0

func FoundationSetup() []match.GoNode

FoundationSetup matches the Boot function containing foundation.Setup() chain calls. It matches both patterns:

  • foundation.Setup().WithConfig(...).Start()
  • foundation.Setup().WithMiddleware(...).WithConfig(...).Start()
  • foundation.Setup().WithCommand(...).Start()
  • return foundation.Setup().WithConfig(...).Start()

Example usage:

GoFile("bootstrap/app.go").
    Find(match.FoundationSetup()).
    Modify(foundationSetupMiddleware(middleware)).
    Apply()

func Func

func Func(name match.GoNode) match.GoNode

Func creates a matcher for function declarations with a specific name matcher. For example, Func(Ident("main")) matches func main() { ... }.

func Ident

func Ident(name string) match.GoNode

Ident creates a matcher for identifier nodes with a specific name. For example, Ident("x") matches the identifier x in expressions like x = 1 or return x.

func Import

func Import(path string, name ...string) match.GoNode

Import creates a matcher for import specifications with a specific path and optional name. For example, Import("fmt") matches import "fmt", and Import("fmt", "f") matches import f "fmt".

func Imports

func Imports() []match.GoNode

Imports matches import declaration blocks in Go source files. It identifies GenDecl nodes with IMPORT token type.

Example usage:

match.GoFile("main.go").
    Find(match.Imports()).
    Modify(addImportToBlock("github.com/goravel/framework")).
    Apply()

This matches:

import (
    "fmt"
    "strings"
    "github.com/goravel/framework/facades"
)

func Jobs

func Jobs() []match.GoNode

Jobs matches the Jobs() function that returns a slice of queue jobs. It looks for a function returning []queue.Job composite literals.

Example usage:

match.GoFile("providers/queue_provider.go").
    Find(match.Jobs()).
    Modify(addJobToList(&EmailJob{})).
    Apply()

This matches:

func (receiver *QueueServiceProvider) Jobs() []queue.Job {
    return []queue.Job{
        &jobs.EmailJob{},
        &jobs.ProcessDataJob{},
    }
}

func KeyValueExpr

func KeyValueExpr(key, value match.GoNode) match.GoNode

KeyValueExpr creates a matcher for key-value expressions with specific key and value matchers. For example, KeyValueExpr(Ident("Name"), BasicLit("\"John\"")) matches Name: "John" in struct literals.

func LastOf

func LastOf(n match.GoNode) match.GoNode

LastOf creates a matcher that only matches the last element in a parent slice. For example, LastOf(Ident("return")) matches return only if it's the last statement in a block.

func Migrations

func Migrations() []match.GoNode

Migrations matches the Migrations() function that returns a slice of database migrations. It looks for a function returning []schema.Migration composite literals.

Example usage:

match.GoFile("providers/migration_provider.go").
    Find(match.Migrations()).
    Modify(addMigrationToList(&CreateUsersTable{})).
    Apply()

This matches:

func (receiver *MigrationServiceProvider) Migrations() []schema.Migration {
    return []schema.Migration{
        &migrations.CreateUsersTable{},
        &migrations.CreatePostsTable{},
    }
}

func Providers

func Providers() []match.GoNode

Providers matches the Providers() function that returns a slice of service providers. It looks for a function returning []foundation.ServiceProvider composite literals. This is the recommended way to register service providers.

Example usage:

match.GoFile("config/app.go").
    Find(match.Providers()).
    Modify(addProviderToList(&CustomServiceProvider{})).
    Apply()

This matches:

func Providers() []foundation.ServiceProvider {
    return []foundation.ServiceProvider{
        &auth.ServiceProvider{},
        &cache.ServiceProvider{},
    }
}

func ProvidersInConfig deprecated added in v1.17.0

func ProvidersInConfig() []match.GoNode

Deprecated: ProvidersInConfig represents the old logic of registering service providers inside the `config/app.go` file.

This pattern is deprecated and will be removed in future versions. Please migrate to the new Providers() function-based registration approach. NOTE: in the current internal setup files, ProvidersInConfig is only used for uninstall (not for install). It will continue to serve as an uninstall-only fallback for the next few releases — during which both Providers() and ProvidersInConfig matchers will be supported to ensure backward compatibility with older versions. After that deprecation period, ProvidersInConfig will be removed entirely.

func RegisterFunc added in v1.17.0

func RegisterFunc() []match.GoNode

RegisterFunc matches the Register() method in service providers. This is used to register services into the service container during the boot process.

Example usage:

match.GoFile("providers/custom_provider.go").
    Find(match.RegisterFunc()).
    Modify(addServiceRegistration).
    Apply()

This matches:

func (receiver *ServiceProvider) Register(app foundation.Application) {
    app.Bind("custom", func() (any, error) {
        return &CustomService{}, nil
    })
}

func Rules added in v1.17.0

func Rules() []match.GoNode

Rules matches the Rules() function that returns a slice of validation rules. It looks for a function returning []validation.Rule composite literals.

Example usage:

match.GoFile("providers/validation_provider.go").
    Find(match.Rules()).
    Modify(addRuleToList(&CustomRule{})).
    Apply()

This matches:

func (receiver *ValidationServiceProvider) Rules() []validation.Rule {
    return []validation.Rule{
        &rules.EmailRule{},
        &rules.PhoneRule{},
    }
}

func Seeders

func Seeders() []match.GoNode

Seeders matches the Seeders() function that returns a slice of database seeders. It looks for a function returning []seeder.Seeder composite literals.

Example usage:

match.GoFile("providers/seeder_provider.go").
    Find(match.Seeders()).
    Modify(addSeederToList(&UserSeeder{})).
    Apply()

This matches:

func (receiver *SeederServiceProvider) Seeders() []seeder.Seeder {
    return []seeder.Seeder{
        &seeders.UserSeeder{},
        &seeders.PostSeeder{},
    }
}

func SelectorExpr

func SelectorExpr(x, sel match.GoNode) match.GoNode

SelectorExpr creates a matcher for selector expressions (e.g., x.sel) with specific matchers. For example, SelectorExpr(Ident("fmt"), Ident("Println")) matches fmt.Println.

func TypeOf

func TypeOf[T any](_ T) match.GoNode

TypeOf creates a matcher that checks if a node is of a specific type T. For example, TypeOf(&dst.CallExpr{}) matches any function call like foo(), bar(x), or fmt.Println("hello").

func ValidationFilters

func ValidationFilters() []match.GoNode

ValidationFilters matches the filters() function that returns validation filters. It looks for a lowercase filters() function returning []validation.Filter composite literals. This is typically used in validation service providers for internal filter registration.

Example usage:

match.GoFile("validation/setup.go").
    Find(match.ValidationFilters()).
    Modify(addValidationFilter(&TrimFilter{})).
    Apply()

This matches:

func filters() []validation.Filter {
    return []validation.Filter{
        &filters.Trim{},
        &filters.Lowercase{},
    }
}

func ValidationRules

func ValidationRules() []match.GoNode

ValidationRules matches the rules() function that returns validation rules. It looks for a lowercase rules() function returning []validation.Rule composite literals. This is typically used in validation service providers for internal rule registration.

Example usage:

match.GoFile("validation/setup.go").
    Find(match.ValidationRules()).
    Modify(addValidationRule(&EmailRule{})).
    Apply()

This matches:

func rules() []validation.Rule {
    return []validation.Rule{
        &rules.Required{},
        &rules.Email{},
    }
}

Types

type GoNode

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

GoNode represents a matcher for Go AST nodes with optional position constraints

func (GoNode) MatchCursor

func (r GoNode) MatchCursor(cursor *dstutil.Cursor) bool

MatchCursor checks if the cursor's current node matches this GoNode matcher. If first or last flags are set, it also verifies the node's position in its parent slice. For example, FirstOf(Ident("x")).MatchCursor(cursor) returns true only if cursor points to the first identifier "x" in its parent slice.

func (GoNode) MatchNode

func (r GoNode) MatchNode(node dst.Node) bool

MatchNode checks if the given node matches this GoNode matcher. For example, Ident("x").MatchNode(node) returns true if node is an identifier with the name "x".

type GoNodes

type GoNodes []match.GoNode

GoNodes is a collection of GoNode matchers

func AnyNodes

func AnyNodes() GoNodes

AnyNodes creates an empty GoNodes collection that matches any sequence of nodes. For example, CallExpr(Ident("print"), AnyNodes()) matches print() with any number of arguments.

func (GoNodes) MatchNodes

func (r GoNodes) MatchNodes(nodes []dst.Node) bool

MatchNodes checks if all nodes in the slice match their corresponding matchers. Returns true if the GoNodes collection is empty or all nodes match. For example, GoNodes{Ident("x"), Ident("y")}.MatchNodes(nodes) returns true if nodes contains exactly two identifiers "x" and "y" in that order.

Jump to

Keyboard shortcuts

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