2019-07-03 22:09:35 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2019-07-04 11:11:41 +08:00
|
|
|
package structs_test
|
2019-07-03 22:09:35 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/structs"
|
2019-07-03 22:09:35 +08:00
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
2019-07-03 22:09:35 +08:00
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-07-03 22:09:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Basic(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-03 22:09:35 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Name string `params:"name"`
|
|
|
|
Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
|
|
|
|
}
|
|
|
|
var user User
|
2020-11-08 14:25:17 +08:00
|
|
|
m, _ := structs.TagMapName(user, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
|
|
|
|
m, _ = structs.TagMapName(&user, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
|
2019-07-03 22:09:35 +08:00
|
|
|
|
2020-11-08 14:25:17 +08:00
|
|
|
m, _ = structs.TagMapName(&user, []string{"params", "my-tag1"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
|
|
|
|
m, _ = structs.TagMapName(&user, []string{"my-tag1", "params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass1": "Pass"})
|
|
|
|
m, _ = structs.TagMapName(&user, []string{"my-tag2", "params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass2": "Pass"})
|
2019-07-03 22:09:35 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-03 22:09:35 +08:00
|
|
|
type Base struct {
|
|
|
|
Pass1 string `params:"password1"`
|
|
|
|
Pass2 string `params:"password2"`
|
|
|
|
}
|
|
|
|
type UserWithBase struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Base `params:"base"`
|
|
|
|
}
|
|
|
|
user := new(UserWithBase)
|
2020-11-08 14:25:17 +08:00
|
|
|
m, _ := structs.TagMapName(user, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{
|
2019-07-04 11:11:41 +08:00
|
|
|
"base": "Base",
|
|
|
|
"password1": "Pass1",
|
|
|
|
"password2": "Pass2",
|
|
|
|
})
|
2019-07-03 22:09:35 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-03 22:09:35 +08:00
|
|
|
type Base struct {
|
|
|
|
Pass1 string `params:"password1"`
|
|
|
|
Pass2 string `params:"password2"`
|
|
|
|
}
|
2020-10-30 22:04:34 +08:00
|
|
|
type UserWithEmbeddedAttribute struct {
|
2019-07-03 22:09:35 +08:00
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Base
|
|
|
|
}
|
2020-10-30 22:04:34 +08:00
|
|
|
type UserWithoutEmbeddedAttribute struct {
|
2019-07-03 22:09:35 +08:00
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Pass Base
|
|
|
|
}
|
2020-10-30 22:04:34 +08:00
|
|
|
user1 := new(UserWithEmbeddedAttribute)
|
|
|
|
user2 := new(UserWithoutEmbeddedAttribute)
|
2020-11-08 14:25:17 +08:00
|
|
|
m, _ := structs.TagMapName(user1, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{"password1": "Pass1", "password2": "Pass2"})
|
|
|
|
m, _ = structs.TagMapName(user2, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{})
|
2019-07-03 22:09:35 +08:00
|
|
|
})
|
|
|
|
}
|
2020-10-22 15:16:31 +08:00
|
|
|
|
|
|
|
func Test_StructOfNilPointer(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Name string `params:"name"`
|
|
|
|
Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
|
|
|
|
}
|
|
|
|
var user *User
|
2020-11-08 14:25:17 +08:00
|
|
|
m, _ := structs.TagMapName(user, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
|
|
|
|
m, _ = structs.TagMapName(&user, []string{"params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
|
2020-10-22 15:16:31 +08:00
|
|
|
|
2020-11-08 14:25:17 +08:00
|
|
|
m, _ = structs.TagMapName(&user, []string{"params", "my-tag1"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass": "Pass"})
|
|
|
|
m, _ = structs.TagMapName(&user, []string{"my-tag1", "params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass1": "Pass"})
|
|
|
|
m, _ = structs.TagMapName(&user, []string{"my-tag2", "params"})
|
|
|
|
t.Assert(m, g.Map{"name": "Name", "pass2": "Pass"})
|
2020-10-22 15:16:31 +08:00
|
|
|
})
|
|
|
|
}
|
2020-11-08 16:21:09 +08:00
|
|
|
|
|
|
|
func Test_MapField(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Name string `params:"name"`
|
|
|
|
Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
|
|
|
|
}
|
|
|
|
var user *User
|
|
|
|
m, _ := structs.MapField(user, []string{"params"})
|
|
|
|
t.Assert(len(m), 3)
|
|
|
|
_, ok := m["Id"]
|
|
|
|
t.Assert(ok, true)
|
|
|
|
_, ok = m["Name"]
|
|
|
|
t.Assert(ok, false)
|
|
|
|
_, ok = m["name"]
|
|
|
|
t.Assert(ok, true)
|
|
|
|
_, ok = m["Pass"]
|
|
|
|
t.Assert(ok, false)
|
|
|
|
_, ok = m["pass"]
|
|
|
|
t.Assert(ok, true)
|
|
|
|
})
|
|
|
|
}
|