Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/greymatter-io/toast

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/greymatter-io/toast

  • v0.0.0-20220302155638-a68a874c9375
  • Source
  • Go
  • Socket score

Version published
Created
Source

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"
    }
  }),
  ...
)

FAQs

Package last updated on 02 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc