Skip to content

ConfigField

go
type ConfigField struct {
	Key       string
	Type      string
	Default   any
	Validator Validator
	Secret    bool
}

Declared through ModuleWithConfigSchema:

go
var _ goroku.ModuleWithConfigSchema = (*Weather)(nil)

func (m *Weather) ConfigSchema() []goroku.ConfigField {
	return []goroku.ConfigField{
		{Key: "units", Type: "choice", Default: "metric",
			Validator: &goroku.ChoiceValidator{PossibleValues: []string{"metric", "imperial"}}},
	}
}

Fields

Key

Storage key, under your module's name. Also the settings-UI identifier, and what a _cfg_<key> string in Strings documents.

Type

ValueMeaning
boolToggle
intInteger
floatDecimal
stringFree text
choiceOne of a fixed set
seriesList of values
urlURL
linkTelegram link
hiddenStored but not shown

Default

Returned when the user has not set anything. Must match Type.

Validator

Rejects bad input before it is stored. The user sees the rejection.

Secret

true redacts the value in logs and replaces it with a placeholder in backups. Restoring a backup keeps the live value rather than overwriting it with the marker.

Use it for API keys, tokens and passwords.

Validators

ValidatorOptions
&BooleanValidator{}
&IntegerValidator{}Minimum/HasMin, Maximum/HasMax
&FloatValidator{}Minimum/HasMin, Maximum/HasMax
&StringValidator{}MinLen, MaxLen
&ChoiceValidator{}PossibleValues []string
&SeriesValidator{}Item Validator, MinLen/HasMin
&URLValidator{}
&LinkValidator{}
&RegExpValidator{}Pattern *regexp.Regexp
&HiddenValidator{}Inner Validator
&TelegramIDValidator{}
&EmojiValidator{}
&EntityLikeValidator{}
&UnionValidator{}Validators []Validator
&NoneTypeValidator{}Accepts nothing

Reading values

go
units := m.DB.GetString(m.Name(), "units", "metric")

Reacting to changes

go
func (m *Weather) ConfigReady(config map[string]any) error {
	units, ok := config["units"].(string)
	if !ok {
		return fmt.Errorf("units must be a string, got %T", config["units"])
	}
	m.mu.Lock()
	m.units = units
	m.mu.Unlock()
	return nil
}

Called at startup and on every change. Returning an error rejects the change and shows the message to the user.

Released under the GNU AGPL v3 License.