mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-10 01:00:05 +00:00
38 lines
699 B
Go
38 lines
699 B
Go
package plugins
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type RuleAction struct {
|
|
yamlUnmarshal func(interface{}) error
|
|
jsonValue []byte
|
|
}
|
|
|
|
func (r *RuleAction) UnmarshalJSON(d []byte) error {
|
|
r.jsonValue = d
|
|
return nil
|
|
}
|
|
|
|
func (r *RuleAction) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
r.yamlUnmarshal = unmarshal
|
|
return nil
|
|
}
|
|
|
|
func (r *RuleAction) Unmarshal(v interface{}) error {
|
|
switch {
|
|
case r.yamlUnmarshal != nil:
|
|
return r.yamlUnmarshal(v)
|
|
|
|
case r.jsonValue != nil:
|
|
jd := json.NewDecoder(bytes.NewReader(r.jsonValue))
|
|
jd.DisallowUnknownFields()
|
|
return jd.Decode(v)
|
|
|
|
default:
|
|
return errors.New("unmarshal on unprimed object")
|
|
}
|
|
}
|