refactor: interface{} to any and reflect.Ptr to reflect.Pointer (#4395)

This pull request standardizes the use of the Go 1.18+ `any` type alias
instead of `interface{}` throughout the codebase. The change improves
code readability and aligns with modern Go best practices. The update
touches many files, including core data structures, code generation
templates, logging utilities, and test data, ensuring consistency across
all usages.

**Type alias migration to `any`:**

* Replaced all instances of `interface{}` with `any` in core data
structures such as `garray` and in generated model structs (e.g.,
`TableUser`, `User1`, `User2`) to modernize type usage.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[3]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[4]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[5]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[6]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
* Updated function signatures, method parameters, and return types from
`interface{}` to `any` in various parts of the codebase, including code
generation, service logic, and logging utilities (e.g., `mlog`).
[[1]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[2]](diffhunk://#diff-2b1953fb78cf3593d8c2c7d911e95b65fd0b847c30ed0b4d167d16fe6d781235L54-R74)
[[3]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[4]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)
[[5]](diffhunk://#diff-c5d51d56f487779a2b6207c7ad26c7a20bbadcc846ce094fe60ab4cabff58c51L107-R107)
[[6]](diffhunk://#diff-f96e6a9fdb416eb1804ceaba1fe0ac637bff22c43837f8bb849c2366ce72d4a1L116-R121)
[[7]](diffhunk://#diff-f94c83a1b08ae060d9346f4a6031fc4a7b9a0b894e02d9afaa09018b6598eac0L112-R112)
[[8]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L36-R36)
[[9]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L74-R74)
[[10]](diffhunk://#diff-748b11dbe8828dd4c040ec23cae0b8fe57ecf0a2d1b7694ea39102294e633c64L96-R96)

**Generated code and templates:**

* Adjusted generated files and code generation templates to output `any`
instead of `interface{}` for relevant struct fields and function
signatures, ensuring that new code generation aligns with the updated
convention.
[[1]](diffhunk://#diff-6c19859cb32c7516ea95ddc8f8235460818eb2f24d2204308e0d9e1b19e7d90fL15-R19)
[[2]](diffhunk://#diff-a15ba2f5e830b4833c47b902515a4f9e5a4f83a3707698f3229b307ec3776b41L15-R18)
[[3]](diffhunk://#diff-52e0837e84d49221d1b810d88fdf78221f36cffcd664fb42f8aba49a79b974dcL15-R19)
[[4]](diffhunk://#diff-11c3457d1a23a4ca6ecd00d6b856289774936b6a708384cf03aff164044e7546L15-R19)
[[5]](diffhunk://#diff-2cff9cf8e6a0cc34087326d8c8149c3bbaf74c76fdbdf5a73daed13cc04249e1L15-R19)
[[6]](diffhunk://#diff-175edfeea54490b8fe4e18ffcbea5835efaf8f0b8acf623359073987cae7eb76L48-R55)
[[7]](diffhunk://#diff-e001b7a4b63603b9b14f00de78a4d570bb76c5f57d856a24643f071032e12356L66-R73)
[[8]](diffhunk://#diff-5582954e8a9983988dc8854ad82067fb2ac6269b988e07357ad8db1dfec5f1a0L39-R41)

**Container and utility updates:**

* Refactored the `garray` container implementation and related
constructors/methods to use `[]any` instead of `[]interface{}`, along
with corresponding function signatures.
[[1]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L31-R31)
[[2]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L52-R52)
[[3]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L62-R62)
[[4]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L73-R86)
[[5]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L96-R97)
[[6]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L107-R114)
[[7]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L124-R124)
[[8]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L135-R143)
[[9]](diffhunk://#diff-3a1259e160a4dfa5fe49dfe739fbdb986c0d0a2220a709882ea48d3ae1b8f911L167-R167)

These changes collectively modernize the codebase and prepare it for
future Go developments by using the idiomatic `any` type.
This commit is contained in:
hailaz
2025-08-28 16:53:19 +08:00
committed by GitHub
parent 26f20787ba
commit ee24da4e72
552 changed files with 4128 additions and 4133 deletions

View File

@ -9,15 +9,15 @@
// Note that package gbinary encodes the data using LittleEndian in default.
package gbinary
func Encode(values ...interface{}) []byte {
func Encode(values ...any) []byte {
return LeEncode(values...)
}
func EncodeByLength(length int, values ...interface{}) []byte {
func EncodeByLength(length int, values ...any) []byte {
return LeEncodeByLength(length, values...)
}
func Decode(b []byte, values ...interface{}) error {
func Decode(b []byte, values ...any) error {
return LeDecode(b, values...)
}

View File

@ -23,7 +23,7 @@ import (
//
// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
// value to string and then to bytes.
func BeEncode(values ...interface{}) []byte {
func BeEncode(values ...any) []byte {
buf := new(bytes.Buffer)
for i := 0; i < len(values); i++ {
if values[i] == nil {
@ -71,7 +71,7 @@ func BeEncode(values ...interface{}) []byte {
return buf.Bytes()
}
func BeEncodeByLength(length int, values ...interface{}) []byte {
func BeEncodeByLength(length int, values ...any) []byte {
b := BeEncode(values...)
if len(b) < length {
b = append(b, make([]byte, length-len(b))...)
@ -81,7 +81,7 @@ func BeEncodeByLength(length int, values ...interface{}) []byte {
return b
}
func BeDecode(b []byte, values ...interface{}) error {
func BeDecode(b []byte, values ...any) error {
var (
err error
buf = bytes.NewBuffer(b)

View File

@ -23,7 +23,7 @@ import (
//
// It supports common variable type asserting, and finally it uses fmt.Sprintf converting
// value to string and then to bytes.
func LeEncode(values ...interface{}) []byte {
func LeEncode(values ...any) []byte {
buf := new(bytes.Buffer)
for i := 0; i < len(values); i++ {
if values[i] == nil {
@ -71,7 +71,7 @@ func LeEncode(values ...interface{}) []byte {
return buf.Bytes()
}
func LeEncodeByLength(length int, values ...interface{}) []byte {
func LeEncodeByLength(length int, values ...any) []byte {
b := LeEncode(values...)
if len(b) < length {
b = append(b, make([]byte, length-len(b))...)
@ -81,7 +81,7 @@ func LeEncodeByLength(length int, values ...interface{}) []byte {
return b
}
func LeDecode(b []byte, values ...interface{}) error {
func LeDecode(b []byte, values ...any) error {
var (
err error
buf = bytes.NewBuffer(b)

View File

@ -20,7 +20,7 @@ type User struct {
Url string
}
var testData = map[string]interface{}{
var testData = map[string]any{
//"nil": nil,
"int": int(123),
"int8": int8(-99),

View File

@ -72,13 +72,13 @@ func SpecialCharsDecode(s string) string {
// OK: SpecialCharsMapOrStruct(m)
// OK: SpecialCharsMapOrStruct(&s)
// Error: SpecialCharsMapOrStruct(s)
func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
func SpecialCharsMapOrStruct(mapOrStruct any) error {
var (
reflectValue = reflect.ValueOf(mapOrStruct)
reflectKind = reflectValue.Kind()
originalKind = reflectKind
)
for reflectValue.IsValid() && (reflectKind == reflect.Ptr || reflectKind == reflect.Interface) {
for reflectValue.IsValid() && (reflectKind == reflect.Pointer || reflectKind == reflect.Interface) {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
@ -106,7 +106,7 @@ func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
}
case reflect.Struct:
if originalKind != reflect.Ptr {
if originalKind != reflect.Pointer {
return gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid input parameter type "%s", should be type of pointer to struct`,

View File

@ -20,10 +20,10 @@ import (
)
// Decode converts INI format to map.
func Decode(data []byte) (res map[string]interface{}, err error) {
res = make(map[string]interface{})
func Decode(data []byte) (res map[string]any, err error) {
res = make(map[string]any)
var (
fieldMap = make(map[string]interface{})
fieldMap = make(map[string]any)
bytesReader = bytes.NewReader(data)
bufioReader = bufio.NewReader(bytesReader)
section string
@ -58,7 +58,7 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
lastSection = section
} else if lastSection != section {
lastSection = section
fieldMap = make(map[string]interface{})
fieldMap = make(map[string]any)
}
haveSection = true
} else if !haveSection {
@ -79,16 +79,16 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
}
// Encode converts map to INI format.
func Encode(data map[string]interface{}) (res []byte, err error) {
func Encode(data map[string]any) (res []byte, err error) {
var (
n int
w = new(bytes.Buffer)
m map[string]interface{}
m map[string]any
ok bool
)
for section, item := range data {
// Section key-value pairs.
if m, ok = item.(map[string]interface{}); ok {
if m, ok = item.(map[string]any); ok {
n, err = w.WriteString(fmt.Sprintf("[%s]\n", section))
if err != nil || n == 0 {
return nil, gerror.Wrapf(err, "w.WriteString failed")

View File

@ -40,12 +40,12 @@ func TestDecode(t *testing.T) {
if err != nil {
gtest.Fatal(err)
}
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
t.Assert(res["addr"].(map[string]interface{})["command"], `/bin/echo "gf=GoFrame"`)
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
t.Assert(res["键"].(map[string]interface{})["呵呵"], "值")
t.Assert(res["addr"].(map[string]any)["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]any)["port"], "9001")
t.Assert(res["addr"].(map[string]any)["command"], `/bin/echo "gf=GoFrame"`)
t.Assert(res["DBINFO"].(map[string]any)["user"], "root")
t.Assert(res["DBINFO"].(map[string]any)["type"], "mysql")
t.Assert(res["键"].(map[string]any)["呵呵"], "值")
})
gtest.C(t, func(t *gtest.T) {
@ -76,10 +76,10 @@ func TestEncode(t *testing.T) {
gtest.Fatal(err)
}
t.Assert(res["addr"].(map[string]interface{})["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]interface{})["port"], "9001")
t.Assert(res["DBINFO"].(map[string]interface{})["user"], "root")
t.Assert(res["DBINFO"].(map[string]interface{})["type"], "mysql")
t.Assert(res["addr"].(map[string]any)["ip"], "127.0.0.1")
t.Assert(res["addr"].(map[string]any)["port"], "9001")
t.Assert(res["DBINFO"].(map[string]any)["user"], "root")
t.Assert(res["DBINFO"].(map[string]any)["type"], "mysql")
})
}
@ -99,9 +99,9 @@ func TestToJson(t *testing.T) {
iniMap, err := gini.Decode([]byte(iniContent))
t.AssertNil(err)
t.Assert(iniMap["addr"].(map[string]interface{})["ip"], json.Get("addr.ip").String())
t.Assert(iniMap["addr"].(map[string]interface{})["port"], json.Get("addr.port").String())
t.Assert(iniMap["DBINFO"].(map[string]interface{})["user"], json.Get("DBINFO.user").String())
t.Assert(iniMap["DBINFO"].(map[string]interface{})["type"], json.Get("DBINFO.type").String())
t.Assert(iniMap["addr"].(map[string]any)["ip"], json.Get("addr.ip").String())
t.Assert(iniMap["addr"].(map[string]any)["port"], json.Get("addr.port").String())
t.Assert(iniMap["DBINFO"].(map[string]any)["user"], json.Get("DBINFO.user").String())
t.Assert(iniMap["DBINFO"].(map[string]any)["type"], json.Get("DBINFO.type").String())
})
}

View File

@ -41,9 +41,9 @@ const (
// Json is the customized JSON struct.
type Json struct {
mu rwmutex.RWMutex
p *interface{} // Pointer for hierarchical data access, it's the root of data in default.
c byte // Char separator('.' in default).
vc bool // Violence Check(false in default), which is used to access data when the hierarchical data key contains separator char.
p *any // Pointer for hierarchical data access, it's the root of data in default.
c byte // Char separator('.' in default).
vc bool // Violence Check(false in default), which is used to access data when the hierarchical data key contains separator char.
}
// Options for Json object creating/loading.
@ -51,29 +51,29 @@ type Options struct {
Safe bool // Mark this object is for in concurrent-safe usage. This is especially for Json object creating.
Tags string // Custom priority tags for decoding, eg: "json,yaml,MyTag". This is especially for struct parsing into Json object.
Type ContentType // Type specifies the data content type, eg: json, xml, yaml, toml, ini.
StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an interface{} as a string instead of as a float64.
StrNumber bool // StrNumber causes the Decoder to unmarshal a number into an any as a string instead of as a float64.
}
// iInterfaces is used for type assert api for Interfaces().
type iInterfaces interface {
Interfaces() []interface{}
Interfaces() []any
}
// iMapStrAny is the interface support for converting struct parameter to map.
type iMapStrAny interface {
MapStrAny() map[string]interface{}
MapStrAny() map[string]any
}
// iVal is the interface for underlying interface{} retrieving.
// iVal is the interface for underlying any retrieving.
type iVal interface {
Val() interface{}
Val() any
}
// setValue sets `value` to `j` by `pattern`.
// Note:
// 1. If value is nil and removed is true, means deleting this value;
// 2. It's quite complicated in hierarchical data search, node creating and data assignment;
func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
func (j *Json) setValue(pattern string, value any, removed bool) error {
var (
err error
array = strings.Split(pattern, string(j.c))
@ -85,33 +85,33 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
// Initialization checks.
if *j.p == nil {
if gstr.IsNumeric(array[0]) {
*j.p = make([]interface{}, 0)
*j.p = make([]any, 0)
} else {
*j.p = make(map[string]interface{})
*j.p = make(map[string]any)
}
}
var (
pparent *interface{} = nil // Parent pointer.
pointer = j.p // Current pointer.
pparent *any = nil // Parent pointer.
pointer = j.p // Current pointer.
)
j.mu.Lock()
defer j.mu.Unlock()
for i := 0; i < length; i++ {
switch (*pointer).(type) {
case map[string]interface{}:
case map[string]any:
if i == length-1 {
if removed && value == nil {
// Delete item from map.
delete((*pointer).(map[string]interface{}), array[i])
delete((*pointer).(map[string]any), array[i])
} else {
if (*pointer).(map[string]interface{}) == nil {
*pointer = map[string]interface{}{}
if (*pointer).(map[string]any) == nil {
*pointer = map[string]any{}
}
(*pointer).(map[string]interface{})[array[i]] = value
(*pointer).(map[string]any)[array[i]] = value
}
} else {
// If the key does not exit in the map.
if v, ok := (*pointer).(map[string]interface{})[array[i]]; !ok {
if v, ok := (*pointer).(map[string]any)[array[i]]; !ok {
if removed && value == nil {
goto done
}
@ -119,12 +119,12 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
if gstr.IsNumeric(array[i+1]) {
// Creating array node.
n, _ := strconv.Atoi(array[i+1])
var v interface{} = make([]interface{}, n+1)
var v any = make([]any, n+1)
pparent = j.setPointerWithValue(pointer, array[i], v)
pointer = &v
} else {
// Creating map node.
var v interface{} = make(map[string]interface{})
var v any = make(map[string]any)
pparent = j.setPointerWithValue(pointer, array[i], v)
pointer = &v
}
@ -134,13 +134,13 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
}
}
case []interface{}:
case []any:
// A string key.
if !gstr.IsNumeric(array[i]) {
if i == length-1 {
*pointer = map[string]interface{}{array[i]: value}
*pointer = map[string]any{array[i]: value}
} else {
var v interface{} = make(map[string]interface{})
var v any = make(map[string]any)
*pointer = v
pparent = pointer
pointer = &v
@ -156,16 +156,16 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
if i == length-1 {
// Leaf node.
if len((*pointer).([]interface{})) > valueNum {
if len((*pointer).([]any)) > valueNum {
if removed && value == nil {
// Deleting element.
if pparent == nil {
*pointer = append((*pointer).([]interface{})[:valueNum], (*pointer).([]interface{})[valueNum+1:]...)
*pointer = append((*pointer).([]any)[:valueNum], (*pointer).([]any)[valueNum+1:]...)
} else {
j.setPointerWithValue(pparent, array[i-1], append((*pointer).([]interface{})[:valueNum], (*pointer).([]interface{})[valueNum+1:]...))
j.setPointerWithValue(pparent, array[i-1], append((*pointer).([]any)[:valueNum], (*pointer).([]any)[valueNum+1:]...))
}
} else {
(*pointer).([]interface{})[valueNum] = value
(*pointer).([]any)[valueNum] = value
}
} else {
if removed && value == nil {
@ -176,8 +176,8 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
j.setPointerWithValue(pointer, array[i], value)
} else {
// It is not the root node.
s := make([]interface{}, valueNum+1)
copy(s, (*pointer).([]interface{}))
s := make([]any, valueNum+1)
copy(s, (*pointer).([]any))
s[valueNum] = value
j.setPointerWithValue(pparent, array[i-1], s)
}
@ -186,10 +186,10 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
// Branch node.
if gstr.IsNumeric(array[i+1]) {
n, _ := strconv.Atoi(array[i+1])
pSlice := (*pointer).([]interface{})
pSlice := (*pointer).([]any)
if len(pSlice) > valueNum {
item := pSlice[valueNum]
if s, ok := item.([]interface{}); ok {
if s, ok := item.([]any); ok {
for i := 0; i < n-len(s); i++ {
s = append(s, nil)
}
@ -199,7 +199,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
if removed && value == nil {
goto done
}
var v interface{} = make([]interface{}, n+1)
var v any = make([]any, n+1)
pparent = j.setPointerWithValue(pointer, array[i], v)
pointer = &v
}
@ -207,19 +207,19 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
if removed && value == nil {
goto done
}
var v interface{} = make([]interface{}, n+1)
var v any = make([]any, n+1)
pparent = j.setPointerWithValue(pointer, array[i], v)
pointer = &v
}
} else {
pSlice := (*pointer).([]interface{})
pSlice := (*pointer).([]any)
if len(pSlice) > valueNum {
pparent = pointer
pointer = &(*pointer).([]interface{})[valueNum]
pointer = &(*pointer).([]any)[valueNum]
} else {
s := make([]interface{}, valueNum+1)
s := make([]any, valueNum+1)
copy(s, pSlice)
s[valueNum] = make(map[string]interface{})
s[valueNum] = make(map[string]any)
if pparent != nil {
// i > 0
j.setPointerWithValue(pparent, array[i-1], s)
@ -227,7 +227,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
pointer = &s[valueNum]
} else {
// i = 0
var v interface{} = s
var v any = s
*pointer = v
pparent = pointer
pointer = &s[valueNum]
@ -244,7 +244,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
}
if gstr.IsNumeric(array[i]) {
n, _ := strconv.Atoi(array[i])
s := make([]interface{}, n+1)
s := make([]any, n+1)
if i == length-1 {
s[n] = value
}
@ -255,13 +255,13 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
pparent = pointer
}
} else {
var v1, v2 interface{}
var v1, v2 any
if i == length-1 {
v1 = map[string]interface{}{
v1 = map[string]any{
array[i]: value,
}
} else {
v1 = map[string]interface{}{
v1 = map[string]any{
array[i]: nil,
}
}
@ -271,7 +271,7 @@ func (j *Json) setValue(pattern string, value interface{}, removed bool) error {
*pointer = v1
pparent = pointer
}
v2 = v1.(map[string]interface{})[array[i]]
v2 = v1.(map[string]any)[array[i]]
pointer = &v2
}
}
@ -280,18 +280,18 @@ done:
return nil
}
// convertValue converts `value` to map[string]interface{} or []interface{},
// convertValue converts `value` to map[string]any or []any,
// which can be supported for hierarchical data access.
func (j *Json) convertValue(value interface{}) (convertedValue interface{}, err error) {
func (j *Json) convertValue(value any) (convertedValue any, err error) {
if value == nil {
return
}
switch value.(type) {
case map[string]interface{}:
case map[string]any:
return value, nil
case []interface{}:
case []any:
return value, nil
default:
@ -333,19 +333,19 @@ func (j *Json) convertValue(value interface{}) (convertedValue interface{}, err
// setPointerWithValue sets `key`:`value` to `pointer`, the `key` may be a map key or slice index.
// It returns the pointer to the new value set.
func (j *Json) setPointerWithValue(pointer *interface{}, key string, value interface{}) *interface{} {
func (j *Json) setPointerWithValue(pointer *any, key string, value any) *any {
switch (*pointer).(type) {
case map[string]interface{}:
(*pointer).(map[string]interface{})[key] = value
case map[string]any:
(*pointer).(map[string]any)[key] = value
return &value
case []interface{}:
case []any:
n, _ := strconv.Atoi(key)
if len((*pointer).([]interface{})) > n {
(*pointer).([]interface{})[n] = value
return &(*pointer).([]interface{})[n]
if len((*pointer).([]any)) > n {
(*pointer).([]any)[n] = value
return &(*pointer).([]any)[n]
} else {
s := make([]interface{}, n+1)
copy(s, (*pointer).([]interface{}))
s := make([]any, n+1)
copy(s, (*pointer).([]any))
s[n] = value
*pointer = s
return &s[n]
@ -357,7 +357,7 @@ func (j *Json) setPointerWithValue(pointer *interface{}, key string, value inter
}
// getPointerByPattern returns a pointer to the value by specified `pattern`.
func (j *Json) getPointerByPattern(pattern string) *interface{} {
func (j *Json) getPointerByPattern(pattern string) *any {
if j.p == nil {
return nil
}
@ -369,7 +369,7 @@ func (j *Json) getPointerByPattern(pattern string) *interface{} {
}
// getPointerByPatternWithViolenceCheck returns a pointer to the value of specified `pattern` with violence check.
func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *interface{} {
func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *any {
if !j.vc {
return j.getPointerByPatternWithoutViolenceCheck(pattern)
}
@ -419,7 +419,7 @@ func (j *Json) getPointerByPatternWithViolenceCheck(pattern string) *interface{}
}
// getPointerByPatternWithoutViolenceCheck returns a pointer to the value of specified `pattern`, with no violence check.
func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *interface{} {
func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *any {
if j.vc {
return j.getPointerByPatternWithViolenceCheck(pattern)
}
@ -454,17 +454,17 @@ func (j *Json) getPointerByPatternWithoutViolenceCheck(pattern string) *interfac
// checkPatternByPointer checks whether there's value by `key` in specified `pointer`.
// It returns a pointer to the value.
func (j *Json) checkPatternByPointer(key string, pointer *interface{}) *interface{} {
func (j *Json) checkPatternByPointer(key string, pointer *any) *any {
switch (*pointer).(type) {
case map[string]interface{}:
if v, ok := (*pointer).(map[string]interface{})[key]; ok {
case map[string]any:
if v, ok := (*pointer).(map[string]any)[key]; ok {
return &v
}
case []interface{}:
case []any:
if gstr.IsNumeric(key) {
n, err := strconv.Atoi(key)
if err == nil && len((*pointer).([]interface{})) > n {
return &(*pointer).([]interface{})[n]
if err == nil && len((*pointer).([]any)) > n {
return &(*pointer).([]any)[n]
}
}
}

View File

@ -16,7 +16,7 @@ import (
)
// Interface returns the json value.
func (j *Json) Interface() interface{} {
func (j *Json) Interface() any {
if j == nil {
return nil
}
@ -51,7 +51,7 @@ func (j *Json) IsNil() bool {
// "list.10", "array.0.name", "array.0.1.id".
//
// It returns a default value specified by `def` if value for `pattern` is not found.
func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
func (j *Json) Get(pattern string, def ...any) *gvar.Var {
if j == nil {
return nil
}
@ -75,13 +75,13 @@ func (j *Json) Get(pattern string, def ...interface{}) *gvar.Var {
// GetJson gets the value by specified `pattern`,
// and converts it to an un-concurrent-safe Json object.
func (j *Json) GetJson(pattern string, def ...interface{}) *Json {
func (j *Json) GetJson(pattern string, def ...any) *Json {
return New(j.Get(pattern, def...).Val())
}
// GetJsons gets the value by specified `pattern`,
// and converts it to a slice of un-concurrent-safe Json object.
func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
func (j *Json) GetJsons(pattern string, def ...any) []*Json {
array := j.Get(pattern, def...).Array()
if len(array) > 0 {
jsonSlice := make([]*Json, len(array))
@ -95,7 +95,7 @@ func (j *Json) GetJsons(pattern string, def ...interface{}) []*Json {
// GetJsonMap gets the value by specified `pattern`,
// and converts it to a map of un-concurrent-safe Json object.
func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
func (j *Json) GetJsonMap(pattern string, def ...any) map[string]*Json {
m := j.Get(pattern, def...).Map()
if len(m) > 0 {
jsonMap := make(map[string]*Json, len(m))
@ -109,12 +109,12 @@ func (j *Json) GetJsonMap(pattern string, def ...interface{}) map[string]*Json {
// Set sets value with specified `pattern`.
// It supports hierarchical data access by char separator, which is '.' in default.
func (j *Json) Set(pattern string, value interface{}) error {
func (j *Json) Set(pattern string, value any) error {
return j.setValue(pattern, value, false)
}
// MustSet performs as Set, but it panics if any error occurs.
func (j *Json) MustSet(pattern string, value interface{}) {
func (j *Json) MustSet(pattern string, value any) {
if err := j.Set(pattern, value); err != nil {
panic(err)
}
@ -145,10 +145,10 @@ func (j *Json) Len(pattern string) int {
p := j.getPointerByPattern(pattern)
if p != nil {
switch (*p).(type) {
case map[string]interface{}:
return len((*p).(map[string]interface{}))
case []interface{}:
return len((*p).([]interface{}))
case map[string]any:
return len((*p).(map[string]any))
case []any:
return len((*p).([]any))
default:
return -1
}
@ -158,7 +158,7 @@ func (j *Json) Len(pattern string) int {
// Append appends value to the value by specified `pattern`.
// The target value by `pattern` should be type of slice.
func (j *Json) Append(pattern string, value interface{}) error {
func (j *Json) Append(pattern string, value any) error {
p := j.getPointerByPattern(pattern)
if p == nil || *p == nil {
if pattern == "." {
@ -167,37 +167,37 @@ func (j *Json) Append(pattern string, value interface{}) error {
return j.Set(fmt.Sprintf("%s.0", pattern), value)
}
switch (*p).(type) {
case []interface{}:
case []any:
if pattern == "." {
return j.Set(fmt.Sprintf("%d", len((*p).([]interface{}))), value)
return j.Set(fmt.Sprintf("%d", len((*p).([]any))), value)
}
return j.Set(fmt.Sprintf("%s.%d", pattern, len((*p).([]interface{}))), value)
return j.Set(fmt.Sprintf("%s.%d", pattern, len((*p).([]any))), value)
}
return gerror.NewCodef(gcode.CodeInvalidParameter, "invalid variable type of %s", pattern)
}
// MustAppend performs as Append, but it panics if any error occurs.
func (j *Json) MustAppend(pattern string, value interface{}) {
func (j *Json) MustAppend(pattern string, value any) {
if err := j.Append(pattern, value); err != nil {
panic(err)
}
}
// Map converts current Json object to map[string]interface{}.
// Map converts current Json object to map[string]any.
// It returns nil if fails.
func (j *Json) Map() map[string]interface{} {
func (j *Json) Map() map[string]any {
return j.Var().Map()
}
// Array converts current Json object to []interface{}.
// Array converts current Json object to []any.
// It returns nil if fails.
func (j *Json) Array() []interface{} {
func (j *Json) Array() []any {
return j.Var().Array()
}
// Scan automatically calls Struct or Structs function according to the type of parameter
// `pointer` to implement the converting.
func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
func (j *Json) Scan(pointer any, mapping ...map[string]string) error {
return j.Var().Scan(pointer, mapping...)
}

View File

@ -19,7 +19,7 @@ import (
//
// The parameter `safe` specifies whether using this Json object in concurrent-safe context,
// which is false in default.
func New(data interface{}, safe ...bool) *Json {
func New(data any, safe ...bool) *Json {
return NewWithTag(data, string(ContentTypeJSON), safe...)
}
@ -31,7 +31,7 @@ func New(data interface{}, safe ...bool) *Json {
//
// The parameter `safe` specifies whether using this Json object in concurrent-safe context, which
// is false in default.
func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
func NewWithTag(data any, tags string, safe ...bool) *Json {
option := Options{
Tags: tags,
}
@ -43,7 +43,7 @@ func NewWithTag(data interface{}, tags string, safe ...bool) *Json {
// NewWithOptions creates a Json object with any variable type of `data`, but `data` should be a map
// or slice for data access reason, or it will make no sense.
func NewWithOptions(data interface{}, options Options) *Json {
func NewWithOptions(data any, options Options) *Json {
var j *Json
switch result := data.(type) {
case []byte:
@ -68,7 +68,7 @@ func NewWithOptions(data interface{}, options Options) *Json {
}
default:
var (
pointedData interface{}
pointedData any
reflectInfo = reflection.OriginValueAndKind(data)
)
switch reflectInfo.OriginKind {

View File

@ -155,7 +155,7 @@ func trimBOM(data []byte) []byte {
func loadContentWithOptions(data []byte, options Options) (*Json, error) {
var (
err error
result interface{}
result any
)
data = trimBOM(data)
if len(data) == 0 {

View File

@ -25,7 +25,7 @@ func (j *Json) UnmarshalJSON(b []byte) error {
}
// UnmarshalValue is an interface implement which sets any type of value for Json.
func (j *Json) UnmarshalValue(value interface{}) error {
func (j *Json) UnmarshalValue(value any) error {
if r := NewWithOptions(value, Options{
StrNumber: true,
}); r != nil {
@ -36,7 +36,7 @@ func (j *Json) UnmarshalValue(value interface{}) error {
}
// MapStrAny implements interface function MapStrAny().
func (j *Json) MapStrAny() map[string]interface{} {
func (j *Json) MapStrAny() map[string]any {
if j == nil {
return nil
}
@ -44,7 +44,7 @@ func (j *Json) MapStrAny() map[string]interface{} {
}
// Interfaces implements interface function Interfaces().
func (j *Json) Interfaces() []interface{} {
func (j *Json) Interfaces() []any {
if j == nil {
return nil
}

View File

@ -17,32 +17,32 @@ import (
// Valid checks whether `data` is a valid JSON data type.
// The parameter `data` specifies the json format data, which can be either
// bytes or string type.
func Valid(data interface{}) bool {
func Valid(data any) bool {
return json.Valid(gconv.Bytes(data))
}
// Marshal is alias of Encode in order to fit the habit of json.Marshal/Unmarshal functions.
func Marshal(v interface{}) (marshaledBytes []byte, err error) {
func Marshal(v any) (marshaledBytes []byte, err error) {
return Encode(v)
}
// MarshalIndent is alias of json.MarshalIndent in order to fit the habit of json.MarshalIndent function.
func MarshalIndent(v interface{}, prefix, indent string) (marshaledBytes []byte, err error) {
func MarshalIndent(v any, prefix, indent string) (marshaledBytes []byte, err error) {
return json.MarshalIndent(v, prefix, indent)
}
// Unmarshal is alias of DecodeTo in order to fit the habit of json.Marshal/Unmarshal functions.
func Unmarshal(data []byte, v interface{}) (err error) {
func Unmarshal(data []byte, v any) (err error) {
return DecodeTo(data, v)
}
// Encode encodes any golang variable `value` to JSON bytes.
func Encode(value interface{}) ([]byte, error) {
func Encode(value any) ([]byte, error) {
return json.Marshal(value)
}
// MustEncode performs as Encode, but it panics if any error occurs.
func MustEncode(value interface{}) []byte {
func MustEncode(value any) []byte {
b, err := Encode(value)
if err != nil {
panic(err)
@ -51,21 +51,21 @@ func MustEncode(value interface{}) []byte {
}
// EncodeString encodes any golang variable `value` to JSON string.
func EncodeString(value interface{}) (string, error) {
func EncodeString(value any) (string, error) {
b, err := json.Marshal(value)
return string(b), err
}
// MustEncodeString encodes any golang variable `value` to JSON string.
// It panics if any error occurs.
func MustEncodeString(value interface{}) string {
func MustEncodeString(value any) string {
return string(MustEncode(value))
}
// Decode decodes json format `data` to golang variable.
// The parameter `data` can be either bytes or string type.
func Decode(data interface{}, options ...Options) (interface{}, error) {
var value interface{}
func Decode(data any, options ...Options) (any, error) {
var value any
if err := DecodeTo(gconv.Bytes(data), &value, options...); err != nil {
return nil, err
} else {
@ -76,7 +76,7 @@ func Decode(data interface{}, options ...Options) (interface{}, error) {
// DecodeTo decodes json format `data` to specified golang variable `v`.
// The parameter `data` can be either bytes or string type.
// The parameter `v` should be a pointer type.
func DecodeTo(data interface{}, v interface{}, options ...Options) (err error) {
func DecodeTo(data any, v any, options ...Options) (err error) {
decoder := json.NewDecoder(bytes.NewReader(gconv.Bytes(data)))
if len(options) > 0 {
// The StrNumber option is for certain situations, not for all.
@ -93,7 +93,7 @@ func DecodeTo(data interface{}, v interface{}, options ...Options) (err error) {
// DecodeToJson codes json format `data` to a Json object.
// The parameter `data` can be either bytes or string type.
func DecodeToJson(data interface{}, options ...Options) (*Json, error) {
func DecodeToJson(data any, options ...Options) (*Json, error) {
if v, err := Decode(gconv.Bytes(data), options...); err != nil {
return nil, err
} else {

View File

@ -46,14 +46,14 @@ func Benchmark_Get_Complicated_Json(b *testing.B) {
func Benchmark_Stdlib_Json_Unmarshal_Simple_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
var m map[string]interface{}
var m map[string]any
json2.Unmarshal([]byte(jsonStr1), &m)
}
}
func Benchmark_Stdlib_Json_Unmarshal_Complicated_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
var m map[string]interface{}
var m map[string]any
json2.Unmarshal([]byte(jsonStr2), &m)
}
}

View File

@ -124,7 +124,7 @@ func ExampleValid() {
}
func ExampleMarshal() {
data := map[string]interface{}{
data := map[string]any{
"name": "john",
"score": 100,
}

View File

@ -59,7 +59,7 @@ func Test_MapAttributeConvert(t *testing.T) {
gtest.AssertNil(err)
tx := struct {
Title map[string]interface{}
Title map[string]any
}{}
err = j.Var().Scan(&tx)

View File

@ -213,10 +213,10 @@ func Test_Struct(t *testing.T) {
}
type M struct {
Id string `json:"id"`
Me map[string]interface{} `json:"me"`
Txt string `json:"txt"`
Items []*Item `json:"items"`
Id string `json:"id"`
Me map[string]any `json:"me"`
Txt string `json:"txt"`
Items []*Item `json:"items"`
}
txt := []byte(`

View File

@ -102,7 +102,7 @@ func Test_Decode(t *testing.T) {
})
})
gtest.C(t, func(t *gtest.T) {
var v interface{}
var v any
err := gjson.DecodeTo(data, &v)
t.AssertNil(err)
t.Assert(v, g.Map{
@ -558,7 +558,7 @@ func TestJson_Options(t *testing.T) {
s := S{
Id: 53687091200,
}
m := make(map[string]interface{})
m := make(map[string]any)
t.AssertNil(gjson.DecodeTo(gjson.MustEncode(s), &m, gjson.Options{
StrNumber: false,
}))

View File

@ -20,8 +20,8 @@ import (
)
// Decode converts properties format to map.
func Decode(data []byte) (res map[string]interface{}, err error) {
res = make(map[string]interface{})
func Decode(data []byte) (res map[string]any, err error) {
res = make(map[string]any)
pr, err := properties.Load(data, properties.UTF8)
if err != nil || pr == nil {
err = gerror.Wrapf(err, `Lib magiconair load Properties data failed.`)
@ -42,10 +42,10 @@ func Decode(data []byte) (res map[string]interface{}, err error) {
}
// Encode converts map to properties format.
func Encode(data map[string]interface{}) (res []byte, err error) {
func Encode(data map[string]any) (res []byte, err error) {
pr := properties.NewProperties()
flattened := map[string]interface{}{}
flattened := map[string]any{}
flattened = flattenAndMergeMap(flattened, data, "", ".")
@ -87,20 +87,20 @@ func ToJson(data []byte) (res []byte, err error) {
// deepSearch scans deep maps, following the key indexes listed in the sequence "path".
// The last value is expected to be another map, and is returned.
func deepSearch(m map[string]interface{}, path []string) map[string]interface{} {
func deepSearch(m map[string]any, path []string) map[string]any {
for _, k := range path {
m2, ok := m[k]
if !ok {
// intermediate key does not exist
// => create it and continue from there
m3 := make(map[string]interface{})
m3 := make(map[string]any)
m[k] = m3
m = m3
continue
}
m3, ok := m2.(map[string]interface{})
m3, ok := m2.(map[string]any)
if !ok {
m3 = make(map[string]interface{})
m3 = make(map[string]any)
m[k] = m3
}
// continue search from here
@ -110,21 +110,21 @@ func deepSearch(m map[string]interface{}, path []string) map[string]interface{}
}
// flattenAndMergeMap recursively flattens the given map into a new map
func flattenAndMergeMap(shadow map[string]interface{}, m map[string]interface{}, prefix string, delimiter string) map[string]interface{} {
func flattenAndMergeMap(shadow map[string]any, m map[string]any, prefix string, delimiter string) map[string]any {
if shadow != nil && prefix != "" && shadow[prefix] != nil {
return shadow
}
var m2 map[string]interface{}
var m2 map[string]any
if prefix != "" {
prefix += delimiter
}
for k, val := range m {
fullKey := prefix + k
switch val.(type) {
case map[string]interface{}:
m2 = val.(map[string]interface{})
case map[interface{}]interface{}:
switch val := val.(type) {
case map[string]any:
m2 = val
case map[any]any:
m2 = gconv.Map(val)
default:
// immediate value

View File

@ -50,7 +50,7 @@ var errorTests = []struct {
func TestDecode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := make(map[string]interface{})
m := make(map[string]any)
m["properties"] = pStr
res, err := gproperties.Encode(m)
if err != nil {
@ -79,7 +79,7 @@ func TestDecode(t *testing.T) {
func TestEncode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
m := make(map[string]interface{})
m := make(map[string]any)
m["properties"] = pStr
res, err := gproperties.Encode(m)
if err != nil {
@ -97,7 +97,7 @@ func TestEncode(t *testing.T) {
func TestToJson(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
res, err := gproperties.Encode(map[string]interface{}{
res, err := gproperties.Encode(map[string]any{
"sql": g.Map{
"userName": "admin",
"password": "123456",

View File

@ -16,7 +16,7 @@ import (
"github.com/gogf/gf/v2/internal/json"
)
func Encode(v interface{}) ([]byte, error) {
func Encode(v any) ([]byte, error) {
buffer := bytes.NewBuffer(nil)
if err := toml.NewEncoder(buffer).Encode(v); err != nil {
err = gerror.Wrap(err, `toml.Encoder.Encode failed`)
@ -25,8 +25,8 @@ func Encode(v interface{}) ([]byte, error) {
return buffer.Bytes(), nil
}
func Decode(v []byte) (interface{}, error) {
var result interface{}
func Decode(v []byte) (any, error) {
var result any
if err := toml.Unmarshal(v, &result); err != nil {
err = gerror.Wrap(err, `toml.Unmarshal failed`)
return nil, err
@ -34,7 +34,7 @@ func Decode(v []byte) (interface{}, error) {
return result, nil
}
func DecodeTo(v []byte, result interface{}) (err error) {
func DecodeTo(v []byte, result any) (err error) {
err = toml.Unmarshal(v, result)
if err != nil {
err = gerror.Wrap(err, `toml.Unmarshal failed`)

View File

@ -68,9 +68,9 @@ func TestDecode(t *testing.T) {
return
}
t.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
t.Assert(decodeStr.(map[string]any)["toml"], tomlStr)
decodeStr1 := make(map[string]interface{})
decodeStr1 := make(map[string]any)
err = gtoml.DecodeTo(res, &decodeStr1)
if err != nil {
t.Errorf("decodeTo failed. %v", err)
@ -86,7 +86,7 @@ func TestDecode(t *testing.T) {
return
}
decodeStr1 := make(map[string]interface{})
decodeStr1 := make(map[string]any)
err = gtoml.DecodeTo([]byte(tomlErr), &decodeStr1)
if err == nil {
t.Errorf("decodeTo failed. %v", err)

View File

@ -18,7 +18,7 @@ import (
)
// Decode parses `content` into and returns as map.
func Decode(content []byte) (map[string]interface{}, error) {
func Decode(content []byte) (map[string]any, error) {
res, err := convert(content)
if err != nil {
return nil, err
@ -31,7 +31,7 @@ func Decode(content []byte) (map[string]interface{}, error) {
}
// DecodeWithoutRoot parses `content` into a map, and returns the map without root level.
func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
func DecodeWithoutRoot(content []byte) (map[string]any, error) {
res, err := convert(content)
if err != nil {
return nil, err
@ -42,7 +42,7 @@ func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
return nil, err
}
for _, v := range m {
if r, ok := v.(map[string]interface{}); ok {
if r, ok := v.(map[string]any); ok {
return r, nil
}
}
@ -72,7 +72,7 @@ func XMLEscapeChars(b ...bool) {
// Encode encodes map `m` to an XML format content as bytes.
// The optional parameter `rootTag` is used to specify the XML root tag.
func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
func Encode(m map[string]any, rootTag ...string) ([]byte, error) {
b, err := mxj.Map(m).Xml(rootTag...)
if err != nil {
err = gerror.Wrapf(err, `mxj.Map.Xml failed`)
@ -82,7 +82,7 @@ func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
// EncodeWithIndent encodes map `m` to an XML format content as bytes with indent.
// The optional parameter `rootTag` is used to specify the XML root tag.
func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
func EncodeWithIndent(m map[string]any, rootTag ...string) ([]byte, error) {
b, err := mxj.Map(m).XmlIndent("", "\t", rootTag...)
if err != nil {
err = gerror.Wrapf(err, `mxj.Map.XmlIndent failed`)

View File

@ -96,8 +96,8 @@ func Test_Decode1(t *testing.T) {
if err != nil {
t.Errorf("gxml decode error. %s", dstXml)
}
s := srcMap["doc"].(map[string]interface{})
d := dstMap["doc"].(map[string]interface{})
s := srcMap["doc"].(map[string]any)
d := dstMap["doc"].(map[string]any)
for kk, vv := range s {
if vv.(string) != d[kk].(string) {
t.Errorf("convert to map error. src:%v, dst:%v", vv, d[kk])
@ -113,9 +113,9 @@ func Test_Decode2(t *testing.T) {
`
m, err := gxml.Decode([]byte(content))
t.AssertNil(err)
t.Assert(m["doc"].(map[string]interface{})["username"], "johngcn")
t.Assert(m["doc"].(map[string]interface{})["password1"], "123456")
t.Assert(m["doc"].(map[string]interface{})["password2"], "123456")
t.Assert(m["doc"].(map[string]any)["username"], "johngcn")
t.Assert(m["doc"].(map[string]any)["password1"], "123456")
t.Assert(m["doc"].(map[string]any)["password2"], "123456")
})
}
@ -133,14 +133,14 @@ func Test_DecodeWitoutRoot(t *testing.T) {
}
func Test_Encode(t *testing.T) {
m := make(map[string]interface{})
v := map[string]interface{}{
m := make(map[string]any)
v := map[string]any{
"string": "hello world",
"int": 123,
"float": 100.92,
"bool": true,
}
m["root"] = interface{}(v)
m["root"] = any(v)
xmlStr, err := gxml.Encode(m)
if err != nil {
@ -155,14 +155,14 @@ func Test_Encode(t *testing.T) {
}
func Test_EncodeIndent(t *testing.T) {
m := make(map[string]interface{})
v := map[string]interface{}{
m := make(map[string]any)
v := map[string]any{
"string": "hello world",
"int": 123,
"float": 100.92,
"bool": true,
}
m["root"] = interface{}(v)
m["root"] = any(v)
_, err := gxml.EncodeWithIndent(m, "xml")
if err != nil {
@ -211,8 +211,8 @@ func Test_Issue3716(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
xml = `<Person><Bio>I am a software developer &amp; I love coding.</Bio><Email>john.doe@example.com</Email><Name>&lt;&gt;&amp;&apos;&quot;AAA</Name></Person>`
m = map[string]interface{}{
"Person": map[string]interface{}{
m = map[string]any{
"Person": map[string]any{
"Name": "<>&'\"AAA",
"Email": "john.doe@example.com",
"Bio": "I am a software developer & I love coding.",

View File

@ -19,7 +19,7 @@ import (
)
// Encode encodes `value` to an YAML format content as bytes.
func Encode(value interface{}) (out []byte, err error) {
func Encode(value any) (out []byte, err error) {
if out, err = yaml.Marshal(value); err != nil {
err = gerror.Wrap(err, `yaml.Marshal failed`)
}
@ -27,7 +27,7 @@ func Encode(value interface{}) (out []byte, err error) {
}
// EncodeIndent encodes `value` to an YAML format content with indent as bytes.
func EncodeIndent(value interface{}, indent string) (out []byte, err error) {
func EncodeIndent(value any, indent string) (out []byte, err error) {
out, err = Encode(value)
if err != nil {
return
@ -48,9 +48,9 @@ func EncodeIndent(value interface{}, indent string) (out []byte, err error) {
}
// Decode parses `content` into and returns as map.
func Decode(content []byte) (map[string]interface{}, error) {
func Decode(content []byte) (map[string]any, error) {
var (
result map[string]interface{}
result map[string]any
err error
)
if err = yaml.Unmarshal(content, &result); err != nil {
@ -61,7 +61,7 @@ func Decode(content []byte) (map[string]interface{}, error) {
}
// DecodeTo parses `content` into `result`.
func DecodeTo(value []byte, result interface{}) (err error) {
func DecodeTo(value []byte, result any) (err error) {
err = yaml.Unmarshal(value, result)
if err != nil {
err = gerror.Wrap(err, `yaml.Unmarshal failed`)
@ -72,7 +72,7 @@ func DecodeTo(value []byte, result interface{}) (err error) {
// ToJson converts `content` to JSON format content.
func ToJson(content []byte) (out []byte, err error) {
var (
result interface{}
result any
)
if result, err = Decode(content); err != nil {
return nil, err

View File

@ -76,7 +76,7 @@ func Test_Decode(t *testing.T) {
result, err := gyaml.Decode([]byte(yamlStr))
t.AssertNil(err)
t.Assert(result, map[string]interface{}{
t.Assert(result, map[string]any{
"url": "https://goframe.org",
"server": g.Slice{"120.168.117.21", "120.168.117.22"},
"pi": 3.14,
@ -88,10 +88,10 @@ func Test_Decode(t *testing.T) {
func Test_DecodeTo(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
result := make(map[string]interface{})
result := make(map[string]any)
err := gyaml.DecodeTo([]byte(yamlStr), &result)
t.AssertNil(err)
t.Assert(result, map[string]interface{}{
t.Assert(result, map[string]any{
"url": "https://goframe.org",
"server": g.Slice{"120.168.117.21", "120.168.117.22"},
"pi": 3.14,
@ -106,7 +106,7 @@ func Test_DecodeError(t *testing.T) {
_, err := gyaml.Decode([]byte(yamlErr))
t.AssertNE(err, nil)
result := make(map[string]interface{})
result := make(map[string]any)
err = gyaml.DecodeTo([]byte(yamlErr), &result)
t.AssertNE(err, nil)
})
@ -143,10 +143,6 @@ func Test_ToJson(t *testing.T) {
}
p := gjson.New(res)
if err != nil {
t.Errorf("parser failed. %v", err)
return
}
expectJson, err := p.ToJson()
if err != nil {
t.Errorf("parser ToJson failed. %v", err)