shared

package
v0.1.1-0...-cbd2f86 Latest Latest
Warning

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

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

Documentation

Overview

Package shared provides functions shared by generated code.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ParseString is Parser[string].
	ParseString = wrap(cast.ToStringE)
	// ParseInt is Parser[int].
	ParseInt = wrap(cast.ToIntE)
	// ParseInt8 is Parser[int8].
	ParseInt8 = wrap(cast.ToInt8E)
	// ParseInt16 is Parser[int16].
	ParseInt16 = wrap(cast.ToInt16E)
	// ParseInt32 is Parser[int32].
	ParseInt32 = wrap(cast.ToInt32E)
	// ParseInt64 is Parser[int64].
	ParseInt64 = wrap(cast.ToInt64E)
	// ParseUint is Parser[uint].
	ParseUint = wrap(cast.ToUintE)
	// ParseUint8 is Parser[uint8].
	ParseUint8 = wrap(cast.ToUint8E)
	// ParseUint16 is Parser[uint16].
	ParseUint16 = wrap(cast.ToUint16E)
	// ParseUint32 is Parser[uint32].
	ParseUint32 = wrap(cast.ToUint32E)
	// ParseUint64 is Parser[uint64].
	ParseUint64 = wrap(cast.ToUint64E)
	// ParseFloat32 is Parser[float32].
	ParseFloat32 = wrap(cast.ToFloat32E)
	// ParseFloat64 is Parser[float64].
	ParseFloat64 = wrap(cast.ToFloat64E)
	// ParseDuration is Parser[time.Duration].
	ParseDuration = wrap(cast.ToDurationE)
)

Functions

func DefaultValue

func DefaultValue(defaultValue string, hasDefaultValue bool) *string

DefaultValue returns the default value as a pointer if it's set.

func IsSupportedType

func IsSupportedType(typ structure.TypeBase) (isSupported bool, parserName string)

IsSupportedType checks if there is a parser for the given type.

func LoadSlice

func LoadSlice[T any](c *LoadContext, v *core.Viper, l Loader[T]) ([]T, core.Facts, error)

LoadSlice loads a config value slice with the given loader.

func Parse

func Parse[T any](c *LoadContext, value, defaultValue *string, p Parser[T]) (result T, facts core.Facts, err error)

Parse parsers a config value with the given parser.

func ParseBool

func ParseBool(s string) (bool, error)

ParseBool is Parser[bool].

Also, can parse YAML-specific bool representation, i.e., yes/no, on/off.

func ParseSlice

func ParseSlice[T any](c *LoadContext, v *core.Viper, key string, p Parser[T]) ([]T, core.Facts, error)

ParseSlice parsers a config value slice with the given parser.

func ViperSub

func ViperSub(v *core.Viper, configKey string, hasConfigKey bool) *core.Viper

ViperSub calls core.Viper.Sub if the config key is present.

Types

type LoadContext

type LoadContext struct {
	Keys []string
}

LoadContext tracks the path when reading nested config values.

func ContextWithKey

func ContextWithKey(c *LoadContext, key string, hasKey bool) *LoadContext

ContextWithKey adds the config key to the context if provided.

func (*LoadContext) WithIndex

func (c *LoadContext) WithIndex(index uint64) *LoadContext

WithIndex adds an array index to the context.

func (*LoadContext) WithKey

func (c *LoadContext) WithKey(key string) *LoadContext

WithKey adds a key to the context.

type Loader

type Loader[T any] func(*LoadContext, *core.Viper) (T, core.Facts, error)

Loader loads a value of type T from the given config.

func GetLoader

func GetLoader[T any, PtrT configLoaderPtr[T]]() Loader[T]

GetLoader returns a loader for the given custom type.

type Parser

type Parser[T any] func(string) (T, error)

Parser parses a value of type T from the given string.

func GetParser

func GetParser[T any, PtrT stringParserPtr[T]]() Parser[T]

GetParser returns a parser for the given custom type.

Jump to

Keyboard shortcuts

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