Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Mar 8, 2023
1 parent ec976a7 commit ce28a39
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
* `[knf]` Code refactoring
* `[options]` Code refactoring
* `[progress]` Code refactoring
* `[req]` Code refactoring
* `[spellcheck]` Code refactoring
* `[usage]` Code refactoring
* `[cache]` Better tests for panics
* `[cron]` Better tests for panics
* `[csv]` Better tests for panics
* `[fmtutil/table]` Better tests for panics
* `[log]` Better tests for panics
* `[progress]` Better tests for panics
* `[req]` Better tests for panics
* `[spellcheck]` Better tests for panics

### 12.60.1

Expand Down
5 changes: 0 additions & 5 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ type passThruWriter struct {

// ////////////////////////////////////////////////////////////////////////////////// //

// ErrNilProgress returns if progress struct is nil
var ErrNilProgress = fmt.Errorf("Progress struct is nil")

// ////////////////////////////////////////////////////////////////////////////////// //

// DefaultSettings is default progress bar settings
var DefaultSettings = Settings{
RefreshRate: 100 * time.Millisecond,
Expand Down
91 changes: 63 additions & 28 deletions req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,20 @@ type Engine struct {
// ////////////////////////////////////////////////////////////////////////////////// //

var (
// ErrEngineIsNil is returned if engine struct is nil
ErrEngineIsNil = RequestError{ERROR_CREATE_REQUEST, "Engine is nil"}
// ErrNilEngine is returned if engine struct is nil
ErrNilEngine = RequestError{ERROR_CREATE_REQUEST, "Engine is nil"}

// ErrClientIsNil is returned if client struct is nil
ErrClientIsNil = RequestError{ERROR_CREATE_REQUEST, "Engine.Client is nil"}
// ErrNilClient is returned if client struct is nil
ErrNilClient = RequestError{ERROR_CREATE_REQUEST, "Engine.Client is nil"}

// ErrTransportIsNil is returned if transport is nil
ErrTransportIsNil = RequestError{ERROR_CREATE_REQUEST, "Engine.Transport is nil"}
// ErrNilTransport is returned if transport is nil
ErrNilTransport = RequestError{ERROR_CREATE_REQUEST, "Engine.Transport is nil"}

// ErrDialerIsNil is returned if dialer is nil
ErrDialerIsNil = RequestError{ERROR_CREATE_REQUEST, "Engine.Dialer is nil"}
// ErrNilDialer is returned if dialer is nil
ErrNilDialer = RequestError{ERROR_CREATE_REQUEST, "Engine.Dialer is nil"}

// ErrNilResponse is returned if response is nil
ErrNilResponse = RequestError{ERROR_CREATE_REQUEST, "Response is nil"}

// ErrEmptyURL is returned if given URL is empty
ErrEmptyURL = RequestError{ERROR_CREATE_REQUEST, "URL property can't be empty and must be set"}
Expand Down Expand Up @@ -374,22 +377,28 @@ func (e *Engine) Delete(r Request) (*Response, error) {

// SetUserAgent sets user agent based on app name and version
func (e *Engine) SetUserAgent(app, version string, subs ...string) {
if e != nil {
e.UserAgent = fmt.Sprintf(
"%s/%s (go; %s; %s-%s)",
app, version, runtime.Version(),
runtime.GOARCH, runtime.GOOS,
)

if len(subs) != 0 {
e.UserAgent += " " + strings.Join(subs, " ")
}
if e == nil {
return
}

e.UserAgent = fmt.Sprintf(
"%s/%s (go; %s; %s-%s)",
app, version, runtime.Version(),
runtime.GOARCH, runtime.GOOS,
)

if len(subs) != 0 {
e.UserAgent += " " + strings.Join(subs, " ")
}
}

// SetDialTimeout sets dial timeout
func (e *Engine) SetDialTimeout(timeout float64) {
if e != nil && timeout > 0 {
if e == nil {
return
}

if timeout > 0 {
if e.Dialer == nil {
e.dialTimeout = timeout
} else {
Expand All @@ -400,7 +409,11 @@ func (e *Engine) SetDialTimeout(timeout float64) {

// SetRequestTimeout sets request timeout
func (e *Engine) SetRequestTimeout(timeout float64) {
if e != nil && timeout > 0 {
if e == nil {
return
}

if timeout > 0 {
if e.Dialer == nil {
e.requestTimeout = timeout
} else {
Expand All @@ -409,6 +422,8 @@ func (e *Engine) SetRequestTimeout(timeout float64) {
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Do sends request and process response
func (r Request) Do() (*Response, error) {
return Global.doRequest(r, "")
Expand Down Expand Up @@ -449,29 +464,49 @@ func (r Request) PostFile(file, fieldName string, extraFields map[string]string)
return Global.PostFile(r, file, fieldName, extraFields)
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Discard reads response body for closing connection
func (r *Response) Discard() {
if r == nil || r.Body == nil {
return
}

io.Copy(io.Discard, r.Body)
}

// JSON decodes json encoded body
func (r *Response) JSON(v any) error {
if r == nil || r.Body == nil {
return ErrNilResponse
}

defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(v)
}

// Bytes reads response body as byte slice
func (r *Response) Bytes() []byte {
if r == nil || r.Body == nil {
return nil
}

defer r.Body.Close()
result, _ := io.ReadAll(r.Body)
return result
}

// String reads response body as string
func (r *Response) String() string {
if r == nil {
return ""
}

return string(r.Bytes())
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Error shows error message
func (e RequestError) Error() string {
switch e.class {
Expand Down Expand Up @@ -530,13 +565,13 @@ func (e *Engine) doRequest(r Request, method string) (*Response, error) {
e.Init()
}

err := checkRequest(r)
err := checkEngine(e)

if err != nil {
return nil, err
}

err = checkEngine(e)
err = checkRequest(r)

if err != nil {
return nil, err
Expand Down Expand Up @@ -599,19 +634,19 @@ func checkRequest(r Request) error {

func checkEngine(e *Engine) error {
if e == nil {
return ErrEngineIsNil
return ErrNilEngine
}

if e.Dialer == nil {
return ErrDialerIsNil
return ErrNilDialer
}

if e.Transport == nil {
return ErrTransportIsNil
return ErrNilTransport
}

if e.Client == nil {
return ErrClientIsNil
return ErrNilClient
}

return nil
Expand Down Expand Up @@ -660,19 +695,19 @@ func configureMultipartRequest(r *Request, file, fieldName string, extraFields m
return err
}

defer fd.Close()

buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
part, err := createFormFile(w, fieldName, file)

if err != nil {
fd.Close()
return err
}

_, err = ioCopyFunc(part, fd)

if err != nil {
fd.Close()
return err
}

Expand Down
21 changes: 21 additions & 0 deletions req/req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,27 @@ func (s *ReqSuite) TestQueryEncoding(c *C) {
c.Assert(qrs, Equals, "a=1&b=abcd&c&d")
}

func (s *ReqSuite) TestNil(c *C) {
var e *Engine

_, err := e.doRequest(Request{}, GET)
c.Assert(err, DeepEquals, ErrNilEngine)

c.Assert(func() { e.SetUserAgent("APP", "1") }, NotPanics)
c.Assert(func() { e.SetDialTimeout(1) }, NotPanics)
c.Assert(func() { e.SetRequestTimeout(1) }, NotPanics)

var r *Response

c.Assert(func() { r.Discard() }, NotPanics)

c.Assert(r.JSON(nil), DeepEquals, ErrNilResponse)
c.Assert(r.Bytes(), IsNil)
c.Assert(r.String(), Equals, "")
}

// ////////////////////////////////////////////////////////////////////////////////// //

func (s *ReqSuite) BenchmarkQueryEncoding(c *C) {
q := Query{"a": 1, "b": "abcd", "c": "", "d": nil}

Expand Down
4 changes: 2 additions & 2 deletions spellcheck/spellcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Train(words []string) *Model {

// Correct corrects given value
func (m *Model) Correct(word string) string {
if len(m.terms) == 0 {
if m == nil || len(m.terms) == 0 {
return word
}

Expand All @@ -99,7 +99,7 @@ func (m *Model) Correct(word string) string {

// Suggest suggests words for given word or word part
func (m *Model) Suggest(word string, max int) []string {
if len(m.terms) == 0 {
if m == nil || len(m.terms) == 0 {
return []string{word}
}

Expand Down
7 changes: 7 additions & 0 deletions spellcheck/spellcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ func (s *SpellcheckSuite) TestSpellcheck(c *C) {
c.Assert(model.Suggest("tes", 3), DeepEquals, []string{"test", "", "TeStInG"})
c.Assert(model.Suggest("tes", 1), DeepEquals, []string{"test"})
}

func (s *SpellcheckSuite) TestNil(c *C) {
var m *Model

c.Assert(m.Correct("test"), Equals, "test")
c.Assert(m.Suggest("test", 1), DeepEquals, []string{"test"})
}

0 comments on commit ce28a39

Please sign in to comment.