diff --git a/cmd/parser/main.go b/cmd/parser/main.go index b576f5a..cf1f9ec 100644 --- a/cmd/parser/main.go +++ b/cmd/parser/main.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "flag" "fmt" "github.com/rejlersembriq/gs2" @@ -12,6 +11,15 @@ import ( var filename = flag.String("file", "", "File to parse") +func validateTime(g *gs2.GS2) error { + for _, ts := range g.TimeSeries { + if ts.Start.Add(time.Duration(ts.NoOfValues)*ts.Step) != ts.Stop { + return fmt.Errorf("start %q, stop %q step %q doesnt match", ts.Start.Format(time.RFC3339), ts.Stop.Format(time.RFC3339), ts.Step) + } + } + return nil +} + func main() { flag.Parse() @@ -26,20 +34,20 @@ func main() { start := time.Now() - result, err := gs2.NewDecoder(file).Decode() - if err != nil { - log.Fatal(err) + options := []gs2.DecoderOption{ + gs2.DecodeValidators( + gs2.ValidateNoOfObjects, + gs2.ValidateTimeSeriesValues, + validateTime, + ), } - took := time.Since(start) - - indent, err := json.MarshalIndent(result, "", " ") + _, err = gs2.NewDecoder(file, options...).Decode() if err != nil { log.Fatal(err) } - fmt.Println("") - fmt.Println(string(indent)) + took := time.Since(start) fmt.Printf("Parsing took: %v\n", took) } diff --git a/decode.go b/decode.go index 6c5c049..5da4070 100644 --- a/decode.go +++ b/decode.go @@ -426,6 +426,10 @@ func parseTriplet(val string) (Triplet, error) { } func parseTime(s string) (time.Time, error) { + if s == "" { + return time.Time{}, nil + } + var modifier time.Duration if strings.Contains(s, "24:00:00") { s = strings.Replace(s, "24:00:00", "00:00:00", 1)