toast

package module
v0.0.0-...-af91a63 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: MIT Imports: 10 Imported by: 0

README

ToAST

ToAST is a thin wrapper for the Go standard library ast package that provides a simple interface for working with Go types.

This package only supports parsing of type definitions. It was originally intended to be used for generating type definitions for other languages -- particularly CUE, a typesafe superset of JSON.

Types

Each go/ast type specification maps to one of four structs:

  • PlainType: Basic types, type aliases, and pointers
  • ArrayType: Array and slice types
  • MapType: Maps
  • StructType: Structs, which may contain fields that are themselves one of the four types.

There is also partial support for an EnumType, which is expressed by convention in Go as a type declaration with a group of constants of the same type.

Transforms

When parsing a file, ToAST can apply a number of transformations on matching objects:

  • ExcludeImport excludes specific imports
  • ModifyImport mutates specific imports
  • ExcludeType excludes specific types
  • ModifyType mutates specific types
  • ExcludeField excludes specific fields in a StructType
  • ModifyField mutates specific fields in a StructType
  • CopyIntoStruct copies fields from one or more named StructTypes into a target StructType, replacing the field at of a given name
  • PromoteToEnumType converts a PlainType into an EnumType
  • GenFieldTransform takes a StructType and a Field and returns some Transform that can be matched on subsequent nodes in the file
  • GenEnumTypeTransform takes a string and go/ast.ValueSpec and returns a PromoteToEnumType transform that can be matched on a PlainType in the file

Usage

First, load an *ast.File. For example:

import "go/parser"

filePath := "path/to/file.go"
astFile, err := parser.ParseFile(token.NewFileSet(), filePath, nil, parser.ParseComments)
if err != nil {
	panic(err)
}

Then create a *toast.File:

file := toast.NewFile(astFile,
  WithTransform(&toast.ExcludeImport{
    Match: func(i Import) bool {
      return i.Name == "foo"
    }
  }),
  WithTransform(&toast.ExcludeType{
    Match: func(t Type) bool {
      return t.Name == "bar"
    }
  }),
  ...
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DocsFromCommentGroup

func DocsFromCommentGroup(cg *ast.CommentGroup) string

Types

type ArrayType

type ArrayType struct {
	Name   string `json:"name"`
	Type   string `json:"type"`
	Length int    `json:"length,omitempty"`
	Docs   string `json:"-"`
}

func ArrayTypeFromSpec

func ArrayTypeFromSpec(name, docs string, a *ast.ArrayType) *ArrayType

func (*ArrayType) CUE

func (a *ArrayType) CUE() string

func (*ArrayType) GetDocs

func (a *ArrayType) GetDocs() string

func (*ArrayType) GetName

func (a *ArrayType) GetName() string

func (*ArrayType) GetTypeNames

func (a *ArrayType) GetTypeNames() []string

func (*ArrayType) Go

func (a *ArrayType) Go() string

func (*ArrayType) Reflect

func (a *ArrayType) Reflect() json.RawMessage

func (*ArrayType) SetTypeNames

func (a *ArrayType) SetTypeNames(tt []string)

type CopyIntoStruct

type CopyIntoStruct struct {
	StructName     string
	FieldToReplace string
	FromStructs    map[string]struct{}
	// contains filtered or unexported fields
}

type EnumType

type EnumType struct {
	Name   string   `json:"name"`
	Values []string `json:"values"`
	Docs   string   `json:"-"`
}

func (*EnumType) CUE

func (et *EnumType) CUE() string

func (*EnumType) GetDocs

func (et *EnumType) GetDocs() string

func (*EnumType) GetName

func (et *EnumType) GetName() string

func (*EnumType) GetTypeNames

func (et *EnumType) GetTypeNames() []string

func (*EnumType) Go

func (et *EnumType) Go() string

func (*EnumType) Reflect

func (et *EnumType) Reflect() json.RawMessage

func (*EnumType) SetTypeNames

func (et *EnumType) SetTypeNames(typs []string)

type ExcludeField

type ExcludeField struct {
	Match func(*Field) bool
}

type ExcludeImport

type ExcludeImport struct {
	Match func(Import) bool
}

type ExcludeType

type ExcludeType struct {
	Match func(Type) bool
}

type Field

type Field struct {
	Type
	Tags *structtag.Tags
}

func FieldFromSpec

func FieldFromSpec(f *ast.Field) (*Field, error)

func (*Field) CUE

func (f *Field) CUE() string

func (*Field) Go

func (f *Field) Go() string

func (*Field) Reflect

func (f *Field) Reflect() json.RawMessage

type File

type File struct {
	Imports map[string]Import
	Code    []Type
	// contains filtered or unexported fields
}

func NewFile

func NewFile(file *ast.File, opts ...Option) (*File, error)

func (*File) CUE

func (f *File) CUE() string

func (*File) Go

func (f *File) Go() string

func (*File) Reflect

func (f *File) Reflect() json.RawMessage

type GenEnumTypeTransform

type GenEnumTypeTransform struct {
	Generate func(string, *ast.ValueSpec) *PromoteToEnumType
}

type GenFieldTransform

type GenFieldTransform struct {
	Generate func(*StructType, *Field) Transform
}

type Import

type Import struct {
	Name string `json:"name,omitempty"`
	Path string `json:"path"`
	// contains filtered or unexported fields
}

func ImportFromSpec

func ImportFromSpec(spec *ast.ImportSpec) Import

func (*Import) CUE

func (i *Import) CUE() string

func (*Import) GetOldPath

func (i *Import) GetOldPath() string

func (*Import) Go

func (i *Import) Go() string

func (*Import) Reflect

func (i *Import) Reflect() json.RawMessage

type MapType

type MapType struct {
	Name      string `json:"name"`
	KeyType   string `json:"key_type"`
	ValueType string `json:"value_type"`
	Docs      string `json:"-"`
}

func MapTypeFromSpec

func MapTypeFromSpec(name, docs string, m *ast.MapType) *MapType

func (*MapType) CUE

func (m *MapType) CUE() string

func (*MapType) GetDocs

func (m *MapType) GetDocs() string

func (*MapType) GetName

func (m *MapType) GetName() string

func (*MapType) GetTypeNames

func (m *MapType) GetTypeNames() []string

func (*MapType) Go

func (m *MapType) Go() string

func (*MapType) Reflect

func (m *MapType) Reflect() json.RawMessage

func (*MapType) SetTypeNames

func (m *MapType) SetTypeNames(tt []string)

type ModifyField

type ModifyField struct {
	Apply func(*Field) *Field
}

type ModifyImport

type ModifyImport struct {
	Apply func(Import) Import
}

type ModifyType

type ModifyType struct {
	Apply func(Type) Type
}

type Node

type Node interface {
	// Renders JSON representation of the node.
	Reflect() json.RawMessage
	// Renders Go type definitions.
	Go() string
	// Renders CUE definitions.
	CUE() string
}

type Option

type Option func(*File)

func WithCUEPackageName

func WithCUEPackageName(packageName string) Option

func WithPackageName

func WithPackageName(packageName string) Option

func WithTransform

func WithTransform(t Transform) Option

type PlainType

type PlainType struct {
	Name string `json:"name"`
	Type string `json:"type"`
	Docs string `json:"-"`
}

func PlainTypeFromIdent

func PlainTypeFromIdent(name, docs string, i *ast.Ident) *PlainType

func PlainTypeFromSelectorExpr

func PlainTypeFromSelectorExpr(name, docs string, s *ast.SelectorExpr) *PlainType

func PlainTypeFromStarExpr

func PlainTypeFromStarExpr(name, docs string, star *ast.StarExpr) *PlainType

func (*PlainType) CUE

func (p *PlainType) CUE() string

func (*PlainType) GetDocs

func (p *PlainType) GetDocs() string

func (*PlainType) GetName

func (p *PlainType) GetName() string

func (*PlainType) GetTypeNames

func (p *PlainType) GetTypeNames() []string

func (*PlainType) Go

func (p *PlainType) Go() string

func (*PlainType) Reflect

func (p *PlainType) Reflect() json.RawMessage

func (*PlainType) SetTypeNames

func (p *PlainType) SetTypeNames(tt []string)

type PromoteToEnumType

type PromoteToEnumType struct {
	Apply func(*PlainType) *EnumType
}

type StructType

type StructType struct {
	Name   string   `json:"name"`
	Fields []*Field `json:"fields"`
	Docs   string   `json:"-"`
}

func StructTypeFromSpec

func StructTypeFromSpec(name, docs string, s *ast.StructType) (*StructType, error)

func (*StructType) CUE

func (s *StructType) CUE() string

func (*StructType) GetDocs

func (s *StructType) GetDocs() string

func (*StructType) GetName

func (s *StructType) GetName() string

func (*StructType) GetTypeNames

func (s *StructType) GetTypeNames() []string

func (*StructType) Go

func (s *StructType) Go() string

func (*StructType) Reflect

func (s *StructType) Reflect() json.RawMessage

func (*StructType) SetTypeNames

func (s *StructType) SetTypeNames(tt []string)

type Transform

type Transform interface {
	// contains filtered or unexported methods
}

type Type

type Type interface {
	Node
	GetName() string
	GetTypeNames() []string
	SetTypeNames([]string)
	GetDocs() string
}

func ParseExpr

func ParseExpr(names []*ast.Ident, docs string, expr ast.Expr) (Type, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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