Code Monkey home page Code Monkey logo

go_generics's Introduction

Generics Build Status

This project is bazel-free version of Google go_generics tool released with gVisor project.

It allows for generating code from template while still working with standard Go code. A great example of using it is ScyllaDB go-set package.

Installation

go get -u github.com/mmatczuk/go_generics/cmd/go_generics
go get -u github.com/mmatczuk/go_generics/cmd/go_merge

go_generics

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)
  1. 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() {}
  1. 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)"
  1. The package is renamed to the value via the -p argument.

  2. 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".

Unfortunately, go_generics does not handle anonymous fields with renamed types.

go_merge

go_merge merges multiple Go files into one, may be used in a pipeline before go_generics.

License

This project is distributed under the Apache 2.0 license. See the LICENSE file for details. It contains software from:

GitHub star is always appreciated!

go_generics's People

Contributors

amscanne avatar aoeu avatar fvoznika avatar hugelgupf avatar iangudger avatar mmatczuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.