-
Notifications
You must be signed in to change notification settings - Fork 67
/
binary.go
58 lines (50 loc) · 955 Bytes
/
binary.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
package getters
import (
"fmt"
"math/rand"
"github.com/icrowley/fake"
)
// RandomBinary getter
type RandomBinary struct {
name string
maxSize int64
allowNull bool
}
func (r *RandomBinary) Value() interface{} {
if r.allowNull && rand.Int63n(100) < nilFrequency {
return nil
}
var s string
maxSize := uint64(r.maxSize)
if maxSize == 0 {
maxSize = uint64(rand.Int63n(100))
}
if maxSize <= 10 {
s = fake.FirstName()
} else if maxSize < 30 {
s = fake.FullName()
} else {
s = fake.Sentence()
}
if len(s) > int(maxSize) {
s = s[:int(maxSize)]
}
return s
}
func (r *RandomBinary) String() string {
v := r.Value()
if v == nil {
return NULL
}
return v.(string)
}
func (r *RandomBinary) Quote() string {
v := r.Value()
if v == nil {
return NULL
}
return fmt.Sprintf("%q", v)
}
func NewRandomBinary(name string, maxSize int64, allowNull bool) *RandomBinary {
return &RandomBinary{name, maxSize, allowNull}
}