Need help with aster?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

henrylee2cn
144 Stars 10 Forks Apache License 2.0 148 Commits 0 Opened issues

Description

Easily get the golang syntax tree and modify the code.

Services available

!
?

Need anything else?

Contributors list

# 4,392
Go
spider
golang
slack
140 commits
# 2,865
rust-la...
golang
wechat
bilibil...
2 commits

Aster Build Status <!-- Coverage Status --> Report Card GitHub release GoDoc

Easily get the golang syntax tree and modify the code.

中文版

Feature

  • Convert the AST to
    reflect.Type
    -like types(Kind-Flags), as it would at runtime
  • Collect and package common syntax node types
  • Provides easy-to-use traversal syntax node functionality
  • Easily fetch and modify syntax node information
  • Formatted output modified code
  • Simpler, more natural way of metaprogramming
  • ...

Go Version

  • ≥go1.11

An Example

  • Set struct tag
package main

import ( "flag" "fmt"

"github.com/henrylee2cn/aster/aster"
"github.com/henrylee2cn/goutil"

)

var ( filename = flag.String("filename", "out/eg.structtag.go", "file name") src = flag.String("src", package test type S struct { Apple string BananaPeel,car,OrangeWater string E int } func F(){ type M struct { N int lowerCase string } } , "code text") )

func setStructTag(fa aster.Facade) bool { if fa.TypKind() != aster.Struct { return true } for i := fa.NumFields() - 1; i >= 0; i-- { field := fa.Field(i) if !field.Exported() { continue } field.Tags().Set(&aster.Tag{ Key: "json", Name: goutil.SnakeString(field.Name()), Options: []string{"omitempty"}, }) } return true }

func main() { flag.Parse() prog, _ := aster.LoadFile(*filename, *src) prog.Inspect(setStructTag) _ = prog.Rewrite() }

  • The output of the above program is:
package test

type S struct { Apple string json:"apple,omitempty" BananaPeel string json:"banana_peel,omitempty" car string OrangeWater string json:"orange_water,omitempty" E int json:"e,omitempty" }

func F() { type M struct { N int json:"n,omitempty" lowerCase string } }

Task List

  • [x] Basic
  • [x] Array
  • [x] Slice
  • [x] Struct
  • [x] Pointer
  • [x] Tuple
  • [x] Signature // non-builtin function or method
  • [x] Interface
  • [x] Map
  • [x] Chan

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.