parse

package
v0.0.0-...-9edf09d Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2017 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package parse provides functions for parsing and tagging golang structs. It achieves this by creating an ast and visiting all of its nodes.

Index

Constants

View Source
const (
	// Append will append to tags rather than overwriting them altogether
	Append = iota
	// Overwrite will overwrite tags completely
	Overwrite
	// SkipExisting skips existing tags whether or not they match tag or case
	SkipExisting
)

Append modes

View Source
const (
	// TagAll will tag all structs/fields (unless they are excluded in the IngoreStructs/IgnoreFields slices)
	TagAll = iota
	// SkipSpecifiedStructs (currently unimplemented)
	SkipSpecifiedStructs
	// IncludeSpecifiedStructs (currently unimplemented)
	IncludeSpecifiedStructs
	// SkipStructAndFieldKeypairs (currently unimplemented)
	SkipStructAndFieldKeypairs
	// IncludeStructAndFieldKeypairs (currently unimplemented)
	IncludeStructAndFieldKeypairs
)

Major Tag modes

View Source
const (
	// JSON represents the json tag
	JSON = "json"
	// Snake represents snake case
	Snake = "snake"
	// Camel represents camel case
	Camel = "camel"
	// DefaultGenerateTag represents the default go generate tag that ST will respect
	DefaultGenerateTag = "@st"
)

Basic supported tags and cases

Variables

View Source
var (
	// Case determines the case to use when tagging structs - either Camel or Snake
	Case = DefaultCase
	// Tag determines the tag to use when tagging structs - default is json
	Tag = DefaultTag
	// FlagAppend is true if -a or -append are provided as command line flags - appends to tags instead of overwriting or skipping entirely
	FlagAppend bool
	// FlagOverwrite is true if -o or -overwrite are provided as command line flags - overwrites existing tags
	FlagOverwrite bool

	// Verbose sets the default for how much information is printed to standard out
	Verbose bool
	// Write is true if -w or -write are provided as command line flags - this will write to the original source file
	Write bool
	// IgnoredFieldsString is a comma separated list of ignored fields provided as a command line flag
	IgnoredFieldsString string
	// IgnoredStructsString is a comma separated list of ignored structs provided as a command line flag
	IgnoredStructsString string
	// AppendMode is the mode that ST will operate in. Default is to skip existing tags, can be set to Append or Overwrite
	AppendMode = SkipExisting
	// TagMode is the mode that ST operates on when tagging. Default is to tag all structs/fields.
	TagMode = TagAll
	// GoFile is the name of the GoFile as given by go generate to os.Environ ($GOFILE)
	GoFile string
)
View Source
var (
	// DefaultAppendMode is SkipExisting - will skip existing tags entirely
	DefaultAppendMode = SkipExisting
	// DefaultTagMode is TagAll - will tag all structs/fields unless they are already tagged or in the excluded slices
	DefaultTagMode = TagAll
	// DefaultTag is JSON
	DefaultTag = JSON
	// DefaultCase is Snake case. (common in http, sql, etc)
	DefaultCase = Snake

	// IgnoredFields contains strings for fields that are not to be tagged
	IgnoredFields = make([]string, 0)
	// IgnoredStructs contains strings for structs that are not to be tagged
	IgnoredStructs = make([]string, 0)
)

Defaults

Functions

func AndProcessFiles

func AndProcessFiles(paths []string) error

AndProcessFiles takes a list of paths, iterates over them, stats them, and then inspects source files

func AppendStructTag

func AppendStructTag(field *ast.Field, tagName string, offset *int, data []byte) []byte

AppendStructTag adds an additional tag to a struct tag

func CamelCase

func CamelCase(src string) string

CamelCase converts a string to the CamelCase version of it

func DeleteRange

func DeleteRange(data []byte, start, end int) []byte

DeleteRange deletes a range from a []byte, returning the new slice

func Flags

func Flags() error

Flags sets up command line bindings, calls flagParse(), and calls verify() to check command line flags

func FormatFieldName

func FormatFieldName(n string) string

FormatFieldName formats the field name as either CamelCase or snake_case

func Insert

func Insert(data, insertData []byte, start int) []byte

Insert inserts insertData into data at the given start index

func Inspect

func Inspect(f *ast.File, srcFileData []byte) ([]byte, error)

Inspect visits all nodes in the *ast.File (recursively), performing mutations on the buffer when the type found is an *ast.StructType

func IsIgnoredField

func IsIgnoredField(s string) bool

IsIgnoredField checks if a field is an explicitly ignored field Currently a slice is fine for performance, but we will replace these with maps later.

func IsIgnoredTypeName

func IsIgnoredTypeName(s string) bool

IsIgnoredTypeName checks if the name provided is an ignored struct

func OverwriteStructTag

func OverwriteStructTag(tag *ast.BasicLit, tagName string, offset *int, data []byte) []byte

OverwriteStructTag overwrites the struct tag completely

func Parse

func Parse(data []byte, filename string) (*ast.File, []byte, error)

Parse returns an *ast.File, the data parsed, and an error

func ProcessBytes

func ProcessBytes(data []byte, filename string) ([]byte, error)

ProcessBytes takes a []byte and filename, and inspects the data, returning that data in another []byte

func ProcessFile

func ProcessFile(path string) ([]byte, error)

ProcessFile processes a file, returning the processed []byte

func ResetFlags

func ResetFlags()

ResetFlags is a near copy of the flag.ResetForTesting(usage func()) function.

func SetArgs

func SetArgs(s []string)

SetArgs clears flags and sets os.Args to os.Args[0] (program name) and then to the list of whatever parameters are given after

func SetOptions

func SetOptions(o *Options)

SetOptions sets the current options to the options provided. (This is not thread safe if called from a goroutine)

func SetVars

func SetVars()

SetVars sets up all command line variable bindings

func TagStruct

func TagStruct(srcData []byte, s *ast.StructType, offset *int) []byte

TagStruct tags a struct based on whether or not it is exported, is ignored, and what flags are provided at runtime

func Underscore

func Underscore(str string) string

Underscore will change a string from a camelcased form to a string with underscores. Will change "::" to "/" to maintain compatibility with Rails's underscore

Types

type CommentDirective

type CommentDirective struct {
	BaseText string
	FlagSet  *flag.FlagSet
}

CommentDirective represents a comment with //@st at its beginning. I am really not a fan of treating comments as anything more than a comment, but Go unfortunately has no other constructs

func NewCommentDirective

func NewCommentDirective(s string) (*CommentDirective, error)

NewCommentDirective takes a string (which should be the text from an *ast.Comment), creates a new flag set using the comment as the flag set name with flag.ContinueOnError - flag.ExitOnError will call os.Exit() - and then parses the flags Comments: 1) I would pass in the *ast.Comment directly, but it is already initialized further up the call stack at this point

  1. There is some hackery of the flag package going on in here

s should be in the following format: - It should only be one line - It should say //@st with no spaces - Commands given after //@st will be interpreted just like normal st commands - $GOFILE will be passed in as the final argument

func (*CommentDirective) Args

func (c *CommentDirective) Args() []string

Args returns the underlying FlagSet.Args()

type File

type File struct {
	FileName string
	Data     []byte
}

File represents a basic file with a FileName(path) and the Data contained within the file

func Process

func Process(files []*File) ([]*File, error)

Process iterates over a []*File, processes the *Files, and returns the resulting []*File and the last error that occurred, if any This function could potentially consume a lot of memory if an extraordinarily large set was passed to it

type Options

type Options struct {
	//Tags       []string
	Tag         string
	Case        string
	AppendMode  int
	TagMode     int
	DryRun      bool
	Verbose     bool
	GenerateTag string
}

Options represents package behavior options - will be expanded to take a list of tags to support go generate

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns a new *Options with all default values initialized

Jump to

Keyboard shortcuts

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