Skip to content

Commit dde9528

Browse files
authored
fix golint (#263)
1 parent 40a2ef6 commit dde9528

File tree

8 files changed

+96
-62
lines changed

8 files changed

+96
-62
lines changed

elastic/client.go

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package elastic
22

33
import (
44
"bytes"
5+
"crypto/tls"
56
"encoding/json"
67
"fmt"
78
"io/ioutil"
89
"net/http"
910
"net/url"
10-
"crypto/tls"
1111

1212
"github.com/juju/errors"
1313
)
1414

15+
// Client is the client to communicate with ES.
1516
// Although there are many Elasticsearch clients with Go, I still want to implement one by myself.
1617
// Because we only need some very simple usages.
1718
type Client struct {
@@ -23,21 +24,23 @@ type Client struct {
2324
c *http.Client
2425
}
2526

27+
// ClientConfig is the configuration for the client.
2628
type ClientConfig struct {
27-
Https bool
29+
HTTPS bool
2830
Addr string
2931
User string
3032
Password string
3133
}
3234

35+
// NewClient creates the Cient with configuration.
3336
func NewClient(conf *ClientConfig) *Client {
3437
c := new(Client)
3538

3639
c.Addr = conf.Addr
3740
c.User = conf.User
3841
c.Password = conf.Password
3942

40-
if conf.Https {
43+
if conf.HTTPS {
4144
c.Protocol = "https"
4245
tr := &http.Transport{
4346
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -51,6 +54,7 @@ func NewClient(conf *ClientConfig) *Client {
5154
return c
5255
}
5356

57+
// ResponseItem is the ES item in the response.
5458
type ResponseItem struct {
5559
ID string `json:"_id"`
5660
Index string `json:"_index"`
@@ -60,6 +64,7 @@ type ResponseItem struct {
6064
Source map[string]interface{} `json:"_source"`
6165
}
6266

67+
// Response is the ES response
6368
type Response struct {
6469
Code int
6570
ResponseItem
@@ -73,12 +78,13 @@ const (
7378
ActionIndex = "index"
7479
)
7580

81+
// BulkRequest is used to send multi request in batch.
7682
type BulkRequest struct {
77-
Action string
78-
Index string
79-
Type string
80-
ID string
81-
Parent string
83+
Action string
84+
Index string
85+
Type string
86+
ID string
87+
Parent string
8288
Pipeline string
8389

8490
Data map[string]interface{}
@@ -142,6 +148,7 @@ func (r *BulkRequest) bulk(buf *bytes.Buffer) error {
142148
return nil
143149
}
144150

151+
// BulkResponse is the response for the bulk request.
145152
type BulkResponse struct {
146153
Code int
147154
Took int `json:"took"`
@@ -150,6 +157,7 @@ type BulkResponse struct {
150157
Items []map[string]*BulkResponseItem `json:"items"`
151158
}
152159

160+
// BulkResponseItem is the item in the bulk response.
153161
type BulkResponseItem struct {
154162
Index string `json:"_index"`
155163
Type string `json:"_type"`
@@ -160,20 +168,23 @@ type BulkResponseItem struct {
160168
Found bool `json:"found"`
161169
}
162170

171+
// MappingResponse is the response for the mapping request.
163172
type MappingResponse struct {
164-
Code int
173+
Code int
165174
Mapping Mapping
166175
}
167176

177+
// Mapping represents ES mapping.
168178
type Mapping map[string]struct {
169179
Mappings map[string]struct {
170180
Properties map[string]struct {
171-
Type string `json:"type"`
172-
Fields interface{} `json:"fields"`
181+
Type string `json:"type"`
182+
Fields interface{} `json:"fields"`
173183
} `json:"properties"`
174184
} `json:"mappings"`
175185
}
176186

187+
// DoRequest sends a request with body to ES.
177188
func (c *Client) DoRequest(method string, url string, body *bytes.Buffer) (*http.Response, error) {
178189
req, err := http.NewRequest(method, url, body)
179190
req.Header.Add("Content-Type", "application/json")
@@ -188,6 +199,7 @@ func (c *Client) DoRequest(method string, url string, body *bytes.Buffer) (*http
188199
return resp, err
189200
}
190201

202+
// Do sends the request with body to ES.
191203
func (c *Client) Do(method string, url string, body map[string]interface{}) (*Response, error) {
192204
bodyData, err := json.Marshal(body)
193205
if err != nil {
@@ -221,6 +233,7 @@ func (c *Client) Do(method string, url string, body map[string]interface{}) (*Re
221233
return ret, errors.Trace(err)
222234
}
223235

236+
// DoBulk sends the bulk request to the ES.
224237
func (c *Client) DoBulk(url string, items []*BulkRequest) (*BulkResponse, error) {
225238
var buf bytes.Buffer
226239

@@ -252,18 +265,19 @@ func (c *Client) DoBulk(url string, items []*BulkRequest) (*BulkResponse, error)
252265
return ret, errors.Trace(err)
253266
}
254267

268+
// CreateMapping creates a ES mapping.
255269
func (c *Client) CreateMapping(index string, docType string, mapping map[string]interface{}) error {
256-
reqUrl := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
270+
reqURL := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
257271
url.QueryEscape(index))
258272

259-
r, err := c.Do("HEAD", reqUrl, nil)
273+
r, err := c.Do("HEAD", reqURL, nil)
260274
if err != nil {
261275
return errors.Trace(err)
262276
}
263277

264278
// if index doesn't exist, will get 404 not found, create index first
265279
if r.Code == http.StatusNotFound {
266-
_, err = c.Do("PUT", reqUrl, nil)
280+
_, err = c.Do("PUT", reqURL, nil)
267281

268282
if err != nil {
269283
return errors.Trace(err)
@@ -272,20 +286,21 @@ func (c *Client) CreateMapping(index string, docType string, mapping map[string]
272286
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
273287
}
274288

275-
reqUrl = fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
289+
reqURL = fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
276290
url.QueryEscape(index),
277291
url.QueryEscape(docType))
278292

279-
_, err = c.Do("POST", reqUrl, mapping)
293+
_, err = c.Do("POST", reqURL, mapping)
280294
return errors.Trace(err)
281295
}
282296

283-
func (c *Client) GetMapping(index string, docType string) (*MappingResponse, error){
284-
reqUrl := fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
297+
// GetMapping gets the mapping.
298+
func (c *Client) GetMapping(index string, docType string) (*MappingResponse, error) {
299+
reqURL := fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
285300
url.QueryEscape(index),
286301
url.QueryEscape(docType))
287302
buf := bytes.NewBuffer(nil)
288-
resp, err := c.DoRequest("GET", reqUrl, buf)
303+
resp, err := c.DoRequest("GET", reqURL, buf)
289304

290305
if err != nil {
291306
return nil, errors.Trace(err)
@@ -308,100 +323,107 @@ func (c *Client) GetMapping(index string, docType string) (*MappingResponse, err
308323
return ret, errors.Trace(err)
309324
}
310325

326+
// DeleteIndex deletes the index.
311327
func (c *Client) DeleteIndex(index string) error {
312-
reqUrl := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
328+
reqURL := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
313329
url.QueryEscape(index))
314330

315-
r, err := c.Do("DELETE", reqUrl, nil)
331+
r, err := c.Do("DELETE", reqURL, nil)
316332
if err != nil {
317333
return errors.Trace(err)
318334
}
319335

320336
if r.Code == http.StatusOK || r.Code == http.StatusNotFound {
321337
return nil
322-
} else {
323-
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
324338
}
339+
340+
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
325341
}
326342

343+
// Get gets the item by id.
327344
func (c *Client) Get(index string, docType string, id string) (*Response, error) {
328-
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
345+
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
329346
url.QueryEscape(index),
330347
url.QueryEscape(docType),
331348
url.QueryEscape(id))
332349

333-
return c.Do("GET", reqUrl, nil)
350+
return c.Do("GET", reqURL, nil)
334351
}
335352

336-
// Can use Update to create or update the data
353+
// Update creates or updates the data
337354
func (c *Client) Update(index string, docType string, id string, data map[string]interface{}) error {
338-
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
355+
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
339356
url.QueryEscape(index),
340357
url.QueryEscape(docType),
341358
url.QueryEscape(id))
342359

343-
r, err := c.Do("PUT", reqUrl, data)
360+
r, err := c.Do("PUT", reqURL, data)
344361
if err != nil {
345362
return errors.Trace(err)
346363
}
347364

348365
if r.Code == http.StatusOK || r.Code == http.StatusCreated {
349366
return nil
350-
} else {
351-
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
352367
}
368+
369+
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
353370
}
354371

372+
// Exists checks whether id exists or not.
355373
func (c *Client) Exists(index string, docType string, id string) (bool, error) {
356-
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
374+
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
357375
url.QueryEscape(index),
358376
url.QueryEscape(docType),
359377
url.QueryEscape(id))
360378

361-
r, err := c.Do("HEAD", reqUrl, nil)
379+
r, err := c.Do("HEAD", reqURL, nil)
362380
if err != nil {
363381
return false, err
364382
}
365383

366384
return r.Code == http.StatusOK, nil
367385
}
368386

387+
// Delete deletes the item by id.
369388
func (c *Client) Delete(index string, docType string, id string) error {
370-
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
389+
reqURL := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
371390
url.QueryEscape(index),
372391
url.QueryEscape(docType),
373392
url.QueryEscape(id))
374393

375-
r, err := c.Do("DELETE", reqUrl, nil)
394+
r, err := c.Do("DELETE", reqURL, nil)
376395
if err != nil {
377396
return errors.Trace(err)
378397
}
379398

380399
if r.Code == http.StatusOK || r.Code == http.StatusNotFound {
381400
return nil
382-
} else {
383-
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
384401
}
402+
403+
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
385404
}
386405

406+
// Bulk sends the bulk request.
387407
// only support parent in 'Bulk' related apis
388408
func (c *Client) Bulk(items []*BulkRequest) (*BulkResponse, error) {
389-
reqUrl := fmt.Sprintf("%s://%s/_bulk", c.Protocol, c.Addr)
409+
reqURL := fmt.Sprintf("%s://%s/_bulk", c.Protocol, c.Addr)
390410

391-
return c.DoBulk(reqUrl, items)
411+
return c.DoBulk(reqURL, items)
392412
}
393413

414+
// IndexBulk sends the bulk request for index.
394415
func (c *Client) IndexBulk(index string, items []*BulkRequest) (*BulkResponse, error) {
395-
reqUrl := fmt.Sprintf("%s://%s/%s/_bulk", c.Protocol, c.Addr,
416+
reqURL := fmt.Sprintf("%s://%s/%s/_bulk", c.Protocol, c.Addr,
396417
url.QueryEscape(index))
397418

398-
return c.DoBulk(reqUrl, items)
419+
return c.DoBulk(reqURL, items)
399420
}
400421

422+
// IndexTypeBulk sends the bulk request for index and doc type.
401423
func (c *Client) IndexTypeBulk(index string, docType string, items []*BulkRequest) (*BulkResponse, error) {
402-
reqUrl := fmt.Sprintf("%s://%s/%s/%s/_bulk", c.Protocol, c.Addr,
424+
reqURL := fmt.Sprintf("%s://%s/%s/%s/_bulk", c.Protocol, c.Addr,
403425
url.QueryEscape(index),
404426
url.QueryEscape(docType))
405427

406-
return c.DoBulk(reqUrl, items)
428+
return c.DoBulk(reqURL, items)
407429
}

river/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"github.com/juju/errors"
99
)
1010

11+
// SourceConfig is the configs for source
1112
type SourceConfig struct {
1213
Schema string `toml:"schema"`
1314
Tables []string `toml:"tables"`
1415
}
1516

17+
// Config is the configuration
1618
type Config struct {
1719
MyAddr string `toml:"my_addr"`
1820
MyUser string `toml:"my_user"`
@@ -44,6 +46,7 @@ type Config struct {
4446
SkipNoPkTable bool `toml:"skip_no_pk_table"`
4547
}
4648

49+
// NewConfigWithFile creates a Config from file.
4750
func NewConfigWithFile(name string) (*Config, error) {
4851
data, err := ioutil.ReadFile(name)
4952
if err != nil {
@@ -53,6 +56,7 @@ func NewConfigWithFile(name string) (*Config, error) {
5356
return NewConfig(string(data))
5457
}
5558

59+
// NewConfig creates a Config from data.
5660
func NewConfig(data string) (*Config, error) {
5761
var c Config
5862

@@ -64,10 +68,12 @@ func NewConfig(data string) (*Config, error) {
6468
return &c, nil
6569
}
6670

71+
// TomlDuration supports time codec for TOML format.
6772
type TomlDuration struct {
6873
time.Duration
6974
}
7075

76+
// UnmarshalText implementes TOML UnmarshalText
7177
func (d *TomlDuration) UnmarshalText(text []byte) error {
7278
var err error
7379
d.Duration, err = time.ParseDuration(string(text))

river/master.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func (m *masterInfo) Position() mysql.Position {
8787
defer m.RUnlock()
8888

8989
return mysql.Position{
90-
m.Name,
91-
m.Pos,
90+
Name: m.Name,
91+
Pos: m.Pos,
9292
}
9393
}
9494

0 commit comments

Comments
 (0)