Skip to content

Commit

Permalink
Update xutil
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Sep 11, 2023
1 parent 590a304 commit 84a0b9f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ go get github.com/mix-go/xutil
| Function | Description |
|----------------------------------------------------------------------------------|----------------------------------|
| xhttp.Request(method string, u string, opts ...RequestOption) (*Response, error) | Execute an http request. |
| xhttp.WithBody(body Body) RequestOption | Set configuration item |
| WithHeader(header http.Header) RequestOption | Set configuration item |
| WithContentType(contentType string) RequestOption | Set configuration item |
| WithTimeout(timeout time.Duration) RequestOption | Set configuration item |
| xhttp.BuildJSON(v interface{}) Body | Generate json string |
| xhttp.BuildQuery(m map[string]string) Body | Generate urlencoded query string |

Expand All @@ -33,9 +37,11 @@ go get github.com/mix-go/xutil

## xconv

| Function | Description |
|---------------------------------------------------------|------------------------|
| xconv.StructToMap(i interface{}) map[string]interface{} | Convert struct to map. |
| Function | Description |
|---------------------------------------------------------|-----------------------------------|
| xconv.StructToMap(i interface{}) map[string]interface{} | Convert struct to map. |
| xconv.StringToBytes(s string) []byte | Convert string to bytes (0 copy). |
| xconv.BytesToString(b []byte) string | Convert bytes to bytes (0 copy). |

## xcrypt

Expand Down
16 changes: 16 additions & 0 deletions xconv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ package xconv
import (
"reflect"
"strings"
"unsafe"
)

// StringToBytes converts string to byte slice without a memory allocation.
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}

// BytesToString converts byte slice to string without a memory allocation.
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

// StructToMap Convert struct to map.
// This function first tries to use the bson tag, and if the bson tag does not exist, it will use the json tag.
// if both bson and json tags do not exist, then it will use the field name as the key. Additionally,
Expand Down
9 changes: 8 additions & 1 deletion xhttp/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xhttp

import (
"github.com/mix-go/xutil/xconv"
"net/http"
"time"
)
Expand Down Expand Up @@ -67,8 +68,14 @@ func WithBody(body Body) RequestOption {
}}
}

func WithBodyBytes(body []byte) RequestOption {
return &funcRequestOption{func(opt *requestOptions) {
opt.Body = body
}}
}

func WithBodyString(body string) RequestOption {
return &funcRequestOption{func(opt *requestOptions) {
opt.Body = Body(body)
opt.Body = xconv.StringToBytes(body)
}}
}
7 changes: 5 additions & 2 deletions xhttp/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xhttp

import (
"github.com/mix-go/xutil/xconv"
"io"
"net/http"
"net/url"
Expand All @@ -15,7 +16,7 @@ type Response struct {
type Body []byte

func (t Body) String() string {
return string(t)
return xconv.BytesToString(t)
}

func newResponse(r *http.Response) *Response {
Expand Down Expand Up @@ -46,9 +47,11 @@ func Request(method string, u string, opts ...RequestOption) (*Response, error)
req := &http.Request{
Method: method,
URL: URL,
Body: io.NopCloser(strings.NewReader(opt.Body.String())),
Header: opt.Header,
}
if opt.Body != nil {
req.Body = io.NopCloser(strings.NewReader(opt.Body.String()))
}
r, err := cli.Do(req)
resp := newResponse(r)
return resp, err
Expand Down

0 comments on commit 84a0b9f

Please sign in to comment.