-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumeric.go
119 lines (106 loc) · 2.41 KB
/
numeric.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
package csvutil
import (
"fmt"
"io"
"math"
"math/rand"
"strconv"
"github.com/pkg/errors"
)
// NumericOption is option holder for Numeric.
type NumericOption struct {
// Source file does not have header line. (default false)
NoHeader bool
// Encoding of source file. (default utf8)
Encoding string
// Encoding for output.
OutputEncoding string
// Target column symbol.
Column string
// Max value
Max int
// Min value
Min int
// Output decimal instead of integer
Decimal bool
// Digit of decimal
DecimalDigit int
}
func (o NumericOption) validate() error {
if o.Column == "" {
return errors.New("no column")
}
if o.NoHeader {
if !isDigit(o.Column) {
return errors.New("not number column symbol")
}
}
if o.Max <= o.Min {
return errors.New("max should be greater than min")
}
if o.Decimal && o.DecimalDigit <= 0 {
return errors.New("decimal digit is not positive")
}
return nil
}
func (o NumericOption) outputEncoding() string {
if o.OutputEncoding != "" {
return o.OutputEncoding
}
return o.Encoding
}
// Numeric overwrite value of given column by random numbers.
func Numeric(r io.Reader, w io.Writer, o NumericOption) error {
if err := o.validate(); err != nil {
return errors.Wrap(err, "invalid option")
}
cr, bom := reader(r, o.Encoding)
cw := writer(w, bom, o.outputEncoding())
defer cw.Flush()
var col *column
csvp := NewCSVProcessor(cr, cw)
if o.NoHeader {
csvp.SetPreBodyRead(func() error {
col = newColumnWithIndex(o.Column, nil)
return col.err
})
} else {
csvp.SetHeaderHanlder(func(hdr []string) ([]string, error) {
col = newColumnWithIndex(o.Column, hdr)
return hdr, col.err
})
}
csvp.SetRecordHandler(func(rec []string) ([]string, error) {
rec[col.index] = fakeNumeric(o)
return rec, nil
})
return csvp.Process()
}
func fakeNumeric(o NumericOption) string {
if o.Decimal {
return fakeDecimal(o)
}
return fakeInteger(o)
}
func fakeDecimal(o NumericOption) string {
coefficient := int(math.Pow10(o.DecimalDigit))
lim := (o.Max - o.Min) * coefficient
n := rand.Intn(lim) + (o.Min * coefficient)
nega := false
if n < 0 {
nega = true
n = n * -1
}
s := fmt.Sprintf("%0"+strconv.Itoa((o.DecimalDigit+1))+"d", n)
l := len(s)
s = s[:l-o.DecimalDigit] + "." + s[l-o.DecimalDigit:]
if nega {
return "-" + s
}
return s
}
func fakeInteger(o NumericOption) string {
lim := o.Max - o.Min
n := rand.Intn(lim) + o.Min
return strconv.Itoa(n)
}