varexpr

package
v0.6.17 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package varexpr provides some commonly ENV var parse functions.

parse env value, allow expressions:

${VAR_NAME}            Only var name
${VAR_NAME | default}  With default value, if value is empty.
${VAR_NAME | ?error}   With error on value is empty.

Examples:

only key     - "${SHELL}"
with default - "${NotExist | defValue}"
multi key    - "${GOPATH}/${APP_ENV | prod}/dir"

Index

Constants

View Source
const (
	// SepChar separator char split var name and default value
	SepChar  = "|"
	VarLeft  = "${" // default var left format chars
	VarRight = "}"  // default var right format chars

)

Variables

This section is empty.

Functions

func Parse

func Parse(val string) (string, error)

Parse parse ENV var value from input string, support default value.

Format:

${var_name}            Only var name
${var_name | default}  With default value
${var_name | ?error}   With error on value is empty.

func ParseWith

func ParseWith(val string, optFns ...ParseOptFn) (string, error)

ParseWith parse ENV var value from input string, support default value.

func SafeParse

func SafeParse(val string) string

SafeParse parse ENV var value from input string, support default value.

Types

type ParseOptFn

type ParseOptFn func(o *ParseOpts)

ParseOptFn option func

type ParseOpts

type ParseOpts struct {
	// Getter Env value provider func.
	Getter func(string) string
	// ParseFn custom parse expr func. expr like "${SHELL}" "${NotExist|defValue}"
	ParseFn func(string) (string, error)
	// Regexp custom expression regex.
	Regexp *regexp.Regexp
	// var format chars for expression.
	// default left="${", right="}"
	VarLeft, VarRight string
}

ParseOpts parse options for ParseValue

type Parser

type Parser struct {
	ParseOpts
}

Parser parse ENV var value from input string, support default value.

func New

func New(optFns ...ParseOptFn) *Parser

New create a new Parser

func (*Parser) Parse

func (p *Parser) Parse(val string) (newVal string, err error)

Parse parse ENV var value from input string, support default value.

Format:

${var_name}            Only var name
${var_name | default}  With default value
${var_name | ?error}   With error on value is empty.

Jump to

Keyboard shortcuts

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