func (p *parser) dispatch(depth int, buf *streambuf.Buffer) (string, bool, bool, bool) { if buf.Len() == 0 { return "", false, true, false } var value string var iserror, ok, complete bool snapshot := buf.Snapshot() switch buf.Bytes()[0] { case '*': value, iserror, ok, complete = p.parseArray(depth, buf) case '$': value, ok, complete = p.parseString(buf) case ':': value, ok, complete = p.parseInt(buf) case '+': value, ok, complete = p.parseSimpleString(buf) case '-': iserror = true value, ok, complete = p.parseSimpleString(buf) default: debug("Unexpected message starting with %s", buf.Bytes()[0]) return "", false, false, false } if !ok || !complete { buf.Restore(snapshot) } return value, iserror, ok, complete }
func (parser *parser) parse(buf *streambuf.Buffer) (bool, bool) { snapshot := buf.Snapshot() content, iserror, ok, complete := parser.dispatch(0, buf) if !ok || !complete { // on error or incomplete message drop all parsing progress, due to // parse not being statefull among multiple calls // => parser needs to restart parsing all content buf.Restore(snapshot) return ok, complete } parser.message.IsError = iserror parser.message.Size = buf.BufferConsumed() parser.message.Message = content return true, true }