helpers

package
v0.2.45 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: MIT Imports: 20 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TemplateFuncs = template.FuncMap{

	"trim":                    strings.TrimSpace,
	"trimRightSpace":          trimRightSpace,
	"trimTrailingWhitespaces": trimRightSpace,
	"rpad":                    rpad,
	"quote":                   quote,
	"stripNewlines":           stripNewlines,
	"quoteNewlines":           quoteNewlines,

	"toUpper": strings.ToUpper,
	"toLower": strings.ToLower,

	"replaceRegexp": replaceRegexp,

	"add": add,
	"sub": sub,
	"div": div,
	"mul": mul,

	"parseFloat": parseFloat,
	"parseInt":   parseInt,

	"currency": currency,

	"padLeft":   padLeft,
	"padRight":  padRight,
	"padCenter": padCenter,

	"bold":          bold,
	"underline":     underline,
	"italic":        italic,
	"strikethrough": strikethrough,
	"code":          code,
	"codeBlock":     codeBlock,

	"toYaml":      toYaml,
	"indentBlock": indentBlock,

	"styleBold": styleBold,
}

TemplateFuncs provides helpers for the standard cobra usage and help templates

Functions

func CancelOnSignal

func CancelOnSignal(ctx context.Context, signal_ os.Signal, cancel func()) error

func CastInterfaceListToFloatList added in v0.2.13

func CastInterfaceListToFloatList[To FloatNumber](list []interface{}) ([]To, bool)

func CastInterfaceListToIntList added in v0.2.13

func CastInterfaceListToIntList[To SignedInt | UnsignedInt](list []interface{}) ([]To, bool)

func CastInterfaceToFloat added in v0.2.13

func CastInterfaceToFloat[To FloatNumber](i interface{}) (To, bool)

func CastInterfaceToFloatList added in v0.2.13

func CastInterfaceToFloatList[To FloatNumber](i interface{}) ([]To, bool)

func CastInterfaceToInt added in v0.2.13

func CastInterfaceToInt[To SignedInt | UnsignedInt](i interface{}) (To, bool)

func CastInterfaceToIntList added in v0.2.13

func CastInterfaceToIntList[To SignedInt | UnsignedInt](i interface{}) ([]To, bool)

func CastInterfaceToStringMap added in v0.2.13

func CastInterfaceToStringMap[To any, From any](m interface{}) (map[string]To, bool)

func CastList added in v0.2.6

func CastList[To any, From any](list []From) ([]To, bool)

CastList casts a list of From objects to To, by casting it to an interface{} in between.

func CastList2 added in v0.2.6

func CastList2[To any, From any](list interface{}) ([]To, bool)

CastList2 attempts even harder to cast a list of From object to To, by checking if we might be dealing with a list masquerading as a interface{}, then a []interface{}, before checking for []To.

func CastMapMember added in v0.2.6

func CastMapMember[To any](m map[string]interface{}, k string) (*To, bool)

func CastStringMap added in v0.2.6

func CastStringMap[To any, From any](m map[string]From) (map[string]To, bool)

func CastToNumberList added in v0.2.6

func CastToNumberList[To Number, From Number](list []From) ([]To, bool)

CastToNumberList casts a list of From objects to To. This is useful for transform between different int types, for example.

func ConvertJSONArrayToYAML added in v0.2.5

func ConvertJSONArrayToYAML(jsonContent string) (string, error)

func ConvertJSONMapToYAML added in v0.2.5

func ConvertJSONMapToYAML(jsonContent string) (string, error)

func ConvertYAMLArrayToJSON added in v0.2.5

func ConvertYAMLArrayToJSON(yamlContent string) (string, error)

func ConvertYAMLMapToJSON added in v0.2.5

func ConvertYAMLMapToJSON(yamlContent string) (string, error)

func CreateHTMLTemplate added in v0.2.16

func CreateHTMLTemplate(name string) *html.Template

func CreateTemplate added in v0.2.5

func CreateTemplate(name string) *template.Template

func Float64SliceToStringSlice added in v0.2.16

func Float64SliceToStringSlice(list []float64) []string

func IntSliceToStringSlice added in v0.2.16

func IntSliceToStringSlice(list []int) []string

func InterfaceListToStringList

func InterfaceListToStringList(list []interface{}) []string

func InterfaceToStringList

func InterfaceToStringList(list interface{}) []string

func LoadJSONFile

func LoadJSONFile(path string, target interface{}) error

func LoadYAMLFile

func LoadYAMLFile(path string, target interface{}) error

func ParseCSV added in v0.2.8

func ParseCSV(r io.Reader, options ...ParseCSVOption) (
	[]map[string]interface{}, error,
)

func ParseFS added in v0.2.16

func ParseFS(t *template.Template, f fs.FS, patterns ...string) error

ParseFS will recursively glob for all the files matching the given patterns, and load them into one big template (with sub-templates). The globs use bmatcuk/doublestar and support ** notation for recursive globbing.

func ParseHTMLFS added in v0.2.16

func ParseHTMLFS(t *html.Template, f fs.FS, pattern string, baseDir string) error

ParseHTMLFS will recursively glob for all the files matching the given patterns, and load them into one big template (with sub-templates). It is the html.Template equivalent of ParseFS.

The globs use bmatcuk/doublestar and support ** notation for recursive globbing.

func RenderHtmlTemplateString added in v0.2.7

func RenderHtmlTemplateString(tmpl string, data interface{}) (string, error)

func RenderTemplate added in v0.2.5

func RenderTemplate(tmpl TemplateExecute, data interface{}) (string, error)

func RenderTemplateFile added in v0.2.5

func RenderTemplateFile(filename string, data interface{}) (string, error)

func RenderTemplateString

func RenderTemplateString(tmpl string, data interface{}) (string, error)

func SetFloatListReflectValue added in v0.2.6

func SetFloatListReflectValue[To Number](value reflect.Value, v interface{}) error

func SetIntListReflectValue added in v0.2.6

func SetIntListReflectValue[To Number](value reflect.Value, v interface{}) error

func SetReflectValue added in v0.2.6

func SetReflectValue(value reflect.Value, v interface{}) error

func StringInSlice

func StringInSlice(needle string, haystack []string) bool

func StructToMap

func StructToMap(i interface{}, lowerCaseKeys bool) map[string]interface{}

func ToAlphaString added in v0.2.43

func ToAlphaString(n int) string

Types

type FloatNumber added in v0.2.6

type FloatNumber interface {
	float32 | float64
}

type Number added in v0.2.6

type Number interface {
	int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
}

type ParseCSVOption added in v0.2.25

type ParseCSVOption func(*csv.Reader)

func WithComma added in v0.2.25

func WithComma(c rune) ParseCSVOption

func WithComment added in v0.2.25

func WithComment(c rune) ParseCSVOption

func WithFieldsPerRecord added in v0.2.25

func WithFieldsPerRecord(f int) ParseCSVOption

func WithLazyQuotes added in v0.2.25

func WithLazyQuotes(l bool) ParseCSVOption

func WithTrimLeadingSpace added in v0.2.25

func WithTrimLeadingSpace(t bool) ParseCSVOption

type SignedInt added in v0.2.6

type SignedInt interface {
	int | int8 | int16 | int32 | int64
}

type TemplateExecute added in v0.2.7

type TemplateExecute interface {
	Execute(wr io.Writer, data any) error
}

type UnsignedInt added in v0.2.6

type UnsignedInt interface {
	uint | uint8 | uint16 | uint32 | uint64 | uintptr
}

Jump to

Keyboard shortcuts

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