Tag|Docs

Nested Structs

Bind grouped values into nested schema structs

Nested map-like values can be bound to custom structs. Use this when a group of options belongs together and should not be flattened into the parent schema.

database:"{'host':'localhost','port':5432,'credentials':{'user':'admin','password':'secret'}}"
schema.go
type Credentials struct {
	User     string `option:"user"`
	Password string `option:"password"`
}

type DatabaseTag struct {
	Host        string      `option:"host"`
	Port        int         `option:"port"`
	Credentials Credentials `option:"credentials"`
}

func (t DatabaseTag) Tag() string {
	return "database"
}

Why use nested structs

Nested structs are useful for grouped configuration:

  • database credentials
  • retry policy
  • cache settings
  • validation rules

The outer schema describes the tag, and the nested schema describes the grouped value. This keeps each struct focused and easier to reuse.

Next, combine the pieces when a tag needs a primary value, flags, typed options, and structured values together.