Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
fix: parse newline delimited json response from routing/get
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 9, 2023
1 parent a129864 commit 6474a77
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions routing.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -103,10 +106,25 @@ func (ps *proxyRouting) fetch(ctx context.Context, key string) ([]byte, error) {
return nil, err
}

b64 := string(rb)
b64 = strings.TrimSpace(b64)
b64 = strings.TrimPrefix(b64, "\"")
b64 = strings.TrimSuffix(b64, "\"")
parts := bytes.Split(bytes.TrimSpace(rb), []byte("\n"))
var b64 string

for _, part := range parts {
var evt routing.QueryEvent
err = json.Unmarshal(part, &evt)
if err != nil {
return nil, fmt.Errorf("routing/get value cannot be parsed: %w", err)
}

if evt.Type == routing.Value {
b64 = evt.Extra
break
}
}

if b64 == "" {
return nil, errors.New("routing/get value has no key")
}

rb, err = base64.StdEncoding.DecodeString(b64)
if err != nil {
Expand Down

0 comments on commit 6474a77

Please sign in to comment.