aster

Easily get the golang syntax tree and modify the code.

  • 所有者: andeya/aster
  • 平台:
  • 许可证: Apache License 2.0
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Aster Build 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

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

主要指标

概览
名称与所有者andeya/aster
主编程语言Go
编程语言Go (语言数: 2)
平台
许可证Apache License 2.0
所有者活动
创建于2018-11-28 14:02:19
推送于2025-06-17 02:20:35
最后一次提交2025-06-17 10:20:29
发布数19
最新版本名称v0.8.3 (发布于 )
第一版名称v0.1 (发布于 )
用户参与
星数151
关注者数7
派生数11
提交数156
已启用问题?
问题数1
打开的问题数0
拉请求数3
打开的拉请求数0
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?