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
- Variables
- func AndProcessFiles(paths []string) error
- func AppendStructTag(field *ast.Field, tagName string, offset *int, data []byte) []byte
- func CamelCase(src string) string
- func DeleteRange(data []byte, start, end int) []byte
- func Flags() error
- func FormatFieldName(n string) string
- func Insert(data, insertData []byte, start int) []byte
- func Inspect(f *ast.File, srcFileData []byte) ([]byte, error)
- func IsIgnoredField(s string) bool
- func IsIgnoredTypeName(s string) bool
- func OverwriteStructTag(tag *ast.BasicLit, tagName string, offset *int, data []byte) []byte
- func Parse(data []byte, filename string) (*ast.File, []byte, error)
- func ProcessBytes(data []byte, filename string) ([]byte, error)
- func ProcessFile(path string) ([]byte, error)
- func ResetFlags()
- func SetArgs(s []string)
- func SetOptions(o *Options)
- func SetVars()
- func TagStruct(srcData []byte, s *ast.StructType, offset *int) []byte
- func Underscore(str string) string
- type CommentDirective
- type File
- type Options
Constants ¶
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
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
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 ¶
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 )
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 ¶
AndProcessFiles takes a list of paths, iterates over them, stats them, and then inspects source files
func AppendStructTag ¶
AppendStructTag adds an additional tag to a struct tag
func DeleteRange ¶
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 ¶
FormatFieldName formats the field name as either CamelCase or snake_case
func Inspect ¶
Inspect visits all nodes in the *ast.File (recursively), performing mutations on the buffer when the type found is an *ast.StructType
func IsIgnoredField ¶
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 ¶
IsIgnoredTypeName checks if the name provided is an ignored struct
func OverwriteStructTag ¶
OverwriteStructTag overwrites the struct tag completely
func ProcessBytes ¶
ProcessBytes takes a []byte and filename, and inspects the data, returning that data in another []byte
func ProcessFile ¶
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 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 ¶
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 ¶
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
- 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 ¶
File represents a basic file with a FileName(path) and the Data contained within the file
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