[counter] Add template support for counter step

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-01-31 01:50:08 +01:00
parent f001b79635
commit 287a38aa02
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 26 additions and 5 deletions

View file

@ -36,8 +36,8 @@ func init() {
Key: "counter_step",
Name: "Counter Step",
Optional: true,
SupportTemplate: false,
Type: plugins.ActionDocumentationFieldTypeInt64,
SupportTemplate: true,
Type: plugins.ActionDocumentationFieldTypeString,
},
{
Default: "",
@ -132,8 +132,16 @@ func (a ActorCounter) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, ev
}
var counterStep int64 = 1
if s := attrs.MustInt64("counter_step", ptrIntZero); s != 0 {
counterStep = s
if s := attrs.MustString("counter_step", ptrStringEmpty); s != "" {
parseStep, err := formatMessage(s, m, r, eventData)
if err != nil {
return false, errors.Wrap(err, "execute counter step template")
}
counterStep, err = strconv.ParseInt(parseStep, 10, 64)
if err != nil {
return false, errors.Wrap(err, "parse counter step")
}
}
return false, errors.Wrap(

View file

@ -289,7 +289,7 @@ func (f *FieldCollection) String(name string) (string, error) {
return iv.String(), nil
}
return "", ErrValueMismatch
return fmt.Sprintf("%v", v), nil
}
// StringSlice tries to read key name as []string

View file

@ -78,3 +78,16 @@ func TestFieldCollectionNilDataGet(t *testing.T) {
}
}
}
func TestFieldCollectionIntToString(t *testing.T) {
var val int = 123
fc := FieldCollectionFromData(map[string]interface{}{"test": val})
if !fc.CanString("test") {
t.Fatalf("cannot convert %T to string", val)
}
if v := fc.MustString("test", nil); v != "123" {
t.Errorf("unexpected value: 123 != %s", v)
}
}