Skip to main content

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 caseImplementation
Set a default scalar valuePut default:"value" on the field; call binding.ApplyDefaults(&cfg). The literal fills a zero value.
Set a default durationPut default:"5s" on a time.Duration field; call ApplyDefaults.
Allocate a nil pointer to a scalarPut a literal default tag on the pointer field, for example *int with default:"1".
Allocate a nested config sectionPut default:"dive" on a *Database field; put defaults on Database's fields.
Apply defaults inside an existing structPut default tags on the child fields; ApplyDefaults traverses the struct automatically.
Allocate an empty slice or mapPut default:"alloc" on the collection field; call ApplyDefaults.
Apply child defaults in existing slice or array structsPut defaultElem:"dive" on the collection and default tags on element fields.
Apply child defaults in map value structsPut default tags on value fields; ApplyDefaults traverses existing map values automatically.
Set a literal default for each scalar collection elementDo this in application code. Literal defaultElem:"value" is currently ignored.
Validate a scalar with a built-in rulePut 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-emptyPut validate:"myRule,omitempty" on the field; register model.NewRule[string]("myRule", fn) through model.WithRules.
Validate fields inside a nested structPut validate tags on child fields; Validate traverses structs and nonnil struct pointers automatically.
Validate scalar slice, array, or map elementsPut validateElem:"min(1)" or another matching rule on the collection field.
Validate fields inside struct collection elementsPut validateElem:"dive" on the collection and validate tags on element fields.
Combine dive with another validateElem rulePut 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 defaultsBuild with model.WithEnvPrefix("E2ENGINE"); call ValidateWithDefaults(ctx, &cfg) for defaults, snapshot environment, and validation in order.
Apply typed CLI or file valuesImplement field.ValueSource and call binding.ApplyValues(&cfg, source); use exact Go field paths such as DB.Driver.
Replace a collection from a value sourceReturn the whole collection for a path such as Endpoints[] from ValueSource.Get.
Export a resolved config for inspectionImplement 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.