refactor: logparser v5 to pkgs

This commit is contained in:
Inanc Gumus
2019-08-28 23:46:42 +03:00
parent 4629b59ef2
commit 81b4246973
26 changed files with 601 additions and 458 deletions

View File

@ -0,0 +1,47 @@
// For more tutorials: https://bj.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package pipe
import (
"encoding/json"
"io"
)
// JSON parses json records.
type JSON struct {
reader io.Reader
}
// NewJSONLog creates a json parser.
func NewJSONLog(r io.Reader) *JSON {
return &JSON{reader: r}
}
// Each yields records from a json reader.
func (j *JSON) Each(yield func(Record) error) error {
defer readClose(j.reader)
dec := json.NewDecoder(j.reader)
for {
var r Record
err := dec.Decode(&r)
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := yield(r); err != nil {
return err
}
}
return nil
}