api

package
v0.4.13 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2022 License: Apache-2.0 Imports: 22 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
	LocalTypeName      string
	DefaultValue       string
	UsageString        string
	FlagMethodName     string
	TestFlagMethodName string
	TestValue          string
	TestStrategy       TestStrategy
	ShouldBindDefault  bool
	ShouldTestDefault  bool
}

type NamedType added in v0.4.6

type NamedType int
const (
	NamedTypeA NamedType = iota
	NamedTypeB
)

func NamedTypeString added in v0.4.6

func NamedTypeString(s string) (NamedType, error)

NamedTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func NamedTypeValues added in v0.4.6

func NamedTypeValues() []NamedType

NamedTypeValues returns all values of the enum

func (NamedType) IsANamedType added in v0.4.6

func (i NamedType) IsANamedType() bool

IsANamedType returns "true" if the value is listed in the enum definition. "false" otherwise

func (NamedType) MarshalJSON added in v0.4.6

func (i NamedType) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for NamedType

func (NamedType) String added in v0.4.6

func (i NamedType) String() string

func (*NamedType) UnmarshalJSON added in v0.4.6

func (i *NamedType) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for NamedType

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

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

func (PFlagProvider) WriteCodeFile

func (p PFlagProvider) WriteCodeFile(outputFilePath string) error

WriteCodeFile evaluates the main code file template and writes the output to outputFilePath

func (PFlagProvider) WriteTestFile

func (p PFlagProvider) WriteTestFile(outputFilePath string) error

WriteTestFile 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 PFlagValueType added in v0.4.6

type PFlagValueType struct {
	Name                     string
	ShouldGenerateSetAndType bool
}

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 TestConstTypeAlias added in v0.4.6

type TestConstTypeAlias = int

type TestStrategy

type TestStrategy string

TestStrategy 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"`
	ConstType        NamedType          `json:"constType"`
	AliasType        TestConstTypeAlias `json:"aliasType"`
}

type TypeInfo

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

TypeInfo 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