-
Notifications
You must be signed in to change notification settings - Fork 11
/
example_stream_test.go
58 lines (53 loc) · 1.73 KB
/
example_stream_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package edn_test
import (
"fmt"
"io"
"strings"
"olympos.io/encoding/edn"
)
// Typically, you'd also include timestamps here. Imagine that they are here.
type LogEntry struct {
Level edn.Keyword
Message string `edn:"msg"`
Environment string `edn:"env"`
Service string
}
// This example shows how one can do streaming with the decoder, and how to
// properly know when the stream has no elements left.
func Example_streaming() {
const input = `
{:level :debug :msg "1 < 2 ? true" :env "dev" :service "comparer"}
{:level :warn :msg "slow response time from 127.0.1.39" :env "prod" :service "worker 10"}
{:level :warn :msg "worker 8 has been unavailable for 30s" :env "prod" :service "gateway"}
{:level :info :msg "new processing request: what.png" :env "prod" :service "gateway"}
{:level :debug :msg "1 < nil? error" :env "dev" :service "comparer"}
{:level :warn :msg "comparison failed: 1 < nil" :env "dev" :service "comparer"}
{:level :info :msg "received new processing request: what.png" :env "prod" :service "worker 3"}
{:level :warn :msg "bad configuration value :timeout, using 3h" :env "staging" :service "worker 3"}
`
rdr := strings.NewReader(input)
dec := edn.NewDecoder(rdr)
var err error
for {
var entry LogEntry
err = dec.Decode(&entry)
if err != nil {
break
}
if entry.Level == edn.Keyword("warn") && entry.Environment != "dev" {
fmt.Println(entry.Message)
}
}
if err != nil && err != io.EOF {
// Something bad happened to our reader
fmt.Println(err)
return
}
// If err == io.EOF then we've reached end of stream
fmt.Println("End of stream!")
// Output:
// slow response time from 127.0.1.39
// worker 8 has been unavailable for 30s
// bad configuration value :timeout, using 3h
// End of stream!
}