-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.go
More file actions
101 lines (83 loc) · 2.21 KB
/
type.go
File metadata and controls
101 lines (83 loc) · 2.21 KB
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
package twse
import (
"context"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/pkg/errors"
)
// ServerResponse is embedded in each Do response and
// provides the HTTP status code and header sent by the server.
type ServerResponse struct {
// HTTPStatusCode is the server's response status code. When using a
// resource method's Do call, this will always be in the 2xx range.
HTTPStatusCode int
// Header contains the response header fields from the server.
Header http.Header
}
// DefaultCall DefaultCall function
type DefaultCall struct {
s *Service
urlParams url.Values
ctx context.Context
header http.Header
}
// Context sets the context to be used in this call's Do method. Any
// pending HTTP request will be aborted if the provided context is
// canceled.
func (c *DefaultCall) Context(ctx context.Context) *DefaultCall {
c.ctx = ctx
return c
}
// Header returns an http.Header that can be modified by the caller to
// add HTTP headers to the request.
func (c *DefaultCall) Header() http.Header {
if c.header == nil {
c.header = make(http.Header)
}
return c.header
}
// Float64 unmarshal string to Float64
type Float64 float64
func (f *Float64) unmarshal(data []byte) error {
s := string(data)
s = strings.ReplaceAll(s, ",", "")
s = strings.ReplaceAll(s, " ", "")
if s == "--" || s == `-` {
*f = 0
return nil
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return errors.Wrapf(err, "strconv.ParseFloat %s", s)
}
*f = Float64(v)
return nil
}
// UnmarshalJSON process Date
func (f *Float64) UnmarshalJSON(data []byte) error {
return f.unmarshal(data)
}
// UnmarshalCSV process Date
func (f *Float64) UnmarshalCSV(data []byte) error {
return f.unmarshal(data)
}
// ListFloat64 unmarshal string to ListInt
type ListFloat64 []float64
// UnmarshalJSON process Date
func (l *ListFloat64) UnmarshalJSON(data []byte) error {
result := []float64{}
str := string(data)
str = strings.ReplaceAll(str, `"`, "")
sList := strings.Split(str, "_")
for _, s := range sList[:len(sList)-1] {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return errors.Wrapf(err, "strconv.ParseFloat %s in %v(%s)", s, sList, str)
}
result = append(result, f)
}
*l = result
return nil
}