cfg

The cfg package is used to enable the options pattern in Connector.DefineConfig. This pattern is used in Go for expressing optional arguments. This package defines the various Options as well as their collector Config which is not used directly but rather applies and collects the list of Options behind the scenes.

For example:

con.DefineConfig("Database", cfg.Validation("url"), cfg.Secret())

The following options are supported:

  • cfg.DefaultValue specifies a default value for the property when one is not provided by the configurator
  • cfg.Validation uses a pattern to validate values before they are set
    • str - Plain text, no validation
    • str ^[a-zA-Z0-9]+$ - Text with regexp validation
    • bool - Must be true or false
    • int - An integer, no validation
    • int [0,60] - An integer in range
    • float - A decimal number, no validation
    • float [0.0,1.0) - A decimal number in range
    • dur - A duration such as 7h3m45s500ms300us100ns
    • dur (0s,24h] - A duration in range
    • set Red|Green|Blue - A set of explicit options separated by |
    • url - A URL
    • email - An email address, either Joe <joe@example.com> or just joe@example.com
    • json - A valid JSON string
  • cfg.Validator supplies a function func(ctx context.Context, value string) error that checks the raw value in addition to the pattern-based rule above. A value is rejected when the function returns an error. It is the hook through which a structured config is validated as a decoded structure rather than as text, and is typically wired by the code-generated intermediate rather than by hand. The function must be a pure check of its input, without I/O or downstream calls
  • cfg.Secret indicates that the value of this property is a secret and should not be logged. In PROD and LAB, a microservice defining any secret config refuses to start over a plaintext transport
  • cfg.Description is intended to explain the purpose of the config property and how it will impact the microservice