Improve the parser, automatically set empty destinations to TRANSIENT when using a dialer, better error handling, when a socket gets closed, increment the ID and re-create it

This commit is contained in:
idk
2020-11-29 16:09:55 -05:00
parent 38ca0d08e7
commit 0d10b5b516
4 changed files with 46 additions and 16 deletions

View File

@ -44,6 +44,25 @@ func parseReply(line string) (*Reply, error) {
if len(parts) < 3 {
return nil, fmt.Errorf("Malformed Reply.\n%s\n", line)
}
preParseReply := func() []string {
val := ""
quote := false
for _, v := range parts {
if strings.Contains(v, "=\"") {
quote = true
}
if strings.Contains(v, "\"\n") || strings.Contains(v, "\" ") {
quote = false
}
if quote {
val += v + "_"
} else {
val += v + " "
}
}
return strings.Split(strings.TrimSuffix(strings.TrimSpace(val), "_"), " ")
}
parts = preParseReply()
r := &Reply{
Topic: parts[0],
@ -64,8 +83,9 @@ func parseReply(line string) (*Reply, error) {
kvPair := strings.SplitN(v, "=", 2)
if kvPair != nil {
if len(kvPair) == 1 {
return nil, fmt.Errorf("Malformed key-value-pair len 1.\n%s\n", kvPair)
} else if len(kvPair) != 2 {
return nil, fmt.Errorf("Malformed key-value-pair.\n%s\n", kvPair)
return nil, fmt.Errorf("Malformed key-value-pair len != 2.\n%s\n", kvPair)
}
}
r.Pairs[kvPair[0]] = kvPair[len(kvPair)-1]