structfields

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: MIT Imports: 6 Imported by: 0

README

Package structfields

Enumerate struct types and their fields from Go source files.

Go Reference

Notes

  • The intended use-case is generating docs for config structs.
  • Embedded struct fields are flattened into regular fields.
  • Embedded fields are not de-duplicated.
  • Fields with inline struct types are not well supported.

Documentation

Index

Constants

Needs is the packages.Mode with all the flags required to find fields.

Variables

This section is empty.

Functions

func FormatTypeExpr added in v0.0.3

func FormatTypeExpr(expr ast.Expr) string

FormatTypeExpr poorly formats a subset of possible type expressions. TODO: make this method less terrible.

func ResolvePackage

func ResolvePackage(pkg *packages.Package, file *ast.File, name string) (*packages.Package, bool)

ResolvePackage will resolve a package by its name. It's legal to pass a nil file, but then import aliases and duplicates are not handled.

func ResolveType

func ResolveType(pkg *packages.Package, name string) (*ast.StructType, *ast.File, bool)

ResolveType returns a struct type defined in the provided package along with the file it's declared in. The bool return value will be false if the name could not be resolved.

func ResolveTypeExpr added in v0.0.3

func ResolveTypeExpr(pkg *packages.Package, file *ast.File, expr ast.Expr) (*ast.StructType, *ast.File, bool)

ResolveTypeExpr returns the struct type referred to by the provided expression. A nil file may be passed, but package resolution will not be accurate. The boom return value will be false if the type expression could not be resolved to a struct type.

Types

type FieldType

type FieldType struct {
	Name    string
	Type    ast.Expr
	Doc     string
	Comment string
	Tag     string
}

FieldType contains information about a struct field.

func Fields

func Fields(pkg *packages.Package, file *ast.File, stype *ast.StructType) []*FieldType

Fields returns a list of the struct type's fields. A nil file may be passed, but this limits the ability to resolve embeded types.

type StructType

type StructType struct {
	Doc        string
	Name       string
	Directives []string
	Fields     []*FieldType
}

StructType contains details about a struct and it's fields.

func Load

func Load(dir string, patterns ...string) ([]*StructType, error)

Load finds all structs in the packages specified in the patterns. Embeded fields are treated the same as regular fields.

func Structs

func Structs(pkg *packages.Package) []*StructType

Structs finds all structs in the provided package. Embeded fields are treated the same as regular fields.

Jump to

Keyboard shortcuts

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