go_generics

command
v0.0.0-202406181927 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: Apache-2.0, MIT Imports: 14 Imported by: 0

Documentation

Overview

go_generics reads a Go source file and writes a new version of that file with a few transformations applied to each. Namely:

  1. Global types can be explicitly renamed with the -t option. For example, if -t=A=B is passed in, all references to A will be replaced with references to B; a function declaration like:

    func f(arg *A)

    would be renamed to:

    func f(arg *B)

  2. Global type definitions and their method sets will be removed when they're being renamed with -t. For example, if -t=A=B is passed in, the following definition and methods that existed in the input file wouldn't exist at all in the output file:

    type A struct{}

    func (*A) f() {}

  3. All global types, variables, constants and functions (not methods) are prefixed and suffixed based on the option -prefix and -suffix arguments. For example, if -suffix=A is passed in, the following globals:

    func f() type t struct{}

    would be renamed to:

    func fA() type tA struct{}

    Some special tags are also modified. For example:

    "state:.(t)"

    would become:

    "state:.(tA)"

4. The package is renamed to the value via the -p argument. 5. Value of constants can be modified with -c argument.

Note that not just the top-level declarations are renamed, all references to them are also properly renamed as well, taking into account visibility rules and shadowing. For example, if -suffix=A is passed in, the following:

var b = 100

func f() {
	g(b)
	b := 0
	g(b)
}

Would be replaced with:

var bA = 100

func f() {
	g(bA)
	b := 0
	g(b)
}

Note that the second call to g() kept "b" as an argument because it refers to the local variable "b".

Note that go_generics can handle anonymous fields with renamed types if -anon is passed in, however it does not perform strict checking on parameter types that share the same name as the global type and therefore will rename them as well.

You can see an example in the tools/go_generics/generics_tests/interface test.

Directories

Path Synopsis
Package globals provides an AST visitor that calls the visit function for all global identifiers.
Package globals provides an AST visitor that calls the visit function for all global identifiers.
tests

Jump to

Keyboard shortcuts

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