Skip to content

Commit

Permalink
remove usage of gconv.Unsafe* functions internally to avoid unexpecte…
Browse files Browse the repository at this point in the history
…d errors
  • Loading branch information
gqcn committed Sep 14, 2021
1 parent 99b6085 commit 727f58a
Show file tree
Hide file tree
Showing 32 changed files with 63 additions and 77 deletions.
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_any_any_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func (m *AnyAnyMap) Merge(other *AnyAnyMap) {
// String returns the map as a string.
func (m *AnyAnyMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_int_any_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (m *IntAnyMap) Merge(other *IntAnyMap) {
// String returns the map as a string.
func (m *IntAnyMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_int_int_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (m *IntIntMap) Merge(other *IntIntMap) {
// String returns the map as a string.
func (m *IntIntMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_int_str_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (m *IntStrMap) Merge(other *IntStrMap) {
// String returns the map as a string.
func (m *IntStrMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_str_any_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (m *StrAnyMap) Merge(other *StrAnyMap) {
// String returns the map as a string.
func (m *StrAnyMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_str_int_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (m *StrIntMap) Merge(other *StrIntMap) {
// String returns the map as a string.
func (m *StrIntMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_hash_str_str_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (m *StrStrMap) Merge(other *StrStrMap) {
// String returns the map as a string.
func (m *StrStrMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
2 changes: 1 addition & 1 deletion container/gmap/gmap_list_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func (m *ListMap) Merge(other *ListMap) {
// String returns the map as a string.
func (m *ListMap) String() string {
b, _ := m.MarshalJSON()
return gconv.UnsafeBytesToStr(b)
return string(b)
}

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
Expand Down
4 changes: 2 additions & 2 deletions container/gtype/byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Byte) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Byte) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Byte) UnmarshalJSON(b []byte) error {
v.Set(gconv.Uint8(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Uint8(string(b)))
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion container/gtype/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (v *Bytes) MarshalJSON() ([]byte, error) {
val := v.Val()
dst := make([]byte, base64.StdEncoding.EncodedLen(len(val)))
base64.StdEncoding.Encode(dst, val)
return gconv.UnsafeStrToBytes(`"` + gconv.UnsafeBytesToStr(dst) + `"`), nil
return []byte(`"` + string(dst) + `"`), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Expand Down
4 changes: 2 additions & 2 deletions container/gtype/float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func (v *Float32) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Float32) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil
return []byte(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Float32) UnmarshalJSON(b []byte) error {
v.Set(gconv.Float32(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Float32(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func (v *Float64) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Float64) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil
return []byte(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Float64) UnmarshalJSON(b []byte) error {
v.Set(gconv.Float64(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Float64(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Int) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Int) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.Itoa(v.Val())), nil
return []byte(strconv.Itoa(v.Val())), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Int) UnmarshalJSON(b []byte) error {
v.Set(gconv.Int(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Int(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Int32) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Int32) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.Itoa(int(v.Val()))), nil
return []byte(strconv.Itoa(int(v.Val()))), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Int32) UnmarshalJSON(b []byte) error {
v.Set(gconv.Int32(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Int32(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Int64) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Int64) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatInt(v.Val(), 10)), nil
return []byte(strconv.FormatInt(v.Val(), 10)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Int64) UnmarshalJSON(b []byte) error {
v.Set(gconv.Int64(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Int64(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func (v *String) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *String) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(`"` + v.Val() + `"`), nil
return []byte(`"` + v.Val() + `"`), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *String) UnmarshalJSON(b []byte) error {
v.Set(gconv.UnsafeBytesToStr(bytes.Trim(b, `"`)))
v.Set(string(bytes.Trim(b, `"`)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Uint) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Uint) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Uint) UnmarshalJSON(b []byte) error {
v.Set(gconv.Uint(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Uint(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Uint32) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Uint32) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Uint32) UnmarshalJSON(b []byte) error {
v.Set(gconv.Uint32(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Uint32(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions container/gtype/uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (v *Uint64) String() string {

// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Uint64) MarshalJSON() ([]byte, error) {
return gconv.UnsafeStrToBytes(strconv.FormatUint(v.Val(), 10)), nil
return []byte(strconv.FormatUint(v.Val(), 10)), nil
}

// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Uint64) UnmarshalJSON(b []byte) error {
v.Set(gconv.Uint64(gconv.UnsafeBytesToStr(b)))
v.Set(gconv.Uint64(string(b)))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions database/gdb/gdb_type_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func (r Record) Interface() interface{} {
// Json converts `r` to JSON format content.
func (r Record) Json() string {
content, _ := gparser.VarToJson(r.Map())
return gconv.UnsafeBytesToStr(content)
return string(content)
}

// Xml converts `r` to XML format content.
func (r Record) Xml(rootTag ...string) string {
content, _ := gparser.VarToXml(r.Map(), rootTag...)
return gconv.UnsafeBytesToStr(content)
return string(content)
}

// Map converts `r` to map[string]interface{}.
Expand Down
2 changes: 1 addition & 1 deletion database/gredis/gredis_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c *Conn) ReceiveVarWithTimeout(timeout time.Duration) (*gvar.Var, error) {
func resultToVar(result interface{}, err error) (*gvar.Var, error) {
if err == nil {
if result, ok := result.([]byte); ok {
return gvar.New(gconv.UnsafeBytesToStr(result)), err
return gvar.New(string(result)), err
}
// It treats all returned slice as string slice.
if result, ok := result.([]interface{}); ok {
Expand Down
7 changes: 3 additions & 4 deletions encoding/gbase64/gbase64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package gbase64

import (
"encoding/base64"
"github.com/gogf/gf/util/gconv"
"io/ioutil"
)

Expand All @@ -27,7 +26,7 @@ func EncodeString(src string) string {

// EncodeToString encodes bytes to string with BASE64 algorithm.
func EncodeToString(src []byte) string {
return gconv.UnsafeBytesToStr(Encode(src))
return string(Encode(src))
}

// EncryptFile encodes file content of <path> using BASE64 algorithms.
Expand Down Expand Up @@ -55,7 +54,7 @@ func EncodeFileToString(path string) (string, error) {
if err != nil {
return "", err
}
return gconv.UnsafeBytesToStr(content), nil
return string(content), nil
}

// MustEncodeFileToString encodes file content of <path> to string using BASE64 algorithms.
Expand Down Expand Up @@ -103,7 +102,7 @@ func MustDecodeString(data string) []byte {
// DecodeString decodes string with BASE64 algorithm.
func DecodeToString(data string) (string, error) {
b, err := DecodeString(data)
return gconv.UnsafeBytesToStr(b), err
return string(b), err
}

// MustDecodeToString decodes string with BASE64 algorithm.
Expand Down
15 changes: 7 additions & 8 deletions encoding/gjson/gjson_api_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/gogf/gf/encoding/gxml"
"github.com/gogf/gf/encoding/gyaml"
"github.com/gogf/gf/internal/json"
"github.com/gogf/gf/util/gconv"
)

// ========================================================================
Expand Down Expand Up @@ -50,7 +49,7 @@ func (j *Json) MustToJson() []byte {
}

func (j *Json) MustToJsonString() string {
return gconv.UnsafeBytesToStr(j.MustToJson())
return string(j.MustToJson())
}

func (j *Json) MustToJsonIndent() []byte {
Expand All @@ -62,7 +61,7 @@ func (j *Json) MustToJsonIndent() []byte {
}

func (j *Json) MustToJsonIndentString() string {
return gconv.UnsafeBytesToStr(j.MustToJsonIndent())
return string(j.MustToJsonIndent())
}

// ========================================================================
Expand Down Expand Up @@ -96,7 +95,7 @@ func (j *Json) MustToXml(rootTag ...string) []byte {
}

func (j *Json) MustToXmlString(rootTag ...string) string {
return gconv.UnsafeBytesToStr(j.MustToXml(rootTag...))
return string(j.MustToXml(rootTag...))
}

func (j *Json) MustToXmlIndent(rootTag ...string) []byte {
Expand All @@ -108,7 +107,7 @@ func (j *Json) MustToXmlIndent(rootTag ...string) []byte {
}

func (j *Json) MustToXmlIndentString(rootTag ...string) string {
return gconv.UnsafeBytesToStr(j.MustToXmlIndent(rootTag...))
return string(j.MustToXmlIndent(rootTag...))
}

// ========================================================================
Expand All @@ -135,7 +134,7 @@ func (j *Json) MustToYaml() []byte {
}

func (j *Json) MustToYamlString() string {
return gconv.UnsafeBytesToStr(j.MustToYaml())
return string(j.MustToYaml())
}

// ========================================================================
Expand All @@ -162,7 +161,7 @@ func (j *Json) MustToToml() []byte {
}

func (j *Json) MustToTomlString() string {
return gconv.UnsafeBytesToStr(j.MustToToml())
return string(j.MustToToml())
}

// ========================================================================
Expand Down Expand Up @@ -192,5 +191,5 @@ func (j *Json) MustToIni() []byte {

// MustToIniString .
func (j *Json) MustToIniString() string {
return gconv.UnsafeBytesToStr(j.MustToIni())
return string(j.MustToIni())
}
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_request_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (r *Request) GetBody() []byte {
// GetBodyString retrieves and returns request body content as string.
// It can be called multiple times retrieving the same body content.
func (r *Request) GetBodyString() string {
return gconv.UnsafeBytesToStr(r.GetBody())
return string(r.GetBody())
}

// GetJson parses current request content as JSON format, and returns the JSON object.
Expand Down Expand Up @@ -375,7 +375,7 @@ func (r *Request) parseForm() {
// It might be JSON/XML content.
if s := gstr.Trim(name + strings.Join(values, " ")); len(s) > 0 {
if s[0] == '{' && s[len(s)-1] == '}' || s[0] == '<' && s[len(s)-1] == '>' {
r.bodyContent = gconv.UnsafeStrToBytes(s)
r.bodyContent = []byte(s)
params = ""
break
}
Expand Down
Loading

0 comments on commit 727f58a

Please sign in to comment.