value

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 10 Imported by: 9

Documentation

Overview

Package value provides a value facade for native.Provider to be able to easy be configured using flag libraries like the SDK implementation or other compatible ones. It implements encoding.TextMarshaler and encoding.TextUnmarshaler, too.

Example:

pv := value.NewProvider(native.DefaultProvider)

flag.Var(pv.Consumer.Formatter, "log.format", "Configures the log format.")
flag.Var(pv.Level, "log.level", "Configures the log level.")

flag.Parse()

Now you can call:

$ <myExecutable> -log.format=json -log.level=debug ...

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Consumer

type Consumer struct {
	// Formatter is the corresponding formatter.Formatter facade.
	Formatter Formatter
}

Consumer is a value facade for transparent setting of consumer.Consumer for the slf4g/native implementation. This is quite handy for usage with flags package of the SDK or similar flag libraries. This might be usable, too in contexts where serialization might be required.

func NewConsumer

func NewConsumer(target ConsumerTarget, customizer ...func(*Consumer)) Consumer

NewConsumer create a new instance of Provider with the given target ProviderTarget instance.

type ConsumerTarget

type ConsumerTarget interface {
	consumer.MutableAware
}

ConsumerTarget defines an object that receives the consumer.Consumer managed by the Consumer value facade.

type Formatter

type Formatter struct {
	// Target is the instance of consumer.MutableAware which should be
	// configured by this facade.
	Target FormatterTarget

	// Codec is used to transform provided plain data. If this is not defined
	// DefaultFormatterCodec is used.
	Codec FormatterCodec

	// ColorMode enables in cases where the Target is color.ModeMutableAware
	// to modify the value of it.
	// This will not always work. Not all targets supports it. In this case
	// the value will be swallowed.
	ColorMode *FormatterColorMode
}

Formatter is a value facade for transparent setting of consumer.Consumer for the slf4g/native implementation. This is quite handy for usage with flags package of the SDK or similar flag libraries. This might be usable, too in contexts where serialization might be required.

func NewFormatter

func NewFormatter(target FormatterTarget, customizer ...func(*Formatter)) Formatter

NewFormatter creates a new instance of Formatter with the given target.

func (Formatter) Get

func (instance Formatter) Get() interface{}

Get implements flag.Getter.

func (Formatter) MarshalText

func (instance Formatter) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (Formatter) Set

func (instance Formatter) Set(plain string) error

Set implements flag.Value.

func (Formatter) String

func (instance Formatter) String() string

String implements flag.Value.

func (Formatter) Type added in v1.4.0

func (instance Formatter) Type() string

Type returns the type as a string.

func (Formatter) UnmarshalText

func (instance Formatter) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type FormatterCodec

type FormatterCodec interface {
	// Parse takes a string and creates out of it an instance of formatter.Formatter.
	Parse(plain string) (formatter.Formatter, error)

	// Format takes an instance of formatter.Formatter and formats it as a string.
	Format(what formatter.Formatter) (string, error)
}

FormatterCodec transforms strings to formatter.Formatter and other way around.

var DefaultFormatterCodec FormatterCodec = MappingFormatterCodec{
	"text": formatter.NewText(),
	"json": formatter.NewJson(),
}

DefaultFormatterCodec is the default instance of FormatterCodec which should cover the most of the cases.

func NewFormatterCodecFacade

func NewFormatterCodecFacade(provider func() FormatterCodec) FormatterCodec

NewFormatterCodecFacade creates a new facade of FormatterCodec with the given function that provides the actual FormatterCodec to use.

func NoopFormatterCodec

func NoopFormatterCodec() FormatterCodec

NoopFormatterCodec provides a noop implementation of FormatterCodec.

type FormatterColorMode added in v1.4.0

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

FormatterColorMode enables the access the color.Mode of a FormatterTarget.

func (FormatterColorMode) Get added in v1.4.0

func (instance FormatterColorMode) Get() interface{}

Get implements flag.Getter.

func (*FormatterColorMode) Set added in v1.4.0

func (instance *FormatterColorMode) Set(plain string) error

Set implements flag.Value.

func (FormatterColorMode) String added in v1.4.0

func (instance FormatterColorMode) String() string

String implements flag.Value.

func (FormatterColorMode) Type added in v1.4.0

func (instance FormatterColorMode) Type() string

Type returns the type as a string.

type FormatterTarget

type FormatterTarget interface {
	formatter.MutableAware
}

FormatterTarget defines an object that receives the consumer.Consumer managed by the Formatter value facade.

type Level

type Level struct {
	// Target is the instance of LevelTarget which should be configured
	// by this facade.
	Target LevelTarget

	// Names is used to transform provided plain data. If this is not defined
	// the Target is assumed as nlevel.NamesAware or if this even does not work
	// nlevel.DefaultNames is used.
	Names level.Names
}

Level is a value facade for transparent setting of level.Level for the slf4g/native implementation. This is quite handy for usage with flags package of the SDK or similar flag libraries. This might be usable, too in contexts where serialization might be required.

func NewLevel

func NewLevel(target LevelTarget, customizer ...func(*Level)) Level

NewLevel creates a new instance of Level with the given target.

func (Level) Get

func (instance Level) Get() interface{}

Get implements flag.Getter.

func (Level) MarshalText

func (instance Level) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (Level) Set

func (instance Level) Set(plain string) error

Set implements flag.Value.

func (Level) String

func (instance Level) String() string

String implements flag.Value.

func (Level) Type added in v1.4.0

func (instance Level) Type() string

Type returns the type as a string.

func (Level) UnmarshalText

func (instance Level) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type LevelTarget

type LevelTarget interface {
	level.MutableAware
}

LevelTarget defines an object that receives the level.Level managed by the Level value facade.

type MappingFormatterCodec

type MappingFormatterCodec map[string]formatter.Formatter

MappingFormatterCodec is a default implementation of FormatterCodec which handles the most common cases by default.

func (MappingFormatterCodec) Format

func (instance MappingFormatterCodec) Format(what formatter.Formatter) (string, error)

Format implements FormatterCodec.Format

func (MappingFormatterCodec) Parse

func (instance MappingFormatterCodec) Parse(plain string) (formatter.Formatter, error)

Parse implements FormatterCodec.Parse

type Provider

type Provider struct {
	// Level is the corresponding level.Level facade.
	Level Level

	// Consumer is the corresponding consumer.Consumer facade.
	Consumer Consumer
}

Provider is a value facade for transparent setting of native.Provider for the slf4g/native implementation. This is quite handy for usage with flags package of the SDK or similar flag libraries. This might be usable, too in contexts where serialization might be required.

func NewProvider

func NewProvider(target ProviderTarget, customizer ...func(*Provider)) Provider

NewProvider create a new instance of Provider with the given target ProviderTarget instance.

Example
package main

import (
	"flag"

	"github.com/echocat/slf4g/native"
	"github.com/echocat/slf4g/native/facade/value"
)

func main() {
	pv := value.NewProvider(native.DefaultProvider)

	flag.Var(pv.Consumer.Formatter, "log.format", "Configures the log format.")
	flag.Var(pv.Consumer.Formatter.ColorMode, "log.color", "Configures the log color mode.")
	flag.Var(pv.Level, "log.level", "Configures the log level.")

	flag.Parse()

	// Now you can call:
	// $ <myExecutable> -log.format=json -log.level=debug ...
}
Output:

type ProviderTarget

type ProviderTarget interface {
	LevelTarget
	ConsumerTarget
}

ProviderTarget defines an object that receives the Level and Formatter managed by the Provider value facade.

Jump to

Keyboard shortcuts

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