Use cases
These are recipes for the current implementation. The examples use a hierarchical CLI configuration like E2Engine's, but do not depend on E2Engine types.
| Use case | Implementation |
|---|---|
| Set a default scalar value | Put default:"value" on the field; call binding.ApplyDefaults(&cfg). The literal fills a zero value. |
| Set a default duration | Put default:"5s" on a time.Duration field; call ApplyDefaults. |
| Allocate a nil pointer to a scalar | Put a literal default tag on the pointer field, for example *int with default:"1". |
| Allocate a nested config section | Put default:"dive" on a *Database field; put defaults on Database's fields. |
| Apply defaults inside an existing struct | Put default tags on the child fields; ApplyDefaults traverses the struct automatically. |
| Allocate an empty slice or map | Put default:"alloc" on the collection field; call ApplyDefaults. |
| Apply child defaults in existing slice or array structs | Put defaultElem:"dive" on the collection and default tags on element fields. |
| Apply child defaults in map value structs | Put default tags on value fields; ApplyDefaults traverses existing map values automatically. |
| Set a literal default for each scalar collection element | Do this in application code. Literal defaultElem:"value" is currently ignored. |
| Validate a scalar with a built-in rule | Put a tag such as validate:"min(1),max(5)" on the field; call binding.Validate(ctx, &cfg). |
Validate a string with myRule only when non-empty | Put validate:"myRule,omitempty" on the field; register model.NewRule[string]("myRule", fn) through model.WithRules. |
| Validate fields inside a nested struct | Put validate tags on child fields; Validate traverses structs and nonnil struct pointers automatically. |
| Validate scalar slice, array, or map elements | Put validateElem:"min(1)" or another matching rule on the collection field. |
| Validate fields inside struct collection elements | Put validateElem:"dive" on the collection and validate tags on element fields. |
Combine dive with another validateElem rule | Put rules on the element's fields instead. The current implementation ignores other element rules when dive is present. |
Require Runner.RemoteURL only when Runner.Mode == "remote" | Put local rules such as validate:"oneof(local,remote)" in tags; check the cross-field condition in code after Validate. |
| Let environment variables override defaults | Build with model.WithEnvPrefix("E2ENGINE"); call ValidateWithDefaults(ctx, &cfg) for defaults, snapshot environment, and validation in order. |
| Apply typed CLI or file values | Implement field.ValueSource and call binding.ApplyValues(&cfg, source); use exact Go field paths such as DB.Driver. |
| Replace a collection from a value source | Return the whole collection for a path such as Endpoints[] from ValueSource.Get. |
| Export a resolved config for inspection | Implement field.ValueSink and call binding.WriteValues(&cfg, sink); append entries if repeated collection child paths matter. |
Struct traversal: A validate:"dive" tag is not needed for nested structs. Validate descends automatically; use validateElem:"dive" only for collection elements that are structs.
Optional rules: omitempty skips all rules on a zero field, regardless of where it appears in the comma-separated tag.
Conditional rules: Tags express local checks. Cross-field decisions such as requiring a remote URL only in remote mode belong in application code or a custom rule on the containing struct.