go_generics

Templates, generics engine for Go

  • 所有者: mmatczuk/go_generics
  • 平台:
  • 許可證: Apache License 2.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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!

主要指標

概覽
名稱與所有者mmatczuk/go_generics
主編程語言Go
編程語言Makefile (語言數: 3)
平台
許可證Apache License 2.0
所有者活动
創建於2018-08-08 10:17:39
推送於2018-12-12 14:37:52
最后一次提交2018-12-12 15:30:37
發布數0
用户参与
星數136
關注者數3
派生數5
提交數11
已啟用問題?
問題數0
打開的問題數0
拉請求數1
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?