-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproperty.go
133 lines (113 loc) · 2.53 KB
/
property.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package hpcmodel
import (
"fmt"
"strconv"
"time"
)
type Property interface {
SetValue(value string)
SetCategory(category QueryCategory)
PropertyID() string
}
func BuildProperty(records []string, category QueryCategory) (Property, error) {
var p Property
switch category.PropertyClass {
case "StringProperty":
p = &StringProperty{}
case "NumberProperty":
p = &NumberProperty{}
case "DateTimeProperty":
p = &DateTimeProperty{}
case "TimestampProperty":
p = &TimestampProperty{}
default:
return nil, fmt.Errorf("error PropertyClass: %s", category.PropertyClass)
}
p.SetCategory(category)
value := category.ParseRecord.Parse(records)
p.SetValue(value)
return p, nil
}
type StringProperty struct {
Category QueryCategory
Value string
Text string
Data string
}
func (p *StringProperty) SetValue(value string) {
p.Value = value
p.Text = value
p.Data = value
}
func (p *StringProperty) SetCategory(category QueryCategory) {
p.Category = category
}
func (p *StringProperty) PropertyID() string {
return p.Category.ID
}
type NumberProperty struct {
Category QueryCategory
Value string
Text string
Data float64
}
func (p *NumberProperty) SetValue(value string) {
data, err := strconv.ParseFloat(value, 64)
if err != nil {
panic(err)
}
p.Value = value
p.Text = value
p.Data = data
}
func (p *NumberProperty) SetCategory(category QueryCategory) {
p.Category = category
}
func (p *NumberProperty) PropertyID() string {
return p.Category.ID
}
type DateTimeProperty struct {
Category QueryCategory
TimeFormat string
Value string
Text string
Data time.Time
}
func (p *DateTimeProperty) SetValue(value string) {
const timeFormat = "2006-01-02T15:04:05"
data, err := time.Parse(timeFormat, value)
if err != nil {
panic(err)
}
p.Value = value
p.Text = data.Format("2006-01-02 15:04:05")
p.Data = data
}
func (p *DateTimeProperty) SetCategory(category QueryCategory) {
p.Category = category
}
func (p *DateTimeProperty) PropertyID() string {
return p.Category.ID
}
type TimestampProperty struct {
Category QueryCategory
Value string
Text string
Data time.Time
}
func (p *TimestampProperty) SetValue(value string) {
f, err := strconv.ParseInt(value, 10, 64)
if err != nil {
panic(err)
}
data := time.Unix(f, 0).UTC()
p.Value = value
p.Text = data.Format("2006-01-02 15:04:05")
p.Data = data
}
func (p *TimestampProperty) SetCategory(category QueryCategory) {
p.Category = category
}
func (p *TimestampProperty) PropertyID() string {
return p.Category.ID
}