前面关于复杂类型的转换功能如果大家觉得还不够的话,那么您可以了解下Scan转换方法,该方法可以实现对任意参数到struct/struct数组/map/map数组的转换,并且根据开发者输入的转换目标参数自动识别执行转换。

该方法定义如下:

// Scan automatically calls MapToMap, MapToMaps, Struct or Structs function according to
// the type of parameter `pointer` to implement the converting.
// It calls function MapToMap if `pointer` is type of *map to do the converting.
// It calls function MapToMaps if `pointer` is type of *[]map/*[]*map to do the converting.
// It calls function Struct if `pointer` is type of *struct/**struct to do the converting.
// It calls function Structs if `pointer` is type of *[]struct/*[]*struct to do the converting.
func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)

我们接下来看几个示例便可快速理解。

自动识别转换Struct

package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	type User struct {
		Uid  int
		Name string
	}
	params := g.Map{
		"uid":  1,
		"name": "john",
	}
	var user *User
	if err := gconv.Scan(params, &user); err != nil {
		panic(err)
	}
	g.Dump(user)
}

执行后,输出结果为:

{
	"Name": "john",
	"Uid": 1
}

自动识别转换Struct数组

package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	type User struct {
		Uid  int
		Name string
	}
	params := g.Slice{
		g.Map{
			"uid":  1,
			"name": "john",
		},
		g.Map{
			"uid":  2,
			"name": "smith",
		},
	}
	var users []*User
	if err := gconv.Scan(params, &users); err != nil {
		panic(err)
	}
	g.Dump(users)
}

执行后,终端输出:

[
    {
            "Uid": 1,
            "Name": "john"
    },
    {
            "Uid": 2,
            "Name": "smith"
    }
]

自动识别转换Map

package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var (
		user   map[string]string
		params = g.Map{
			"uid":  1,
			"name": "john",
		}
	)
	if err := gconv.Scan(params, &user); err != nil {
		panic(err)
	}
	g.Dump(user)
}

执行后,输出结果为:

{
   "Uid": "1", 
   "Name": "john"
}

自动识别转换Map数组

package main

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var (
		users  []map[string]string
		params = g.Slice{
			g.Map{
				"uid":  1,
				"name": "john",
			},
			g.Map{
				"uid":  2,
				"name": "smith",
			},
		}
	)
	if err := gconv.Scan(params, &users); err != nil {
		panic(err)
	}
	g.Dump(users)
}

执行后,输出结果为:

[
    {
            "name": "john",
            "uid": "1"
    },
    {
            "name": "smith",
            "uid": "2"
    }
]
















Content Menu

  • No labels

2 Comments

  1. gjson.Json在继承的结构体中使用gconv.Scan转换,会导致值丢失,其他类型正常,请问这个该怎么解决?

    代码如下:
    type Test struct {
    Id int64 `json:"id" description:""`
    Flag *gjson.Json `json:"flag" description:"标签"`
    Title string `json:"title" description:"标题"`
    Status int `json:"status" description:"状态"`
    CreatedAt *gtime.Time `json:"createdAt" description:"创建时间"`
    UpdatedAt *gtime.Time `json:"updatedAt" description:"修改时间"`
    }

    var (
    A *Test
    B *Test
    )

    A = &Test{
    Id: 2,
    Flag: gjson.New("[\"1\", \"2\"]"),
    Title: "测试",
    Status: 1,
    CreatedAt: gtime.Now(),
    UpdatedAt: gtime.Now(),
    }

    type Test2 struct {
    Test
    }
    type Test3 struct {
    Test
    }
    var (
    A2 Test2
    B2 Test3
    )

    if err = gconv.Scan(A, &B); err != nil {
    return nil, err
    }
    g.DumpWithType("A", A)
    g.DumpWithType("B", B)

    if err = gconv.Scan(B, &A2); err != nil {
    return nil, err
    }
    g.DumpWithType("A2", A2)

    if err = gconv.Scan(A2, &B2); err != nil {
    return nil, err
    }
    g.DumpWithType("B2", B2)



    结果如下:

    string(1) "A"
    *admin.Test(6) {
        Id:        int64(2),
        Flag:      *gjson.Json(13) "[\"1\",\"2\"]",
        Title:     string(6) "测试",
        Status:    int(1),
        CreatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
        UpdatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
    }
    string(1) "B"
    *admin.Test(6) {
        Id:        int64(2),
        Flag:      *gjson.Json(13) "[\"1\",\"2\"]",
        Title:     string(6) "测试",
        Status:    int(1),
        CreatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
        UpdatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
    }
    string(2) "A2"
    admin.Test2(6) {
        Id:        int64(2),
        Flag:      *gjson.Json(13) "[\"1\",\"2\"]",
        Title:     string(6) "测试",
        Status:    int(1),
        CreatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
        UpdatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
    }
    string(2) "B2"
    admin.Test3(6) {
        Id:        int64(2),
        Flag:      *gjson.Json(4) "null",
        Title:     string(6) "测试",
        Status:    int(1),
        CreatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
        UpdatedAt: *gtime.Time(19) "2022-12-29 18:14:55",
    }





  2. 自动识别转换Map例子输出的数据有误

    应该是

    {
        "uid":  "1",
        "name": "john",
    }