-
Notifications
You must be signed in to change notification settings - Fork 2
/
option.go
65 lines (57 loc) · 1.33 KB
/
option.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
package wkhtmltoimage
import (
"fmt"
"strconv"
)
type optType int
const (
optTypeString optType = iota + 1
optTypeBool
optTypeInt
optTypeUint
optTypeFloat
)
type setterFunc func(name, value string) error
type setOp struct {
name string
value interface{}
typ optType
setter setterFunc
setEmpty bool
}
func newSetOp(name string, value interface{}, typ optType, setter setterFunc, setEmpty bool) *setOp {
return &setOp{
name: name,
value: value,
typ: typ,
setter: setter,
setEmpty: setEmpty,
}
}
func (op *setOp) execute() error {
switch op.typ {
case optTypeString:
if val := op.value.(string); op.setEmpty || val != "" {
return op.setter(op.name, val)
}
case optTypeBool:
if val := op.value.(bool); op.setEmpty || val {
return op.setter(op.name, strconv.FormatBool(val))
}
case optTypeInt:
if val := op.value.(int64); op.setEmpty || val > 0 {
return op.setter(op.name, strconv.FormatInt(val, 10))
}
case optTypeUint:
if val := op.value.(uint64); op.setEmpty || val > 0 {
return op.setter(op.name, strconv.FormatUint(val, 10))
}
case optTypeFloat:
if val := op.value.(float64); op.setEmpty || uint64(val) > 0 {
return op.setter(op.name, strconv.FormatFloat(val, 'E', -1, 64))
}
default:
return fmt.Errorf("invalid option type: %d", op.typ)
}
return nil
}