forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.go
65 lines (56 loc) · 1.33 KB
/
top.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
// 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 (
"fmt"
"github.com/miscoler/xlsx/format/styles"
"github.com/miscoler/xlsx/internal/ml"
"github.com/miscoler/xlsx/internal/ml/primitives"
)
type topRule struct {
baseRule
}
//Top is helper object to set specific options for rule
var Top topRule
func (x topRule) initIfRequired(r *Info) {
if !r.initialized {
r.initialized = true
r.validator = Top
r.rule = &ml.ConditionalRule{
Type: primitives.ConditionTypeTop10,
Rank: 10,
}
}
}
func (x topRule) Default(r *Info) {
x.initIfRequired(r)
}
func (x topRule) Value(rank uint, settings ...interface{}) Option {
return func(r *Info) {
x.initIfRequired(r)
r.rule.Rank = rank
for _, p := range settings {
switch pv := p.(type) {
case string:
if pv == "%" {
r.rule.Percent = true
}
case *styles.Info:
r.style = pv
}
}
}
}
func (x topRule) Validate(r *Info) error {
if r.rule.Percent {
if r.rule.Rank < 1 || r.rule.Rank > 100 {
return fmt.Errorf("top: value(%d) should be between (1 - 100)", r.rule.Rank)
}
} else {
if r.rule.Rank < 1 || r.rule.Rank > 1000 {
return fmt.Errorf("top: value(%d) should be between 1 and 1000", r.rule.Rank)
}
}
return nil
}