conf

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Config helper go package

This package is a helper for processing json configurations files. It can unmarshal json to a go struct or to the universal map[string]any and prepare it data to show in the terminal or gui form.

GoDoc Go Report Card

How to use

The is an example of usage which read config file and show it in the fine-io gui form: cmd/conf/main.go

The conf package helps unmarshal json to go struct and prepare it data to show in the gui form. This autmatically created form has names of fields and entries to edit values and validator which check fields type.

Current package version can automatically process string and number types of input json. Config package can unmarshal json to the map[string]any without nesessary to create a struct with fields type.

Conf

How to install

The package can be installed using:

go get github.com/teonet-go/conf

The packages example can be installed with:

go install github.com/teonet-go/conf@latest

License

BSD

Documentation

Overview

Config helper go package. It provides configuration handling functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field[T any] struct {
	NameDisplay string // Name to show in form etc.
	Name        string // Field name
	Type        string // Field type
	ValueStr    string // Field value as string
	Value       any    // Field with real struct value

	// Field entry is a custom field which can be used in GetFields and
	// SetValues callbacks
	Entry T
}

Field is a struct that contains metadata and values for a single field of a struct or map[string]any that is passed to the GetFields function. It allows associating additional data with each field through the generic Entry field. The NameDisplay field contains a display name for the field that can be used in UIs.

func (*Field[T]) SetValue added in v0.0.5

func (field *Field[T]) SetValue(p any, value ...string) (err error)

SetValue sets the value of a field in a struct or map.

The function takes in the following parameters:

  • p: An interface{} value representing the struct or map.
  • field: A pointer to the Field[T] struct, which contains information about the field to be set.
  • value: A string representing the value to be set.

The function returns an error if the field cannot be set. The error message provides information about the field name, value, and type.

The function supports setting values for fields of the following types: string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64.

If the value cannot be converted to the field's type, an error is returned.

If the parameter 'p' is not a pointer to a struct or a map, the function panics.

The function does not modify the 'p' parameter directly, but it modifies the value of the specified field.

The function does not have any return values.

func (*Field[T]) ValidateValue added in v0.0.5

func (field *Field[T]) ValidateValue(value string) (err error)

ValidateValue validates the value of a given field.

The function takes in two parameters: field, a pointer to a Field[T] struct, and value, a string representing the value to be validated. The field struct contains information about the field's type and name. The value parameter is a string that will be converted to the appropriate type based on the field's type.

The function returns an error if the value is not of the expected type.

type Fields

type Fields[T any] []*Field[T]

Fields is a slice of Field pointers for the generic type T. It allows operating on a group of fields together.

func GetFields

func GetFields[T any](o any, f func(field *Field[T])) (fields Fields[T])

GetFields generates a list of fields from the given object and calls a function for each detected field.

The function accepts two parameters:

  • o: the object from which to extract the fields. It may be a struct or a map[string]any.
  • f: the function to be called for each field, which takes a pointer to a Field[T] struct as its parameter. Where T is the type of the Entry fied in the Field struct.

It returns a Fields[T] which is a slice of pointers to Field[T] structs.

func (Fields[T]) SetValues

func (fields Fields[T]) SetValues(p any, f func(field *Field[T]) (string, bool))

SetValues iterates over each field in the Fields collection and sets their values based on the provided function.

Parameters:

  • p: The target object where the field values will be set.
  • f: A function that takes a pointer to a Field and returns a string representing the field value.

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Number is an interface that combines the Integer and Float interfaces from the constraints package. This allows code to accept numbers of either integer or floating point types through the single Number interface.

Directories

Path Synopsis
cmd
conf
Config helper go package GUI sample application using Fyne widgets to show json config data.
Config helper go package GUI sample application using Fyne widgets to show json config data.
context
The context package example.
The context package example.
fyne
form
Packages form create fine-go forms widget.
Packages form create fine-go forms widget.

Jump to

Keyboard shortcuts

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