format

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const EnumInvalid = -1

EnumInvalid is the constant value that an enum gets assigned when it could not be parsed

View Source
const KeyPrefix = "__"

KeyPrefix is the prefix prepended to custom URL query keys that conflict with service config prop keys, consisting of two underscore characters ("__")

Variables

View Source
var ColorizeContainer = ColorizeDesc

ColorizeContainer colorizes the input string as "Container"

View Source
var ColorizeDesc = color.New(color.FgHiBlack).SprintFunc()

ColorizeDesc colorizes the input string as "Description"

View Source
var ColorizeEnum = color.New(color.FgHiCyan).SprintFunc()

ColorizeEnum colorizes the input string as "Enum"

View Source
var ColorizeError = ColorizeFalse

ColorizeError colorizes the input string as "Error"

View Source
var ColorizeFalse = color.New(color.FgHiRed).SprintFunc()

ColorizeFalse colorizes the input string as "False"

View Source
var ColorizeLink = color.New(color.FgHiBlue).SprintFunc()

ColorizeLink colorizes the input string as "Link"

View Source
var ColorizeNumber = color.New(color.FgHiBlue).SprintFunc()

ColorizeNumber colorizes the input string as "Number"

View Source
var ColorizeProp = color.New(color.FgHiMagenta).SprintFunc()

ColorizeProp colorizes the input string as "Prop"

View Source
var ColorizeString = color.New(color.FgHiYellow).SprintFunc()

ColorizeString colorizes the input string as "String"

View Source
var ColorizeTrue = color.New(color.FgHiGreen).SprintFunc()

ColorizeTrue colorizes the input string as "True"

Functions

func BuildQuery

func BuildQuery(cqr t.ConfigQueryResolver) string

BuildQuery converts the fields of a config object to a delimited query string

func BuildQueryWithCustomFields

func BuildQueryWithCustomFields(cqr t.ConfigQueryResolver, query url.Values) url.Values

BuildQueryWithCustomFields converts the fields of a config object to a delimited query string, escaping any custom fields that share the same key as a config prop using a "__" prefix

func ColorFormatTree

func ColorFormatTree(rootNode *ContainerNode, withValues bool) string

ColorFormatTree returns a color highlighted string representation of a node tree

func ColorizeToken

func ColorizeToken(value string, tokenType NodeTokenType) string

ColorizeToken colorizes the value according to the tokenType

func ColorizeValue

func ColorizeValue(value string, isEnum bool) string

ColorizeValue colorizes the input string according to what type appears to be

func CreateEnumFormatter

func CreateEnumFormatter(names []string, optAliases ...map[string]int) types.EnumFormatter

CreateEnumFormatter creates a EnumFormatter struct

func EscapeKey

func EscapeKey(key string) string

EscapeKey adds the KeyPrefix to custom URL query keys that conflict with service config prop keys

func GetConfigFieldString

func GetConfigFieldString(config r.Value, field FieldInfo) (value string, err error)

GetConfigFieldString serializes the config field value to a string representation

func GetConfigPropFromString

func GetConfigPropFromString(structType r.Type, value string) (r.Value, error)

GetConfigPropFromString deserializes a config property from a string representation using the ConfigProp interface

func GetConfigPropString

func GetConfigPropString(propPtr r.Value) (string, error)

GetConfigPropString serializes a config property to a string representation using the ConfigProp interface

func GetConfigQueryResolver

func GetConfigQueryResolver(config t.ServiceConfig) t.ConfigQueryResolver

GetConfigQueryResolver returns the config itself if it implements ConfigQueryResolver otherwise it creates and returns a PropKeyResolver that implements it

func GetServiceConfig

func GetServiceConfig(service types.Service) types.ServiceConfig

GetServiceConfig returns the inner config of a service

func IsNumber

func IsNumber(value string) bool

IsNumber returns whether the specified string is number-like

func ParseBool

func ParseBool(value string, defaultValue bool) (parsedValue bool, ok bool)

ParseBool returns true for "1","true","yes" or false for "0","false","no" or defaultValue for any other value

func PrintBool

func PrintBool(value bool) string

PrintBool returns "Yes" if value is true, otherwise returns "No"

func SetConfigField

func SetConfigField(config r.Value, field FieldInfo, inputValue string) (valid bool, err error)

SetConfigField deserializes the inputValue and sets the field of a config to that value

func SetConfigPropsFromQuery

func SetConfigPropsFromQuery(cqr t.ConfigQueryResolver, query url.Values) (url.Values, error)

SetConfigPropsFromQuery iterates over all the config prop keys and sets the config prop to the corresponding query value based on the key. SetConfigPropsFromQuery returns a non-nil url.Values query with all config prop keys removed, even if any of them could not be used to set a config field, and with any escaped keys unescaped. The error returned is the first error that occurred, subsequent errors are just discarded.

func UnescapeKey

func UnescapeKey(key string) string

UnescapeKey removes the KeyPrefix from custom URL query keys that conflict with service config prop keys

Types

type ConsoleTreeRenderer

type ConsoleTreeRenderer struct {
	WithValues bool
}

ConsoleTreeRenderer renders a ContainerNode tree into a ansi-colored console string

func (ConsoleTreeRenderer) RenderTree

func (r ConsoleTreeRenderer) RenderTree(root *ContainerNode, _ string) string

RenderTree renders a ContainerNode tree into a ansi-colored console string

type ContainerNode

type ContainerNode struct {
	*FieldInfo
	Items        []Node
	MaxKeyLength int
}

ContainerNode is a Node with child items

func GetConfigFormat

func GetConfigFormat(config types.ServiceConfig) *ContainerNode

GetConfigFormat returns type and field information about a ServiceConfig

func GetServiceConfigFormat

func GetServiceConfigFormat(service types.Service) *ContainerNode

GetServiceConfigFormat returns type and field information about a ServiceConfig, resolved from it's Service

func (*ContainerNode) Field

func (n *ContainerNode) Field() *FieldInfo

Field returns the inner FieldInfo

func (*ContainerNode) TokenType

func (n *ContainerNode) TokenType() NodeTokenType

TokenType always returns ContainerToken for ContainerNode

func (*ContainerNode) Update

func (n *ContainerNode) Update(tv r.Value)

Update updates the items to match the provided value

type EnumFormatter

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

EnumFormatter is the helper methods for enum-like types

func (EnumFormatter) Names

func (ef EnumFormatter) Names() []string

Names is the list of the valid Enum string values

func (EnumFormatter) Parse

func (ef EnumFormatter) Parse(s string) int

Parse takes an enum mapped string and returns it's int representation or EnumInvalid (-1)

func (EnumFormatter) Print

func (ef EnumFormatter) Print(e int) string

Print takes a enum mapped int and returns it's string representation or "Invalid"

type FieldInfo

type FieldInfo struct {
	Name          string
	Type          r.Type
	EnumFormatter types.EnumFormatter
	Description   string
	DefaultValue  string
	Template      string
	Required      bool
	URLParts      []URLPart
	Title         bool
	Base          int
	Keys          []string
	ItemSeparator rune
}

FieldInfo is the meta data about a config field

func (*FieldInfo) IsEnum

func (fi *FieldInfo) IsEnum() bool

IsEnum returns whether a EnumFormatter has been assigned to the field and that it is of a suitable type

func (*FieldInfo) IsURLPart

func (fi *FieldInfo) IsURLPart(part URLPart) bool

IsURLPart returns whether the field is serialized as the specified part of an URL

type MarkdownTreeRenderer

type MarkdownTreeRenderer struct {
	HeaderPrefix      string
	PropsDescription  string
	PropsEmptyMessage string
}

MarkdownTreeRenderer renders a ContainerNode tree into a markdown documentation string

func (MarkdownTreeRenderer) RenderTree

func (r MarkdownTreeRenderer) RenderTree(root *ContainerNode, scheme string) string

RenderTree renders a ContainerNode tree into a markdown documentation string

type Node

type Node interface {
	Field() *FieldInfo
	TokenType() NodeTokenType
	Update(tv r.Value)
}

Node is the generic config tree item

type NodeTokenType

type NodeTokenType int

NodeTokenType is used to represent the type of value that a node has for syntax highlighting

const (
	// UnknownToken represents all unknown/unspecified tokens
	UnknownToken NodeTokenType = iota
	// NumberToken represents all numbers
	NumberToken
	// StringToken represents strings and keys
	StringToken
	// EnumToken represents enum values
	EnumToken
	// TrueToken represent boolean true
	TrueToken
	// FalseToken represent boolean false
	FalseToken
	// PropToken represent a serializable struct prop
	PropToken
	// ErrorToken represent a value that was not serializable or otherwise invalid
	ErrorToken
	// ContainerToken is used for Array/Slice and Map tokens
	ContainerToken
)

type PropKeyResolver

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

PropKeyResolver implements the ConfigQueryResolver interface for services that uses key tags for query props

func NewPropKeyResolver

func NewPropKeyResolver(config t.ServiceConfig) PropKeyResolver

NewPropKeyResolver creates a new PropKeyResolver and initializes it using the provided config

func (*PropKeyResolver) Bind

func (pkr *PropKeyResolver) Bind(config t.ServiceConfig) PropKeyResolver

Bind is called to create a new instance of the PropKeyResolver, with he internal config reference set to the provided config. This should only be used for configs of the same type.

func (*PropKeyResolver) Get

func (pkr *PropKeyResolver) Get(key string) (string, error)

Get returns the value of a config property tagged with the corresponding key

func (*PropKeyResolver) IsDefault

func (pkr *PropKeyResolver) IsDefault(key string, value string) bool

IsDefault returns whether the specified key value is the default value

func (*PropKeyResolver) KeyIsPrimary

func (pkr *PropKeyResolver) KeyIsPrimary(key string) bool

KeyIsPrimary returns whether the key is the primary (and not an alias)

func (*PropKeyResolver) QueryFields

func (pkr *PropKeyResolver) QueryFields() []string

QueryFields returns a list of tagged keys

func (*PropKeyResolver) Set

func (pkr *PropKeyResolver) Set(key string, value string) error

Set sets the value of it's bound struct's property, tagged with the corresponding key

func (*PropKeyResolver) SetDefaultProps

func (pkr *PropKeyResolver) SetDefaultProps(config t.ServiceConfig) (firstError error)

SetDefaultProps mutates the provided config, setting the tagged fields with their default values If the provided config is nil, the internal config will be updated instead. The error returned is the first error that occurred, subsequent errors are just discarded.

func (*PropKeyResolver) UpdateConfigFromParams

func (pkr *PropKeyResolver) UpdateConfigFromParams(config t.ServiceConfig, params *t.Params) (firstError error)

UpdateConfigFromParams mutates the provided config, updating the values from it's corresponding params If the provided config is nil, the internal config will be updated instead. The error returned is the first error that occurred, subsequent errors are just discarded.

type TreeRenderer

type TreeRenderer interface {
	RenderTree(root *ContainerNode, scheme string) string
}

TreeRenderer renders a ContainerNode tree into a string

type URLPart

type URLPart int

URLPart is an indicator as to what part of an URL a field is serialized to

const (
	URLQuery URLPart = iota
	URLUser
	URLPassword
	URLHost
	URLPort
	URLPath
)

indicator as to what part of an URL a field is serialized to

func ParseURLPart

func ParseURLPart(s string) URLPart

ParseURLPart returns the URLPart that matches the supplied string

func ParseURLParts

func ParseURLParts(s string) []URLPart

ParseURLParts returns the URLParts that matches the supplied string

func (URLPart) String

func (i URLPart) String() string

func (URLPart) Suffix

func (u URLPart) Suffix() rune

Suffix returns the separator between the URLPart and it's subsequent part

type ValueNode

type ValueNode struct {
	*FieldInfo
	Value string
	// contains filtered or unexported fields
}

ValueNode is a Node without any child items

func (*ValueNode) Field

func (n *ValueNode) Field() *FieldInfo

Field returns the inner FieldInfo

func (*ValueNode) TokenType

func (n *ValueNode) TokenType() NodeTokenType

TokenType returns a NodeTokenType that matches the value

func (*ValueNode) Update

func (n *ValueNode) Update(tv r.Value)

Update updates the value string from the provided value

Jump to

Keyboard shortcuts

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