configstruct

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package configstruct parses unstructured maps into structures

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Set

func Set(config configmap.Getter, opt interface{}) (err error)

Set interprets the field names in defaults and looks up config values in the config passed in. Any values found in config will be set in the opt structure.

opt must be a pointer to a struct. The struct should have entirely public fields. The field names are converted from CamelCase to snake_case and looked up in the config supplied or a `config:"field_name"` is looked up.

If items are found then they are converted from string to native types and set in opt.

All the field types in the struct must implement fmt.Scanner.

func StringToInterface

func StringToInterface(def interface{}, in string) (newValue interface{}, err error)

StringToInterface turns in into an interface{} the same type as def

This supports a subset of builtin types, string, integer types, bool, time.Duration and []string.

Builtin types are expected to be encoding as their natural stringificatons as produced by fmt.Sprint except for []string which is expected to be encoded a a CSV with empty array encoded as "".

Any other types are expected to be encoded by their String() methods and decoded by their `Set(s string) error` methods.

Types

type Item

type Item struct {
	Name  string            // snake_case
	Field string            // CamelCase
	Set   func(interface{}) // set this field
	Value interface{}
}

Item describes a single entry in the options structure

func Items

func Items(opt interface{}) (items []Item, err error)

Items parses the opt struct and returns a slice of Item objects.

opt must be a pointer to a struct. The struct should have entirely public fields.

The config_name is looked up in a struct tag called "config" or if not found is the field name converted from CamelCase to snake_case.

Nested structs are looked up too. If the parent struct has a struct tag, this will be used as a prefix for the values in the sub struct, otherwise they will be embedded as they are.

Jump to

Keyboard shortcuts

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