This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypes.go
102 lines (90 loc) · 1.85 KB
/
types.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
package manifest
import (
"bytes"
"io"
)
//BufWrapper is a wrapper type for bytes.Buffer.
type BufWrapper struct {
Buf *bytes.Buffer
Err error
}
//NewBufWrapper returns an instance of BufWrapper.
func NewBufWrapper() *BufWrapper {
return &BufWrapper{
Buf: new(bytes.Buffer),
Err: nil,
}
}
//WriteValidString receives an interface and performs a buffer.Write if data is set.
//It returns true if value is set, and false if it isn't.
func (b *BufWrapper) WriteValidString(data interface{}, write string) bool {
if b.Err != nil {
return false
}
switch data.(type) {
case string:
if data.(string) != "" {
_, b.Err = b.Buf.WriteString(write)
return true
}
case float64:
if data.(float64) > float64(0) {
_, b.Err = b.Buf.WriteString(write)
return true
}
case int64:
if data.(int64) > 0 {
_, b.Err = b.Buf.WriteString(write)
return true
}
case int:
if data.(int) > 0 {
_, b.Err = b.Buf.WriteString(write)
return true
}
case bool:
if data.(bool) {
_, b.Err = b.Buf.WriteString(write)
return true
}
}
return false
}
//WriteString wraps buffer.WriteString
func (b *BufWrapper) WriteString(s string) {
if b.Err != nil {
return
}
_, b.Err = b.Buf.WriteString(s)
}
//WriteRune wraps buffer.WriteRune
func (b *BufWrapper) WriteRune(r rune) {
if b.Err != nil {
return
}
_, b.Err = b.Buf.WriteRune(r)
}
//Write wraps buffer.Write
func (b *BufWrapper) Write(p []byte) {
if b.Err != nil {
return
}
_, b.Err = b.Buf.Write(p)
}
// ReadFrom wraps buffer.ReadFrom
func (b *BufWrapper) ReadFrom(r io.Reader) (int64, error) {
if b.Err != nil {
return 0, b.Err
}
var count int64
count, b.Err = b.Buf.ReadFrom(r)
return count, b.Err
}
// ReadString wraps buffer.ReadString
func (b *BufWrapper) ReadString(delim byte) (line string) {
if b.Err != nil {
return
}
line, b.Err = b.Buf.ReadString(delim)
return
}