api

package
v0.3.32 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TagName     = "pflag"
	JSONTagName = "json"
)

Variables

View Source
var DefaultTestType = &TestType{
	StringValue: "Welcome to defaults",
}

Functions

func GenerateCodeFile

func GenerateCodeFile(buffer *bytes.Buffer, info TypeInfo) error

func GenerateTestFile

func GenerateTestFile(buffer *bytes.Buffer, info TypeInfo) error

Types

type ComplexJSONType

type ComplexJSONType struct {
	StringValue string `json:"str"`
	IntValue    int    `json:"i"`
}

func (*ComplexJSONType) UnmarshalJSON

func (c *ComplexJSONType) UnmarshalJSON(b []byte) error

type FieldInfo

type FieldInfo struct {
	Name              string
	GoName            string
	Typ               types.Type
	DefaultValue      string
	UsageString       string
	FlagMethodName    string
	TestValue         string
	TestStrategy      TestStrategy
	ShouldBindDefault bool
	ShouldTestDefault bool
}

type NestedType

type NestedType struct {
	IntValue int `json:"i" pflag:",this is an important flag"`
}

type PFlagProvider

type PFlagProvider struct {
	// contains filtered or unexported fields
}

func (PFlagProvider) Imports

func (p PFlagProvider) Imports() map[string]string

Adds any needed imports for types not directly declared in this package.

func (PFlagProvider) WriteCodeFile

func (p PFlagProvider) WriteCodeFile(outputFilePath string) error

Evaluates the main code file template and writes the output to outputFilePath

func (PFlagProvider) WriteTestFile

func (p PFlagProvider) WriteTestFile(outputFilePath string) error

Evaluates the test code file template and writes the output to outputFilePath

type PFlagProviderGenerator

type PFlagProviderGenerator struct {
	// contains filtered or unexported fields
}

PFlagProviderGenerator parses and generates GetPFlagSet implementation to add PFlags for a given struct's fields.

func NewGenerator

func NewGenerator(pkg, targetTypeName, defaultVariableName string, shouldBindDefaultVar bool) (*PFlagProviderGenerator, error)

NewGenerator initializes a PFlagProviderGenerator for pflags files for targetTypeName struct under pkg. If pkg is not filled in, it's assumed to be current package (which is expected to be the common use case when invoking pflags from go:generate comments)

func (PFlagProviderGenerator) Generate

func (PFlagProviderGenerator) GetTargetPackage

func (g PFlagProviderGenerator) GetTargetPackage() *types.Package

type SliceOrArray

type SliceOrArray interface {
	Elem() types.Type
}

type Tag

type Tag struct {
	Name         string
	DefaultValue string
	Usage        string
}

Represents parsed PFlag Go-struct tag.

type Foo struct {
    StringValue      string            `json:"str" pflag:"\"hello world\",This is a string value"`
}

Name will be "str", Default value is "hello world" and Usage is "This is a string value"

func ParseTag

func ParseTag(tag string) (t Tag, err error)

Parses tag. Name is computed from json tag, defaultvalue is the name of the pflag tag and usage is the concatenation of all options for pflag tag. e.g. `json:"name" pflag:"2,this is a useful param"`

type TestStrategy

type TestStrategy string

Determines how tests should be generated.

const (
	JSON        TestStrategy = "Json"
	SliceJoined TestStrategy = "SliceJoined"
	Raw         TestStrategy = "Raw"
)

type TestType

type TestType struct {
	StringValue      string            `json:"str" pflag:"\"hello world\",\"life is short\""`
	BoolValue        bool              `json:"bl" pflag:"true"`
	NestedType       NestedType        `json:"nested"`
	IntArray         []int             `json:"ints" pflag:"[]int{12%2C1}"`
	StringArray      []string          `json:"strs" pflag:"[]string{\"12\"%2C\"1\"}"`
	ComplexJSONArray []ComplexJSONType `json:"complexArr"`
	StringToJSON     ComplexJSONType   `json:"c" pflag:",I'm a complex type but can be converted from string."`
	IgnoredMap       map[string]string `json:"ignored-map" pflag:"-,"`
	StorageConfig    storage.Config    `json:"storage"`
	IntValue         *int              `json:"i"`
	StringMap        map[string]string `json:"m" pflag:",I'm a map of strings"`
}

type TypeInfo

type TypeInfo struct {
	Timestamp time.Time
	Fields    []FieldInfo
	Package   string
	Name      string
	TypeRef   string
	Imports   map[string]string
}

Holds the finalized information passed to the template for evaluation.

Jump to

Keyboard shortcuts

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