Skip to content

ConfigField

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

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"}}},
	}
}

フィールド

Key

モジュール名の下での保存キー。設定画面での識別子でもあり、Strings_cfg_<key> が説明する対象でもあります。

Type

意味
boolオン・オフ
int整数
float小数
string自由テキスト
choice固定の選択肢から1つ
series値のリスト
urlURL
linkTelegram のリンク
hidden保存するが表示しない

Default

利用者が何も設定していない間に返る値。Type と一致している必要があります。

Validator

不正な入力を保存前に弾きます。拒否は利用者に見えます。

Secret

true にすると、値はログで伏せられ、バックアップではプレースホルダに置き換わり ます。バックアップから復元しても、マーカーで上書きされずに現在の値が保たれます。

API キー、トークン、パスワードに使ってください。

バリデータ

バリデータオプション
&BooleanValidator{}
&IntegerValidator{}Minimum/HasMinMaximum/HasMax
&FloatValidator{}Minimum/HasMinMaximum/HasMax
&StringValidator{}MinLenMaxLen
&ChoiceValidator{}PossibleValues []string
&SeriesValidator{}Item ValidatorMinLen/HasMin
&URLValidator{}
&LinkValidator{}
&RegExpValidator{}Pattern *regexp.Regexp
&HiddenValidator{}Inner Validator
&TelegramIDValidator{}
&EmojiValidator{}
&EntityLikeValidator{}
&UnionValidator{}Validators []Validator
&NoneTypeValidator{}何も受け付けない

値を読む

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

変更に反応する

go
func (m *Weather) ConfigReady(config map[string]any) error {
	units, ok := config["units"].(string)
	if !ok {
		return fmt.Errorf("units は文字列である必要があります。実際は %T", config["units"])
	}
	m.mu.Lock()
	m.units = units
	m.mu.Unlock()
	return nil
}

起動時と、変更のたびに呼ばれます。エラーを返すと変更は拒否され、そのメッセージが 利用者に表示されます。

Released under the GNU AGPL v3 License.