-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
52 lines (47 loc) · 1.09 KB
/
parser.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
package parser
import (
"fmt"
"strconv"
"strings"
"time"
)
type Parser interface {
Parse(string) (float64, error)
}
// Parsers loads Parser types from the array of strings
// Example:
// 1,Time,2006-01-02
// ^-^----^----index(int),name(string),format(string)
func ParsersFromArgs(args []string) (map[int]Parser, error) {
parsers := make(map[int]Parser)
for _, arg := range args {
split := strings.Split(arg, ",")
if len(split) < 3 {
return nil, fmt.Errorf("Bad parser opts: %s", arg)
}
index, err := strconv.ParseInt(split[0], 0, 64)
if err != nil {
return nil, fmt.Errorf("Bad parser opts: %s", arg)
}
switch split[1] {
case "Time":
if len(split) != 3 {
return nil, fmt.Errorf("Bad parser opts: %s", arg)
}
parsers[int(index)] = TimeParser{Format: split[2]}
default:
return nil, fmt.Errorf("Unknown parser: %s", split[1])
}
}
return parsers, nil
}
type TimeParser struct {
Format string
}
func (t TimeParser) Parse(v string) (float64, error) {
parsed, err := time.Parse(t.Format, v)
if err != nil {
return 0.0, err
}
return float64(parsed.Unix()), nil
}