Documentation ¶
Overview ¶
Package stronf exposes the core of the structconf module to support writing custom configuration handlers.
The core ideas revolve around the Field, HandleFunc, and how values are Coerce'd into the Field.
The core properties of a Field are:
- Not mutable from outside of the stronf package.
- Expose only enough information to make programmatic decisions about what values to return.
- Expose reflect package members only when necessary, and to avoid an unecessary abstraction layer.
The core properties of a settable field in SettableFields are:
- Is settable from the reflect package's perspective.
- Satisfies either encoding.TextUnmarshaler or encoding.BinaryUnmarshaler interfaces, checked for in that order.
- Is a value type.
When testing, there is no interface for the Field, so a test struct must be defined and a call to SettableFields is required to get a slice of Field to work with. There are good examples of this in the confhandler package tests.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field represents a single settable struct field in the parsed struct.
func SettableFields ¶
SettableFields returns a slice of all settable struct fields in the provided struct. The provided argument must be a pointer to a struct.
func (Field) LookupTag ¶
LookupTag will return the value associated with the key and optional tag. See examples for supported formats. The bool reports if the key or tag was explicitly found in the struct tag.
Example ¶
package main import ( "fmt" "log" "github.com/kevinfalting/structconf/stronf" ) func main() { type Config struct { DatabaseURL string `conf:"env:DATABASE_URL,required,key:val" custom:"flag:db-url" emptyKey:""` } var config Config fields, err := stronf.SettableFields(&config) if err != nil { log.Println(err) return } if len(fields) != 1 { log.Println("expected 1 field") return } field := fields[0] if _, ok := field.LookupTag("no-exist", "hello"); ok { log.Println("should not recieve a value for a key that doesn't exist") } if val, ok := field.LookupTag("emptyKey", ""); ok { fmt.Printf("val: %q\n", val) } if val, ok := field.LookupTag("custom", "flag"); ok { fmt.Printf("val: %q\n", val) } if val, ok := field.LookupTag("conf", "env"); ok { fmt.Printf("val: %q\n", val) } if val, ok := field.LookupTag("conf", "required"); ok { fmt.Printf("val: %q\n", val) } }
Output: val: "" val: "db-url" val: "DATABASE_URL" val: ""
type HandleFunc ¶
HandleFunc is the function signature for working on a Field.
func CombineHandlers ¶
func CombineHandlers(handlers ...HandleFunc) HandleFunc
CombineHandlers will return a handler that calls each handler in the order they are provided. The last handler takes precedence over the one before it.