codegen

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AvailableTransformStrategy = []string{
	"UPPER_SNAKE_CASE",
	"lower_snake_case",
	"UpperCamelCase",
	"lowerCamelCase",
}

Functions

func GetFiles

func GetFiles(srcPath string) ([]string, error)

GetFiles returns all the Golang files for the given path. Ignores hidden files.

func ToDelimitedString added in v0.2.0

func ToDelimitedString(str string, delimiter uint8, forceUpper bool) string

ToDelimitedString returns string separated with delimiter Inspired by https://github.com/iancoleman/strcase

@example

  • ToDelimitedString("ToDelimitedStringInSnakeCase", '.', true) -> DELIMIT.STRING.IN.SNAKE.CASE
  • ToDelimitedString("ToDelimitedStringInSnakeCase", '.', false) -> delimit.string.in.snake.case

func ToLowerSnakeCase added in v0.2.0

func ToLowerSnakeCase(s string) string

ToLowerSnakeCase converts a string to snake_case Inspired by https://github.com/iancoleman/strcase

func ToUpperSnakeCase added in v0.2.0

func ToUpperSnakeCase(s string) string

ToLowerSnakeCase converts a string to SNAKE_CASE Inspired by https://github.com/iancoleman/strcase

Types

type AllValuesCodeBlock added in v0.2.0

type AllValuesCodeBlock struct{}

func (AllValuesCodeBlock) Attrs added in v0.2.0

func (blk AllValuesCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (AllValuesCodeBlock) Build added in v0.2.0

func (blk AllValuesCodeBlock) Build(enum Enum) ([]byte, error)

func (AllValuesCodeBlock) Dependencies added in v0.2.0

func (blk AllValuesCodeBlock) Dependencies() []string

type CodeBlock

type CodeBlock interface {
	// Build builds the code block.
	Build(enum Enum) ([]byte, error)

	// Dependencies returns mandatory dependencies (packages) that need to be imported
	Dependencies() []string

	// Attrs return basic attributes of the code block
	Attrs(enum Enum) CodeBlockAttrs
}

CodeBlock is the interface to build the code block

func NewCodeBlocks

func NewCodeBlocks(cfg Config) []CodeBlock

type CodeBlockAttrs added in v0.2.0

type CodeBlockAttrs struct {
	Name string

	Type CodeBlockType

	// CollisionStrategy indicate what should we do if the code block is already exist
	CollisionStrategy CollisionStrategy
}

type CodeBlockType added in v0.2.0

type CodeBlockType int
const (
	CodeBlockTypeFunc CodeBlockType = iota
	CodeBlockTypeVar
)

type CodeGenerator

type CodeGenerator struct {

	// The strategy to transform name to various cases
	TransformStrategy TransformStrategy

	// Writer indicates how result should be written
	Writer Writer
	// contains filtered or unexported fields
}

func NewCodeGenerator

func NewCodeGenerator(opt Option) *CodeGenerator

func (*CodeGenerator) Generate

func (gen *CodeGenerator) Generate(srcPath string, files []string) error

Generate will parse the target go file and generate attributes based on demand

type CollisionStrategy added in v0.2.0

type CollisionStrategy int

How should the generator do if the code block is already exist

const (
	// Do nothing
	CollisionStrategyIgnore CollisionStrategy = iota

	// Relace entire code block with new one
	CollisionStrategyReplace
)

type Config

type Config struct {
	// Should print the result to console rather than pipe to a file
	DryRun bool

	// Should generate methods implementing json.Marshaler and json.Unmarshaler interface
	Json bool

	// Trim the value of enum name
	Trim string
}

type Enum

type Enum struct {
	Name   string
	Type   string
	Values []EnumValue
}

func NewEnum

func NewEnum(enum goparser.Enum, strategy TransformStrategy, trim string) (Enum, error)

NewEnum return enum for code generation. Will return error if theres is no 0 value.

func (Enum) AllValuesName

func (e Enum) AllValuesName() string

func (Enum) NameMapName

func (e Enum) NameMapName() string

func (Enum) ValueMapName

func (e Enum) ValueMapName() string

type EnumValue

type EnumValue struct {
	goparser.EnumValue

	TransformStrategy TransformStrategy
	Trim              string
}

EnumValue represent an enumerated type contains raw data in a golang file and logic of code generation.

func (EnumValue) IsZero

func (v EnumValue) IsZero() bool

func (EnumValue) String

func (v EnumValue) String() string

type FileWriter

type FileWriter struct{}

func (*FileWriter) Write

func (wr *FileWriter) Write(dest string, buf []byte) error

type JsonMarshalCodeBlock

type JsonMarshalCodeBlock struct{}

func (JsonMarshalCodeBlock) Attrs added in v0.2.0

func (blk JsonMarshalCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (JsonMarshalCodeBlock) Build

func (blk JsonMarshalCodeBlock) Build(enum Enum) ([]byte, error)

func (JsonMarshalCodeBlock) Dependencies added in v0.2.0

func (blk JsonMarshalCodeBlock) Dependencies() []string

type JsonUnmarshalCodeBlock added in v0.2.0

type JsonUnmarshalCodeBlock struct{}

func (JsonUnmarshalCodeBlock) Attrs added in v0.2.0

func (blk JsonUnmarshalCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (JsonUnmarshalCodeBlock) Build added in v0.2.0

func (blk JsonUnmarshalCodeBlock) Build(enum Enum) ([]byte, error)

func (JsonUnmarshalCodeBlock) Dependencies added in v0.2.0

func (blk JsonUnmarshalCodeBlock) Dependencies() []string

type LowerCamelCaseStrategy

type LowerCamelCaseStrategy struct{}

func (LowerCamelCaseStrategy) Run

func (s LowerCamelCaseStrategy) Run(input string) string

type LowerSnakeCaseStrategy

type LowerSnakeCaseStrategy struct{}

func (LowerSnakeCaseStrategy) Run

func (s LowerSnakeCaseStrategy) Run(input string) string

type NameMapCodeBlock added in v0.2.0

type NameMapCodeBlock struct{}

func (NameMapCodeBlock) Attrs added in v0.2.0

func (blk NameMapCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (NameMapCodeBlock) Build added in v0.2.0

func (blk NameMapCodeBlock) Build(enum Enum) ([]byte, error)

func (NameMapCodeBlock) Dependencies added in v0.2.0

func (blk NameMapCodeBlock) Dependencies() []string

type Option

type Option struct {
	CaseStyle string
	Trim      string
	DryRun    bool
	Json      bool
}

type ParserCodeBlock

type ParserCodeBlock struct{}

func (ParserCodeBlock) Attrs added in v0.2.0

func (blk ParserCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (ParserCodeBlock) Build

func (blk ParserCodeBlock) Build(enum Enum) ([]byte, error)

func (ParserCodeBlock) Dependencies added in v0.2.0

func (blk ParserCodeBlock) Dependencies() []string

type StdioWriter

type StdioWriter struct{}

func (*StdioWriter) Write

func (wr *StdioWriter) Write(dest string, buf []byte) error

type StringCodeBlock

type StringCodeBlock struct{}

func (StringCodeBlock) Attrs added in v0.2.0

func (blk StringCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (StringCodeBlock) Build

func (blk StringCodeBlock) Build(enum Enum) ([]byte, error)

func (StringCodeBlock) Dependencies added in v0.2.0

func (blk StringCodeBlock) Dependencies() []string

type TestWriter

type TestWriter struct {
	Result []byte
}

TestWriter do nothing just keep a copy in the struct

func (*TestWriter) Write

func (wr *TestWriter) Write(dest string, buf []byte) error

type TransformStrategy

type TransformStrategy interface {
	Run(string) string
}

The strategy to transform name to various cases

func NewTransformStrategy

func NewTransformStrategy(s string) TransformStrategy

type UpperCamelCaseStrategy

type UpperCamelCaseStrategy struct{}

func (UpperCamelCaseStrategy) Run

func (s UpperCamelCaseStrategy) Run(input string) string

type UpperSnakeCaseStrategy

type UpperSnakeCaseStrategy struct{}

func (UpperSnakeCaseStrategy) Run

func (s UpperSnakeCaseStrategy) Run(input string) string

type ValueMapCodeBlock

type ValueMapCodeBlock struct{}

func (ValueMapCodeBlock) Attrs added in v0.2.0

func (blk ValueMapCodeBlock) Attrs(enum Enum) CodeBlockAttrs

func (ValueMapCodeBlock) Build

func (blk ValueMapCodeBlock) Build(enum Enum) ([]byte, error)

func (ValueMapCodeBlock) Dependencies added in v0.2.0

func (blk ValueMapCodeBlock) Dependencies() []string

type Writer

type Writer interface {
	Write(dest string, buf []byte) error
}

Writer indicates how result should be written

Jump to

Keyboard shortcuts

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