app

package
v0.0.0-...-6299aa6 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(configPath string, name string) (*Application, Error)

Create the Application. This should be done after configuration fields are registered

func CreateWithBuilder

func CreateWithBuilder(configPath string, name string, builderFn ConfigurationBuilderFn) (*Application, Error)

CreateWithBuilder creates the Application using the additional ConfigurationBuilderFn to add application specific configuration

func LoadProfile

func LoadProfile(defaultProfile Profile) error

LoadProfile will attempt to fetch the active profile from the environment. If no active profile is specified then the defaultProfile will be used.

func NewConfiguration

func NewConfiguration(path string) (*Configuration, Error)

NewConfiguration creates a Configuration from the given ini file path

func NewConfigurationFromContents

func NewConfigurationFromContents(contents string) (*Configuration, Error)

NewConfigurationFromContents creates a Configuration from the given ini contents

func OverrideProfile

func OverrideProfile(p Profile)

OverrideProfile allows for active profile to be overridden

func OverrideProfileValue

func OverrideProfileValue(value *string) error

OverrideProfileValue allows for the active profile to be overridden in out of band cases like unit tests.

Types

type Application

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

type CLIArgs

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

CLIArgs stores parsed command line args

type Configuration

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

Configuration encapsulates the configuration for an application

func (*Configuration) GetBoolValue

func (c *Configuration) GetBoolValue(fieldName string) (*bool, Error)

func (*Configuration) GetFloatValue

func (c *Configuration) GetFloatValue(fieldName string) (*float64, Error)

func (*Configuration) GetIntValue

func (c *Configuration) GetIntValue(fieldName string) (*int, Error)

func (*Configuration) GetStringValue

func (c *Configuration) GetStringValue(fieldName string) (*string, Error)

func (*Configuration) GetUintValue

func (c *Configuration) GetUintValue(fieldName string) (*uint, Error)

func (*Configuration) GetValueMetadata

func (c *Configuration) GetValueMetadata(fieldName string) *ValueMetadata

GetValueMetadata returns the metadata obtained when parsing a Field with the associated fieldName

func (*Configuration) LoadFields

func (c *Configuration) LoadFields(cliArgs []string) Error

LoadFields loads the values from the registered fields into the configuration

type ConfigurationBuilderFn

type ConfigurationBuilderFn func(registry *FieldRegistry) Error

ConfigurationBuilderFn function used by an Application to build its configuration field definitions

type Error

type Error interface {
	error
	Code() ErrorCode
	CodeValue() string
	Cause() error
	GetContext() string
	SetContext(string)
	GetMetadataValue(string) string
	GetMetadata() map[string]string
}

Error interface describes an application error

func NewAlreadyExistsError

func NewAlreadyExistsError(format string, args ...any) Error

func NewIOError

func NewIOError(format string, args ...any) Error

func NewIllegalArgumentError

func NewIllegalArgumentError(format string, args ...any) Error

func NewIllegalStateError

func NewIllegalStateError(format string, args ...any) Error

func NewInternalError

func NewInternalError(format string, args ...any) Error

func NewNotFoundError

func NewNotFoundError(format string, args ...any) Error

func NewSvcUnavailableError

func NewSvcUnavailableError(format string, args ...any) Error

func NewSysConfigError

func NewSysConfigError(format string, args ...any) Error

func NewValidationError

func NewValidationError(format string, args ...any) Error

type ErrorBuilder

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

ErrorBuilder builds an Error

func BuildAlreadyExistsError

func BuildAlreadyExistsError() *ErrorBuilder

func BuildIOError

func BuildIOError() *ErrorBuilder

func BuildIllegalArgumentError

func BuildIllegalArgumentError() *ErrorBuilder

func BuildIllegalStateError

func BuildIllegalStateError() *ErrorBuilder

func BuildInternalError

func BuildInternalError() *ErrorBuilder

func BuildNotFoundError

func BuildNotFoundError() *ErrorBuilder

func BuildSvcUnavailableError

func BuildSvcUnavailableError() *ErrorBuilder

func BuildSysConfigError

func BuildSysConfigError() *ErrorBuilder

func BuildValidationError

func BuildValidationError() *ErrorBuilder

func NewErrorBuilder

func NewErrorBuilder(code ErrorCode, codeValue string) *ErrorBuilder

NewErrorBuilder creates a new ErrorBuilder

func NewErrorBuilderWithFactory

func NewErrorBuilderWithFactory(code ErrorCode, codeValue string,
	fn func(impl *ErrorImpl) Error) *ErrorBuilder

NewErrorBuilderWithFactory creates a new ErrorBuilder

func (*ErrorBuilder) Cause

func (b *ErrorBuilder) Cause(err error) *ErrorBuilder

Cause sets the optional error that caused this error

func (*ErrorBuilder) Context

func (b *ErrorBuilder) Context(context string) *ErrorBuilder

Context sets a contextual string that defines the operation in-process when the error occurred

func (*ErrorBuilder) Msg

func (b *ErrorBuilder) Msg(msg string) Error

Msg sets the message for the Error and creates it

func (*ErrorBuilder) Msgf

func (b *ErrorBuilder) Msgf(format string, args ...any) Error

Msgf sets the message for the Error using message formatting and creates it

func (*ErrorBuilder) Str

func (b *ErrorBuilder) Str(key string, val string) *ErrorBuilder

Str sets a string key and value to the metadata associated with this error

type ErrorCode

type ErrorCode uint8
const AlreadyExistsErrorCode ErrorCode = 8

AlreadyExistsErrorCode relates to a client level error where the result of an operation is to produce a new entity but the entity already exists in the system

const IOErrorCode ErrorCode = 9

IOErrorCode relates to an error while trying to access a resource like a file

const IllegalArgumentError ErrorCode = 4

IllegalArgumentError relates to an internal server error that means an internal argument check failed. This type of error usually signifies a bug in the software

const IllegalStateErrorCode ErrorCode = 6

IllegalStateErrorCode relates to a client level error that signifies the operation asked of the server cannot be performed because it is not in the proper state

const InternalErrorCode ErrorCode = 1

InternalErrorCode relates to a general internal server error that should be avoided if a more specific one can be chosen

const NotFoundErrorCode ErrorCode = 7

NotFoundErrorCode relates to a client level error where an entity is referenced by the client that does not exist

const ServiceUnavailableErrorCode ErrorCode = 3

ServiceUnavailableErrorCode signifies that either the server or one of its dependencies is not able to service the request.

const SystemConfigurationErrorCode ErrorCode = 2

SystemConfigurationErrorCode signifies a server error that keeps the server from starting. It is related to an issue that can be fixed in the software's configuration

const UnknownErrorCode ErrorCode = 0
const ValidationErrorCode ErrorCode = 5

ValidationErrorCode signifies a client level error that means the data provided by the client to the server is invalid

type ErrorImpl

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

ErrorImpl implements an Error

func (*ErrorImpl) Cause

func (e *ErrorImpl) Cause() error

Cause returns the optional underlying error

func (*ErrorImpl) Code

func (e *ErrorImpl) Code() ErrorCode

Code returns the associated error code

func (*ErrorImpl) CodeValue

func (e *ErrorImpl) CodeValue() string

CodeValue returns the associated error code

func (*ErrorImpl) Error

func (e *ErrorImpl) Error() string

Error returns a descriptor string that encapsulates all the error's metadata. This satisfies the 'error' interface

func (*ErrorImpl) GetContext

func (e *ErrorImpl) GetContext() string

GetContext returns this error's optional context

func (*ErrorImpl) GetMetadata

func (e *ErrorImpl) GetMetadata() map[string]string

GetMetadata returns this error's optional metadata. It can be nil

func (*ErrorImpl) GetMetadataValue

func (e *ErrorImpl) GetMetadataValue(key string) string

GetMetadataValue returns a value from this error's metadata.

func (*ErrorImpl) SetContext

func (e *ErrorImpl) SetContext(ctx string)

SetContext identifies the context of this error

type Field

type Field struct {
	ShortDescription  string
	LongDescription   string
	Name              string
	ArgName           string
	EnvVar            string
	ConfigSectionName string
	ConfigFieldName   string
	DefaultValue      any
	Required          bool
	Type              ValueType
}

Field defines a general field for configuration

type FieldBuilder

type FieldBuilder[T FieldType] struct {
	// contains filtered or unexported fields
}

FieldBuilder builder for a Field

func (*FieldBuilder[T]) ArgName

func (b *FieldBuilder[T]) ArgName(argName string) *FieldBuilder[T]

func (*FieldBuilder[T]) ConfigName

func (b *FieldBuilder[T]) ConfigName(configSectionName string, configFieldName string) *FieldBuilder[T]

func (*FieldBuilder[T]) Default

func (b *FieldBuilder[T]) Default(defaultValue T) *FieldBuilder[T]

func (*FieldBuilder[T]) Descriptions

func (b *FieldBuilder[T]) Descriptions(shortDesc string, longDesc string) *FieldBuilder[T]

func (*FieldBuilder[T]) EnvVar

func (b *FieldBuilder[T]) EnvVar(envVar string) *FieldBuilder[T]

func (*FieldBuilder[T]) Register

func (b *FieldBuilder[T]) Register() *Field

func (*FieldBuilder[T]) Required

func (b *FieldBuilder[T]) Required() *FieldBuilder[T]

func (*FieldBuilder[T]) ShortDesc

func (b *FieldBuilder[T]) ShortDesc(shortDesc string) *FieldBuilder[T]

type FieldRegistry

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

FieldRegistry is used to create and store Fields

func (*FieldRegistry) CreateBooleanField

func (r *FieldRegistry) CreateBooleanField(name string) *FieldBuilder[bool]

CreateBooleanField Builds a new bool type FieldBuilder

func (*FieldRegistry) CreateFloatField

func (r *FieldRegistry) CreateFloatField(name string) *FieldBuilder[float64]

CreateFloatField Builds a new float64 type FieldBuilder

func (*FieldRegistry) CreateIntField

func (r *FieldRegistry) CreateIntField(name string) *FieldBuilder[int]

CreateIntField Builds a new Integer type FieldBuilder

func (*FieldRegistry) CreateStringField

func (r *FieldRegistry) CreateStringField(name string) *FieldBuilder[string]

CreateStringField Builds a new string type FieldBuilder

func (*FieldRegistry) CreateUintField

func (r *FieldRegistry) CreateUintField(name string) *FieldBuilder[uint]

CreateUintField Builds a new uint type FieldBuilder

type FieldSpecifier

type FieldSpecifier uint8

FieldSpecifier is the enumerated source type that was responsible for specifying the Field's value

const (
	None FieldSpecifier = iota
	EnvironmentVar
	CommandLine
	File
)

func (FieldSpecifier) String

func (s FieldSpecifier) String() string

String Stringer interface

type FieldType

type FieldType interface {
	int | uint | float64 | bool | string
}

type Profile

type Profile uint8

Profile enum

const (
	Production Profile = iota + 1
	Development
	Test
)

func GetActiveProfile

func GetActiveProfile() Profile

GetActiveProfile returns the Profile that the application is running under

func (Profile) String

func (p Profile) String() string

String stringer interface

type ValueMetadata

type ValueMetadata struct {
	Value     any
	Specifier FieldSpecifier
	Field     *Field
}

ValueMetadata contains the resolved value of a Field as well as some extraction metadata

type ValueType

type ValueType uint8

ValueType is the datatype of the Fields value

const (
	Int ValueType = iota
	Uint
	Float
	String
	Bool
)

func (ValueType) String

func (vt ValueType) String() string

String Stringer interface for a ValueType

func (ValueType) ToString

func (vt ValueType) ToString(value any) string

ToString converts the value to a string

Jump to

Keyboard shortcuts

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