aster

Easily get the golang syntax tree and modify the code.

  • Owner: andeya/aster
  • Platform:
  • License:: Apache License 2.0
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

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

Main metrics

Overview
Name With Ownerandeya/aster
Primary LanguageGo
Program languageGo (Language Count: 2)
Platform
License:Apache License 2.0
所有者活动
Created At2018-11-28 14:02:19
Pushed At2025-06-17 02:20:35
Last Commit At2025-06-17 10:20:29
Release Count19
Last Release Namev0.8.3 (Posted on )
First Release Namev0.1 (Posted on )
用户参与
Stargazers Count151
Watchers Count7
Fork Count11
Commits Count156
Has Issues Enabled
Issues Count1
Issue Open Count0
Pull Requests Count3
Pull Requests Open Count0
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private