documentation

package module
v0.7.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2022 License: Apache-2.0 Imports: 0 Imported by: 0

README

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

Documentation

Overview

Package aster can easily get the golang syntax tree and modify the code.

Copyright 2018 henrylee2cn. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Directories

Path Synopsis
_examples
Package aster can easily get the golang syntax tree and modify the code.
Package aster can easily get the golang syntax tree and modify the code.
internal
astutil
Package astutil contains common utilities for working with the Go AST.
Package astutil contains common utilities for working with the Go AST.
buildutil
Package buildutil provides utilities related to the go/build package in the standard library.
Package buildutil provides utilities related to the go/build package in the standard library.
cgo
fastwalk
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
gcexportdata
Package gcexportdata provides functions for locating, reading, and writing export data files containing type information produced by the gc compiler.
Package gcexportdata provides functions for locating, reading, and writing export data files containing type information produced by the gc compiler.
gcimporter
Package gcimporter provides various functions for reading gc-generated object files that can be used to implement the Importer interface defined by the Go 1.5 standard library package.
Package gcimporter provides various functions for reading gc-generated object files that can be used to implement the Importer interface defined by the Go 1.5 standard library package.
gopathwalk
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
imports
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
loader
Package loader loads a complete Go program from source code, parsing and type-checking the initial packages plus their transitive closure of dependencies.
Package loader loads a complete Go program from source code, parsing and type-checking the initial packages plus their transitive closure of dependencies.
module
Package module defines the module.Version type along with support code.
Package module defines the module.Version type along with support code.
packages
Package packages loads Go packages for inspection and analysis.
Package packages loads Go packages for inspection and analysis.
packagesdriver
Package packagesdriver fetches type sizes for go/packages and go/analysis.
Package packagesdriver fetches type sizes for go/packages and go/analysis.
semver
Package semver implements comparison of semantic version strings.
Package semver implements comparison of semantic version strings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL