2019-08-26 14:37:58 +03:00
|
|
|
// For more tutorials: https://bj.learngoprogramming.com
|
2019-08-17 15:55:25 +03:00
|
|
|
//
|
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-26 21:52:47 +03:00
|
|
|
"bufio"
|
2019-08-17 15:55:25 +03:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2019-08-26 14:37:58 +03:00
|
|
|
type jsonLog struct {
|
|
|
|
reader io.Reader
|
2019-08-17 15:55:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:37:58 +03:00
|
|
|
func newJSONLog(r io.Reader) *jsonLog {
|
|
|
|
return &jsonLog{reader: r}
|
2019-08-17 15:55:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:37:58 +03:00
|
|
|
func (j *jsonLog) each(yield resultFn) error {
|
|
|
|
defer readClose(j.reader)
|
2019-08-17 15:55:25 +03:00
|
|
|
|
2019-08-26 21:52:47 +03:00
|
|
|
dec := json.NewDecoder(bufio.NewReader(j.reader))
|
2019-08-17 15:55:25 +03:00
|
|
|
|
2019-08-26 21:52:47 +03:00
|
|
|
for {
|
|
|
|
var r result
|
2019-08-17 15:55:25 +03:00
|
|
|
|
2019-08-26 21:52:47 +03:00
|
|
|
err := dec.Decode(&r)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-17 15:55:25 +03:00
|
|
|
}
|
|
|
|
|
2019-08-26 21:52:47 +03:00
|
|
|
yield(r)
|
2019-08-17 15:55:25 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|