astparser

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 11 Imported by: 4

README

astparser

License MIT GoDoc Go Report Card Build Status codecov

Simple golang structs ast parser

Get the package using:
$ go get -u -v github.com/mkorolyov/astparser
Usage

prepare loader config

cfg := astparser.Config{
	InputDir:"somedir",
	//ExcludeRegexp "easyjson",
	IncludeRegexp:"event_",
}

Then you can load structs via

files := astParser.Load(cfg)

files is a map[string]ParsedFile where key is a file name and value is a ParsedFile type with structs and constants.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(cfg Config) (map[string]ParsedFile, error)

Load parses files and return a map where key is a file name and value as a parsed file obj with golang structs definitions and constants

Types

type Config

type Config struct {
	InputDir      string
	ExcludeRegexp string
	IncludeRegexp string
}

type ConstantDef

type ConstantDef struct {
	Name  string
	Value string
}

ConstantDef describes defined constants

type FieldDef

type FieldDef struct {
	CompositionField bool
	// Could be empty for CompositionField
	FieldName string
	FieldType Type
	JsonName  string
	Nullable  bool
	Comments  []string
	AllTags   map[string]string
}

FieldDef described parsed go struct field.

type ParsedFile

type ParsedFile struct {
	Structs   []StructDef
	Constants []ConstantDef
	Package   string
}

type StructDef

type StructDef struct {
	Name     string
	Fields   []FieldDef
	Comments []string
}

StructDef describes parsed go struct.

type Tag

type Tag struct {
	JsonName  string
	Nullable  bool
	Omitempty bool
	AllTags   map[string]string
}

Tag contains parsed field tags.

type Type

type Type interface{}

Type represent parsed type.

type TypeArray

type TypeArray struct {
	InnerType Type
}

TypeArray indicates that type is golang array or slice. Inner type could be any type golang supports.

type TypeCustom

type TypeCustom struct {
	// type alias. like `type myCost string`
	// is true when we cant resolve alias type.
	Alias bool
	// contains the alias type
	AliasType Type
	Name      string
	Expr      ast.Expr
}

TypeCustomer indicates that type is a defined struct or type alias.

type TypeInterfaceValue added in v0.0.6

type TypeInterfaceValue struct{}

TypeInterfaceValue indicates that type is a interface{}

type TypeMap

type TypeMap struct {
	KeyType   Type
	ValueType Type
}

TypeMap indicates that type is golang map. Both keys and values could be any type golang supports.

type TypePointer

type TypePointer struct {
	InnerType Type
}

TypePointer indicates that type is a point with underlying any golang type

type TypeSimple

type TypeSimple struct {
	Name string
}

TypeSimple indicates that type is a primitive golang type like int or string.

type Walker

type Walker struct {
	Structs   []StructDef
	Constants []ConstantDef
	Package   string
}

TODO parse type comments Walker implements go/ast.Visitor to walk through golang structs and constants to parse them.

func (*Walker) Visit

func (w *Walker) Visit(node ast.Node) ast.Visitor

A Walkers's Visit method is invoked for each node encountered by go/ast.Walk. If the result visitor w is not nil, go/ast.Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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