optiongen

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 9 Imported by: 0

README

option-gen

a option gen tool for convenience

use it in Promgram

go test -v -run TestExecuteAny github.com/hauntedness/optiongen
=== RUN   TestExecuteAny
=== RUN   TestExecuteAny/github.com/hauntedness/optiongen/internal

type CallOption func(*callOptions)
func (op *callOptions) ApplyOption(opts ...CallOption) {
        for i := range opts {
                opts[i](op)
        }
}
var WithIntField = func(intField int) CallOption {
        return func(op *callOptions) {
                op.intField = intField
        }
}
var WithStringField = func(stringField string) CallOption {
        return func(op *callOptions) {
                op.stringField = stringField
        }
}
var WithInterfaceField = func(interfaceField interface{}) CallOption {
        return func(op *callOptions) {
                op.interfaceField = interfaceField
        }
}
var WithWriter = func(writer io.Writer) CallOption {
        return func(op *callOptions) {
                op.writer = writer
        }
}
--- PASS: TestExecuteAny (0.76s)
    --- PASS: TestExecuteAny/github.com/hauntedness/optiongen/internal (0.76s)
PASS
ok      github.com/hauntedness/option-gen       1.376s

Command line

go run github.com/hauntedness/optiongen/cmd/optiongen -o callOptions -p github.com/hauntedness/optiongen/internal

type CallOption func(*callOptions)
func (op *callOptions) ApplyOption(opts ...CallOption) {
        for i := range opts {
                opts[i](op)
        }
}
var WithIntField = func(intField int) CallOption {
        return func(op *callOptions) {
                op.intField = intField
        }
}
var WithStringField = func(stringField string) CallOption {
        return func(op *callOptions) {
                op.stringField = stringField
        }
}
var WithInterfaceField = func(interfaceField interface{}) CallOption {
        return func(op *callOptions) {
                op.interfaceField = interfaceField
        }
}
var WithWriter = func(writer io.Writer) CallOption {
        return func(op *callOptions) {
                op.writer = writer
        }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WithPostfix = func(postfix string) func(o *option) {
	return func(o *option) {
		o.postfix = postfix
	}
}

Functions

func ExecuteAny

func ExecuteAny(value any, args ...Option) string

func ExecuteString

func ExecuteString(typeName string, packagePath string, args ...Option) string

func ExecuteType

func ExecuteType(typ reflect.Type, args ...Option) string

func OptionTypeName

func OptionTypeName(applyeeName string) string

OptionTypeName give the option type a proper name replace xxxOptions --> XXXOption replace xxxConfig --> XXXOption replace xxxConfigs --> XXXOption

func OptionVarName

func OptionVarName(fieldName, postfix string) string

OptionVarName give the option variable a proper name

func TodoImports

func TodoImports(pkg *packages.Package)

TODO: currently, the implementation doesn't understand package alias implement this function in future need

Types

type Field

type Field struct {
	FieldName string
	FieldType string
}

type Gen

type Gen struct {
	TypeName    string
	Fields      []Field
	Index       int
	WithPostfix string
}

Gen is a wide type which will be rendered with a bunch of templates

func LoadDefinition

func LoadDefinition(packagePath string, typeName string, conf *packages.Config) (g Gen, err error)

func (Gen) FieldNameByIndex

func (g Gen) FieldNameByIndex() string

func (Gen) FieldTypeByIndex

func (g Gen) FieldTypeByIndex() string

func (Gen) ParamNameByIndex added in v1.0.2

func (g Gen) ParamNameByIndex() string

func (Gen) Render

func (g Gen) Render(tmpl string) string

func (Gen) RenderApplyFunc

func (g Gen) RenderApplyFunc() string

func (Gen) RenderOptionType

func (g Gen) RenderOptionType() string

func (Gen) RenderOptionVariable

func (g Gen) RenderOptionVariable() string

type Option

type Option func(o *option)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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