optiongen

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 16 Imported by: 0

README

optiongen

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
package internal

import "io"

type CallOption func(*callOptions)

func (op *callOptions) ApplyOptions(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.83s)
    --- PASS: TestExecuteAny/github.com/hauntedness/optiongen/internal (0.83s)
PASS
ok      github.com/hauntedness/optiongen        1.468s

Command line

go run github.com/hauntedness/optiongen/cmd/optiongen --type callOptions -package github.com/hauntedness/optiongen/internal

package internal

import "io"

type CallOption func(*callOptions)

func (op *callOptions) ApplyOptions(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 WithAutoImports = func(autoImports bool) func(o *option) {
	return func(o *option) {
		o.autoImports = autoImports
	}
}
View Source
var WithPostfix = func(postfix string) func(o *option) {
	return func(o *option) {
		o.postfix = postfix
	}
}
View Source
var WithPrefix = func(prefix string) func(o *option) {
	return func(o *option) {
		o.prefix = prefix
	}
}
View Source
var WithWriteFile = func(writeFile string) func(o *option) {
	return func(o *option) {
		o.writeFile = writeFile
	}
}

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 Files added in v1.1.1

func Files(pkg *packages.Package) files

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

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, prefix, postfix string) string

OptionVarName give the option variable a proper name

Types

type Field

type Field struct {
	FieldName string
	FieldType string
}

type Gen

type Gen struct {
	TypeName    string
	Fields      []Field
	Index       int
	WithPrefix  string
	WithPostfix string
	PackageName 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) ParamTypeByIndex added in v1.2.0

func (g Gen) ParamTypeByIndex() 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