-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.go
239 lines (205 loc) · 5.13 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2019 James Cote
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package intacct
import (
"encoding/xml"
"strconv"
"strings"
"time"
)
// Date used to handle intact read and readQuery date format
type Date struct {
t *time.Time
}
// IsNil returns whether the underlying time is nil
func (dx Date) IsNil() bool {
return dx.t == nil || dx.t.IsZero()
}
// TimeToDate converts a time.Time pointer to an intacct.Date pointer
func TimeToDate(t time.Time) Date {
if !t.IsZero() {
return Date{t: &t}
}
return Date{}
}
// Val returns intacct date at *Time.time. Blanks returned as nil
func (dx Date) Val() *time.Time {
if dx.IsNil() {
return nil
}
return dx.t
}
// String returns the date in YYYY-MM-DD format
func (dx Date) String() string {
if dx.IsNil() {
return ""
}
return dx.t.Format("2006-01-02")
}
// MarshalText formats Date for xml and json
func (dx Date) MarshalText() ([]byte, error) {
if dx.IsNil() {
return nil, nil
}
return []byte(dx.t.Format("2006-01-02")), nil
}
// UnmarshalText parses string form Date
func (dx *Date) UnmarshalText(text []byte) error {
if dx == nil {
return nil
}
if len(text) == 0 {
dx.t = nil
return nil
}
s := string(text)
parseLayout := "01/02/2006"
if strings.Count(s, "/") == 0 {
parseLayout = "2006-01-02"
}
if len(s) > len(parseLayout) {
s = s[:len(parseLayout)]
}
t, err := time.Parse(parseLayout, s)
if err != nil {
return err
}
dx.t = &t
return nil
}
// Datetime used to handle intact read and readQuery date format
type Datetime Date
// TimeToDatetime converts a time.Time pointer to an intacct.Datetime pointer
func TimeToDatetime(t time.Time) Datetime {
dx := TimeToDate(t)
return Datetime(dx)
}
// IsNil returns whether the underlying time is nil
func (dt Datetime) IsNil() bool {
return dt.t == nil || dt.t.IsZero()
}
// Val returns intacct datetime.
func (dt Datetime) Val() *time.Time {
return Date(dt).Val()
}
// String returns an RC3339 output of the date
func (dt Datetime) String() string {
if dt.IsNil() {
return ""
}
return dt.t.Format(time.RFC3339)
}
// MarshalText formats Date for xml and json
func (dt Datetime) MarshalText() ([]byte, error) {
return []byte(dt.String()), nil
}
// UnmarshalText parses string form Date
func (dt *Datetime) UnmarshalText(text []byte) error {
if dt == nil {
return nil
}
if len(text) == 0 {
dt.t = nil
return nil
}
s := string(text)
if strings.Count(s, "/") > 1 {
return dt.handleNotRFC3339(s)
}
return dt.handleRFC3339(s)
}
func (dt *Datetime) handleNotRFC3339(s string) error {
parseLayout := "01/02/2006 15:04:05"
if len(s) == 10 {
parseLayout = "01/02/2006"
}
t, err := time.Parse(parseLayout, s)
if err == nil {
dt.t = &t
}
return err
}
func (dt *Datetime) handleRFC3339(s string) error {
parseLayout := time.RFC3339
if len(s) == 10 {
parseLayout = "2006-01-02"
}
t, err := time.Parse(parseLayout, s)
if err == nil {
dt.t = &t
}
return err
}
// Float64 handles intacct xml float values
type Float64 float64
// Int handles intacct xml int values
type Int int64
// Bool handles intacct xml bool values
type Bool bool
// Val returns 0 for blank
func (f Float64) Val() float64 {
return float64(f)
}
// Val returns 0 for blank
func (f Float64) String() string {
return strconv.FormatFloat(float64(f), 'f', -1, 64)
}
// Val returns 0 for blank
func (i Int) Val() int64 {
return int64(i)
}
func (i Int) String() string {
return strconv.FormatInt(int64(i), 10)
}
// Val checks for default true values, false for all others
func (b Bool) Val() bool {
return bool(b)
}
// UnmarshalXML decodes float values and sets value to 0 on any parse errors
func (f *Float64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
_ = d.DecodeElement(&s, &start)
if val, err := strconv.ParseFloat(s, 64); err == nil {
*f = Float64(val)
}
return nil
}
// UnmarshalXML decodes int values and sets value to 0 on any parse errors
func (i *Int) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
_ = d.DecodeElement(&s, &start)
if val, err := strconv.ParseInt(s, 10, 64); err == nil {
*i = Int(val)
}
return nil
}
// UnmarshalXML decodes bool values and sets value to false on any parse errors
func (b *Bool) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
_ = d.DecodeElement(&s, &start)
if val, err := strconv.ParseBool(s); err == nil {
*b = Bool(val)
}
return nil
}
// CustomField provides a key/pair structure for marshalling and
// unmarshalling custom fields for an Intacct object
type CustomField struct {
Name string
Value string
}
// MarshalXML serializes a custom field into <NAME>VALUE</NAME>
func (c CustomField) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(c.Value, xml.StartElement{Name: xml.Name{Local: c.Name}, Attr: start.Attr})
}
// UnmarshalXML decodes unreference xml tags into a CustomField Slice
func (c *CustomField) UnmarshalXML(d *xml.Decoder, s xml.StartElement) error {
var val string
if err := d.DecodeElement(&val, &s); err != nil {
return err
}
*c = CustomField{s.Name.Local, val}
return nil
}