csgen

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 14 Imported by: 4

README

       
     

CSGen

CSGen is a Golang utility package for simplifying development of code generation tools. It contains abstractions around the standard library AST packages to make using them more intuitive for developers.

Get the Package

go get github.com/cscoding21/csgen

Core Uses

The primary use-case for the library is to get a list of all the structs in a file including details about each field.

import (
    "fmt"
    "github.com/cscoding21/csgen"
)

//---get all of the structs within a file.
structs, err := csgen.GetStructs("test_struct.go")

if err != nil {
    panic("error loading file")
}

for _, s := range structs {
    fmt.Println(s.Name)
    for _, f := range s.Fields {
        fmt.Println("  -- ", f.Name, f.Type)
    }
} 

The struct object contains information about the struct's definition as well as all fields contained within.

Struct Properties

Property Type Description
Name string The name of the struct
Type string The type of the struct
FilePath string The path to the file containing the struct
Package string The package the struct is contained within
Fields []Field The list of fields contained within the struct

Additionally, a struct has a GetField(name string) method that will return an individual field object by its name.

Field Properties

Property Type Description
Name string The name of the field
Type string The type of the field
TagString string The tag string for the field
IsPrimitive bool True if the field is a primitive type
IsPointer bool True if the field is defined as a pointer to value
IsSlice bool True if the field is a slice
IsPublic bool True if the field is available outside the package

Additionally, a field has a GetTag(name string) method. This method can be used to extract the value of an individual tag from the tag string. For example, consider the tag string json:"email" csval:"req,email".

s.GetTag("json")    //---returns "email"
s.GetTag("caval")   //---returns "req,email"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteTemplate

func ExecuteTemplate[T any](name string, fileTemplate string, om T) string

ExecuteTemplate executes a template against a given object and return the output as a string

func GetFile

func GetFile(file ...string) string

GetFile returns the current file based on the generators env or passed in value

func GetImports

func GetImports(filePath string) ([]string, error)

GetImports returns all of the imports in a given file

func NewCSGenBuilderForFile

func NewCSGenBuilderForFile(name string, pkg string) *strings.Builder

NewCSGenBuilderForFile returns a string buider with a common header for generated files

func ProfileNode

func ProfileNode(node ast.Node)

ProfileNode get details about an unknown node based on its actual type

func WriteGeneratedGoFile

func WriteGeneratedGoFile(name string, contents string) error

WriteGeneratedGoFile create a text file with the passed in name and contents

Types

type Field

type Field struct {
	Name        string
	Type        string
	TagString   string
	IsPrimitive bool
	IsPointer   bool
	IsSlice     bool
	IsPublic    bool
}

Field a struct that represents a single field within a struct abstraction

func GetVariables

func GetVariables(filePath string) ([]Field, error)

GetVariables returns a list of all variable definitions in a given file

func (*Field) GetTag

func (s *Field) GetTag(name string) string

GetTag returns a single tag value by name based on the standard format rules

type Function

type Function struct {
	Name      string
	Receiver  *string
	Arguments []Field
	Returns   []Field
	IsPublic  bool
}

Function a struct that represents a single function abstraction

func GetFunctions

func GetFunctions(filePath string) ([]Function, error)

GetFunctions returns all of the functions in a given file

type Interface

type Interface struct {
	Name     string
	Methods  []Function
	IsPublic bool
}

Interface a struct that represents a single interface abstraction

func GetInterfaces

func GetInterfaces(filePath string) ([]Interface, error)

GetInterfaces get a list of all declared interfaces in a given file

type Struct

type Struct struct {
	Name     string
	FilePath string
	Package  string
	Type     string
	Fields   []Field
}

Struct a struct that abstracts a golang struct

func GetStructs

func GetStructs(filePath string) ([]Struct, error)

GetStructs return a list of all structs in a given file

func (*Struct) GetField

func (s *Struct) GetField(name string) *Field

GetField return a field object of a struct by its name

Jump to

Keyboard shortcuts

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