structplan

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package structplan builds and caches, per reflect.Type, the flattened list of settable fields on a struct, each with its config metadata already parsed from the two struct tags gostructor understands:

  • cfg (routing & naming): `cfg:"base_name,source:override,source2:override"`. The first element is the field's base name; the rest are per-source key overrides.
  • gos (behavior & metadata): `gos:"default:8080,sep:;,secret,optional"`, a comma-list of bare flags (secret, optional) and key:value meta (default, sep).

The reflection walk runs once per type and is reused across Configure calls rather than repeated each call. Both tags are parsed with plain strings.Split.

Index

Constants

View Source
const (
	CfgTag = "cfg"
	GosTag = "gos"
)

CfgTag and GosTag are the struct tag names gostructor reads.

Variables

This section is empty.

Functions

func ParseCfg

func ParseCfg(tag string) (base string, overrides map[string]string)

ParseCfg splits a cfg tag value into its base name and per-source overrides. "port,env:SERVER_PORT,json:server.host" -> "port", {env:SERVER_PORT, json:server.host}. An empty tag yields an empty base and no overrides.

func ParseGos

func ParseGos(tag string) (meta map[string]string, flags map[string]bool)

ParseGos splits a gos tag value into meta (key:value) and flags (bare tokens). "default:8080,secret,optional" -> {default:8080}, {secret, optional}.

Comma is the list separator, so a meta value may not contain a comma. For a multi-valued slice default, pick a non-comma separator with sep and use it in the default too, e.g. `gos:"sep:|,default:a|b|c"`.

Types

type Field

type Field struct {
	Struct reflect.StructField
	Index  []int // path suitable for reflect.Value.FieldByIndex

	// Base is the base name (first cfg element), used by each source's naming
	// strategy to derive its key when no explicit override is given.
	Base string
	// Overrides maps a source name (e.g. "env") to the explicit key it should
	// use for this field, from `cfg:"base,env:OVERRIDE"`.
	Overrides map[string]string
	// Meta holds gos key:value entries (e.g. "default" -> "8080").
	Meta map[string]string
	// Flags holds gos bare flags that are present (e.g. "secret", "optional").
	Flags map[string]bool
}

Field is one leaf (non-struct) field discovered while flattening a struct's fields, including nested/embedded structs, with its cfg/gos tags parsed once and cached.

type Plan

type Plan struct {
	Fields []Field
}

Plan is the flattened field list for one struct type.

func For

func For(t reflect.Type) *Plan

For returns the cached Plan for t, building and caching it on first use. t must be a struct type (not a pointer to one).

Jump to

Keyboard shortcuts

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