-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcell.go
151 lines (125 loc) · 3.46 KB
/
cell.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
package goSmartSheet
import (
"encoding/json"
"strconv"
)
//Cell is a SmartSheet cell
type Cell struct {
ColumnID int64 `json:"columnId"`
Value *CellValue `json:"value,omitempty"` //TODO: should this be a pointer?
DisplayValue string `json:"displayValue,omitempty"`
}
//CellValue represents the possible strongly typed values that could exist in a SS cell
//another good article on it..
//http://attilaolah.eu/2013/11/29/json-decoding-in-go/
type CellValue struct {
Value json.RawMessage
StringVal *string
IntVal *int
FloatVal *float64
}
//StringDebug returns a debug string containing each of the underlying values of a Cell
func (c *CellValue) StringDebug() (val string) {
delim := ""
if c.StringVal != nil {
val = "String Val: '" + *(c.StringVal) + "'"
delim = " "
}
if c.IntVal != nil {
val = val + delim + "Int Val: '" + strconv.Itoa(*c.IntVal) + "'"
delim = " "
}
if c.FloatVal != nil {
val = val + delim + "Float Val: '" + strconv.FormatFloat(*c.FloatVal, 'f', -1, 64) + "'"
delim = " "
}
return
}
//String returns the underlying value as a string regardless of type
func (c *CellValue) String() (val string) {
if c.StringVal != nil {
val = *(c.StringVal)
return
}
if c.IntVal != nil {
val = strconv.Itoa(*c.IntVal)
return
}
if c.FloatVal != nil {
val = strconv.FormatFloat(*c.FloatVal, 'f', -1, 64) //-1 will remove unimportant 0s
return
}
val = string(c.Value)
return
}
//Int will return the Integer representation of the underlying value. This should only be used if the value is known to be an Int
func (c *CellValue) Int() (val int) {
if c.IntVal != nil {
val = (*(c.IntVal))
}
return
}
//Float will return the Float representation of the underlying value. This should only be used if the value is known to be an Float.
func (c *CellValue) Float() (val float64) {
if c.FloatVal != nil {
val = (*(c.FloatVal))
}
return
}
//SetString will clear all values and set only the string
//This should be used when updating an existing row especially if the type if changing
func (c *CellValue) SetString(v string) {
c.IntVal = nil
c.FloatVal = nil
c.StringVal = &v
}
//SetInt will clear all values and set only the string
//This should be used when updating an existing row especially if the type if changing
func (c *CellValue) SetInt(v int) {
c.IntVal = &v
c.FloatVal = nil
c.StringVal = nil
}
//SetFloat will clear all values and set only the string
//This should be used when updating an existing row especially if the type if changing
func (c *CellValue) SetFloat(v float64) {
c.IntVal = nil
c.FloatVal = &v
c.StringVal = nil
}
//MarshalJSON is a custom marshaller for CellValue
func (c *CellValue) MarshalJSON() ([]byte, error) {
if c.StringVal != nil {
return json.Marshal(c.StringVal)
}
if c.IntVal != nil {
return json.Marshal(c.IntVal)
}
if c.FloatVal != nil {
return json.Marshal(c.FloatVal)
}
return json.Marshal(c.Value) //default raw message
}
//UnmarshalJSON is a custom unmarshaller for CellValue
func (c *CellValue) UnmarshalJSON(b []byte) (err error) {
c.StringVal = nil
c.IntVal = nil
c.FloatVal = nil
//errors unmarshalling to the corrsponding types should not bubble up
s := ""
if e := json.Unmarshal(b, &s); e == nil {
c.StringVal = &s
return
}
var i int
if e := json.Unmarshal(b, &i); e == nil {
c.IntVal = &i
return
}
var f float64
if e := json.Unmarshal(b, &f); e == nil {
c.FloatVal = &f
}
c.Value = json.RawMessage(b) //default to raw message
return
}