/
value.go
169 lines (143 loc) · 3.9 KB
/
value.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rule
import (
"errors"
"fmt"
"github.com/plandem/xlsx/format/styles"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/internal/ml/primitives"
"github.com/plandem/xlsx/internal/number_format/convert"
//"reflect"
"strconv"
"time"
)
type valueRule struct {
baseRule
}
//Value is helper object to set specific options for rule
var Value valueRule
func (x valueRule) initIfRequired(r *Info) {
if !r.initialized {
r.initialized = true
r.validator = Value
r.rule = &ml.ConditionalRule{
Type: primitives.ConditionTypeCellIs,
}
}
}
func (x valueRule) fromInt(value int) string {
return strconv.FormatInt(int64(value), 10)
}
func (x valueRule) fromUInt(value uint) string {
return strconv.FormatUint(uint64(value), 10)
}
func (x valueRule) fromFloat(value float64) string {
return strconv.FormatFloat(value, 'f', -1, 64)
}
func (x valueRule) fromBool(value bool) string {
if value {
return "1"
}
return "0"
}
//nolint
func (x valueRule) setValue(r *Info, values []interface{}, operator primitives.ConditionOperatorType, s *styles.Info) {
x.initIfRequired(r)
r.rule.Operator = operator
for _, v := range values {
var s string
switch value := v.(type) {
case int:
s = x.fromInt(value)
case int8:
s = x.fromInt(int(value))
case int16:
s = x.fromInt(int(value))
case int32:
s = x.fromInt(int(value))
case int64:
s = x.fromInt(int(value))
case uint:
s = x.fromUInt(value)
case uint8:
s = x.fromUInt(uint(value))
case uint16:
s = x.fromUInt(uint(value))
case uint32:
s = x.fromUInt(uint(value))
case uint64:
s = x.fromUInt(uint(value))
case float32:
s = x.fromFloat(float64(value))
case float64:
s = x.fromFloat(float64(value))
case []byte:
s = string(value)
case bool:
s = x.fromBool(value)
case time.Time:
s = value.Format(convert.ISO8601)
case string:
s = value
default:
s = fmt.Sprintf("%v", value)
}
//if value has '=', then remove it
if len(s) > 0 && s[0] == '=' {
s = s[1:]
}
if len(s) > 0 {
r.rule.Formula = append(r.rule.Formula, ml.Formula(s))
}
}
r.style = s
}
func (x valueRule) Between(from, to interface{}, s *styles.Info) Option {
return func(r *Info) {
x.initIfRequired(r)
x.setValue(r, []interface{}{from, to}, primitives.ConditionOperatorBetween, s)
}
}
func (x valueRule) NotBetween(from, to interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{from, to}, primitives.ConditionOperatorNotBetween, s)
}
}
func (x valueRule) Equal(value interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{value}, primitives.ConditionOperatorEqual, s)
}
}
func (x valueRule) NotEqual(value interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{value}, primitives.ConditionOperatorNotEqual, s)
}
}
func (x valueRule) Greater(value interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{value}, primitives.ConditionOperatorGreaterThan, s)
}
}
func (x valueRule) Less(value interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{value}, primitives.ConditionOperatorLessThan, s)
}
}
func (x valueRule) GreaterOrEqual(value interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{value}, primitives.ConditionOperatorGreaterThanOrEqual, s)
}
}
func (x valueRule) LessOrEqual(value interface{}, s *styles.Info) Option {
return func(r *Info) {
x.setValue(r, []interface{}{value}, primitives.ConditionOperatorLessThanOrEqual, s)
}
}
func (x valueRule) Validate(r *Info) error {
if len(r.rule.Formula) == 0 || len(r.rule.Formula[0]) == 0 {
return errors.New("value: no criteria or value for rule")
}
return nil
}