@@ -10,10 +10,16 @@ import (
1010 "strings"
1111)
1212
13+ // UpsertResponse is the response from the either Update or Insert/add operation
14+ type UpsertResponse struct {
15+ UpdatedRange string `json:"updatedRange"`
16+ }
17+
18+ // GetParams is the parameters for the read operations: Get or search
1319type GetParams struct {
1420 Offset int64
1521 Limit int64
16- Search map [string ]string
22+ Condition map [string ]string
1723}
1824
1925// builds the query string from the given params with query string escaping
@@ -27,8 +33,8 @@ func (gp GetParams) queryString() string {
2733 queryString = queryString + fmt .Sprintf ("limit=%d&" , gp .Limit )
2834 }
2935
30- if len (gp .Search ) > 0 {
31- jsonSearch , err := json .Marshal (gp .Search )
36+ if len (gp .Condition ) > 0 {
37+ jsonSearch , err := json .Marshal (gp .Condition )
3238 if err != nil {
3339 return ""
3440 }
@@ -41,21 +47,25 @@ func (gp GetParams) queryString() string {
4147 return removeSuffix (queryString , "&" )
4248}
4349
44- type WriteResponse struct {
45- UpdatedRange string `json:"updatedRange"`
46- }
47-
50+ // UpdateParams is the parameters for the update operation
4851type UpdateParams struct {
4952 Condition map [string ]string `json:"condition"`
5053 Set map [string ]string `json:"set"`
5154 Limit int64 `json:"limit,omitempty"`
5255}
5356
57+ // DeleteParams is the parameters for the delete operation
58+ type DeleteParams struct {
59+ Condition map [string ]string `json:"condition"`
60+ Limit int64 `json:"limit,omitempty"`
61+ }
62+
5463// Interface is the interface for the stein client
5564type Interface interface {
5665 Get (sheet string , params GetParams ) ([]map [string ]interface {}, error )
57- Add (sheet string , rows ... map [string ]interface {}) (WriteResponse , error )
58- Update (sheet string , params UpdateParams ) (WriteResponse , error )
66+ Add (sheet string , rows ... map [string ]interface {}) (UpsertResponse , error )
67+ Update (sheet string , params UpdateParams ) (UpsertResponse , error )
68+ Delete (sheet string , params DeleteParams ) (countDeletedRows int64 , err error )
5969}
6070
6171type stein struct {
@@ -113,9 +123,9 @@ func (s *stein) Get(sheet string, params GetParams) ([]map[string]interface{}, e
113123 return data , nil
114124}
115125
116- func (s * stein ) Add (sheet string , rows ... map [string ]interface {}) (WriteResponse , error ) {
126+ func (s * stein ) Add (sheet string , rows ... map [string ]interface {}) (UpsertResponse , error ) {
117127 var (
118- result WriteResponse
128+ result UpsertResponse
119129 resource = fmt .Sprintf ("%s/%s" , s .url , removePrefix (sheet , "/" ))
120130 )
121131
@@ -143,9 +153,9 @@ func (s *stein) Add(sheet string, rows ...map[string]interface{}) (WriteResponse
143153 return result , nil
144154}
145155
146- func (s * stein ) Update (sheet string , params UpdateParams ) (WriteResponse , error ) {
156+ func (s * stein ) Update (sheet string , params UpdateParams ) (UpsertResponse , error ) {
147157 var (
148- result WriteResponse
158+ result UpsertResponse
149159 resource = fmt .Sprintf ("%s/%s" , s .url , removePrefix (sheet , "/" ))
150160 )
151161
@@ -172,8 +182,46 @@ func (s *stein) Update(sheet string, params UpdateParams) (WriteResponse, error)
172182
173183 err = s .decodeJSON (resp .Body , & result )
174184 if err != nil {
175- return WriteResponse {}, ErrDecodeJSON {Err : err }
185+ return UpsertResponse {}, ErrDecodeJSON {Err : err }
176186 }
177187
178188 return result , nil
179189}
190+
191+ func (s * stein ) Delete (sheet string , params DeleteParams ) (countDeletedRows int64 , err error ) {
192+ var (
193+ result = struct {
194+ CountDeletedRows int64 `json:"clearedRowsCount"`
195+ }{}
196+
197+ resource = fmt .Sprintf ("%s/%s" , s .url , removePrefix (sheet , "/" ))
198+ )
199+
200+ payload , err := json .Marshal (params )
201+ if err != nil {
202+ return countDeletedRows , err
203+ }
204+
205+ req , err := http .NewRequest (http .MethodDelete , resource , bytes .NewBuffer (payload ))
206+ if err != nil {
207+ return countDeletedRows , err
208+ }
209+ req .Header .Set ("Content-Type" , "application/json" )
210+
211+ resp , err := s .httpClient .Do (req )
212+ if err != nil {
213+ return countDeletedRows , err
214+ }
215+ defer resp .Body .Close ()
216+
217+ if resp .StatusCode != http .StatusOK {
218+ return countDeletedRows , ErrNot2XX {StatusCode : resp .StatusCode }
219+ }
220+
221+ err = s .decodeJSON (resp .Body , & result )
222+ if err != nil {
223+ return countDeletedRows , ErrDecodeJSON {Err : err }
224+ }
225+
226+ return result .CountDeletedRows , nil
227+ }
0 commit comments