chirpnest/rules.go
2020-01-02 18:12:33 +08:00

68 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"regexp"
)
func resetMsgParsingRules() error {
var rules, rules2 []MessageParsingRule
b, err := Asset("data/msg_rules.json")
logOnError(err, "resetMsgParsingRules : load data/msg_rules.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &rules)
for _, r := range rules {
r2 := MessageParsingRule{
Priority: r.Priority,
Description: r.Description,
Rule: r.Rule,
MsgType: r.MsgType,
}
r2.MsgTypeID64, err = codeObjSubTypeId(r2.MsgType)
logOnError(err, "resetMsgParsingRules : codeObjSubTypeId("+r2.MsgType+")")
if err != nil {
return err
}
r2.re, err = regexp.Compile(r2.Rule)
logOnError(err, "resetMsgParsingRules : Compile("+r.Rule+")")
if err != nil {
return err
}
rules2 = append(rules2, r2)
}
_, err = db.Exec(`TRUNCATE TABLE msg_rules;`)
logOnError(err, "resetMsgParsingRules : truncate table msg_rules")
if err != nil {
return err
}
stmt, err := db.Prepare(`INSERT INTO msg_rules (prio, msg_type_id, descn, rule)
VALUES (?, ?, ?, ?);`)
logOnError(err, "resetMsgParsingRules : prepare statement")
if err != nil {
return err
}
defer stmt.Close()
for _, r2 := range rules2 {
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.Description, r2.Rule)
logOnError(err, "resetMsgParsingRules : insert statement "+r2.Description)
if err != nil {
return err
}
}
return nil
}