fix issue @1380

This commit is contained in:
John Guo
2021-09-15 21:17:45 +08:00
parent 64a5cedcdf
commit 68e760d13a
17 changed files with 542 additions and 176 deletions

View File

@ -14,11 +14,6 @@ import (
"github.com/gogf/gf/util/gconv"
)
// Interface converts and returns `r` as type of interface{}.
func (r Record) Interface() interface{} {
return r
}
// Json converts `r` to JSON format content.
func (r Record) Json() string {
content, _ := gparser.VarToJson(r.Map())

View File

@ -13,11 +13,6 @@ import (
"math"
)
// Interface converts and returns `r` as type of interface{}.
func (r Result) Interface() interface{} {
return r
}
// IsEmpty checks and returns whether `r` is empty.
func (r Result) IsEmpty() bool {
return r.Len() == 0
@ -33,7 +28,7 @@ func (r Result) Size() int {
return r.Len()
}
// Chunk splits an Result into multiple Results,
// Chunk splits a Result into multiple Results,
// the size of each array is determined by `size`.
// The last chunk may contain less than size elements.
func (r Result) Chunk(size int) []Result {

View File

@ -14,6 +14,7 @@ import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"reflect"
"testing"
@ -480,3 +481,119 @@ func Test_Scan_AutoFilteringByStructAttributes(t *testing.T) {
t.Assert(users[0].Id, 1)
})
}
func Test_Scan_JsonAttributes(t *testing.T) {
type GiftImage struct {
Uid string `json:"uid"`
Url string `json:"url"`
Status string `json:"status"`
Name string `json:"name"`
}
type GiftComment struct {
Name string `json:"name"`
Field string `json:"field"`
Required bool `json:"required"`
}
type Prop struct {
Name string `json:"name"`
Values []string `json:"values"`
}
type Sku struct {
GiftId int64 `json:"gift_id"`
Name string `json:"name"`
ScorePrice int `json:"score_price"`
MarketPrice int `json:"market_price"`
CostPrice int `json:"cost_price"`
Stock int `json:"stock"`
}
type Covers struct {
List []GiftImage `json:"list"`
}
type GiftEntity struct {
Id int64 `json:"id"`
StoreId int64 `json:"store_id"`
GiftType int `json:"gift_type"`
GiftName string `json:"gift_name"`
Description string `json:"description"`
Covers Covers `json:"covers"`
Cover string `json:"cover"`
GiftCategoryId []int64 `json:"gift_category_id"`
HasProps bool `json:"has_props"`
OutSn string `json:"out_sn"`
IsLimitSell bool `json:"is_limit_sell"`
LimitSellType int `json:"limit_sell_type"`
LimitSellCycle string `json:"limit_sell_cycle"`
LimitSellCycleCount int `json:"limit_sell_cycle_count"`
LimitSellCustom bool `json:"limit_sell_custom"` // 只允许特定会员兑换
LimitCustomerTags []int64 `json:"limit_customer_tags"` // 允许兑换的成员
ScorePrice int `json:"score_price"`
MarketPrice float64 `json:"market_price"`
CostPrice int `json:"cost_price"`
Stock int `json:"stock"`
Props []Prop `json:"props"`
Skus []Sku `json:"skus"`
ExpressType []string `json:"express_type"`
Comments []GiftComment `json:"comments"`
Content string `json:"content"`
AtLeastRechargeCount int `json:"at_least_recharge_count"`
Status int `json:"status"`
}
type User struct {
Id int
Passport string
}
var (
table = "jfy_gift"
)
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1380.sql`), ";")
for _, v := range array {
if _, err := db.Exec(v); err != nil {
gtest.Error(err)
}
}
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
var (
entity = new(GiftEntity)
err = db.Model(table).Where("id", 17).Scan(entity)
)
t.AssertNil(err)
t.Assert(len(entity.Skus), 2)
t.Assert(entity.Skus[0].Name, "red")
t.Assert(entity.Skus[0].Stock, 10)
t.Assert(entity.Skus[0].GiftId, 1)
t.Assert(entity.Skus[0].CostPrice, 80)
t.Assert(entity.Skus[0].ScorePrice, 188)
t.Assert(entity.Skus[0].MarketPrice, 388)
t.Assert(entity.Skus[1].Name, "blue")
t.Assert(entity.Skus[1].Stock, 100)
t.Assert(entity.Skus[1].GiftId, 2)
t.Assert(entity.Skus[1].CostPrice, 81)
t.Assert(entity.Skus[1].ScorePrice, 200)
t.Assert(entity.Skus[1].MarketPrice, 288)
t.Assert(entity.Id, 17)
t.Assert(entity.StoreId, 100004)
t.Assert(entity.GiftType, 1)
t.Assert(entity.GiftName, "GIFT")
t.Assert(entity.Description, "支持个性定制的父亲节老师长辈的专属礼物")
t.Assert(len(entity.Covers.List), 3)
t.Assert(entity.OutSn, "259402")
t.Assert(entity.LimitCustomerTags, "[]")
t.Assert(entity.ScorePrice, 10)
t.Assert(len(entity.Props), 1)
t.Assert(len(entity.Comments), 2)
t.Assert(entity.Status, 99)
t.Assert(entity.Content, `<p>礼品详情</p>`)
})
}

35
database/gdb/testdata/issue1380.sql vendored Normal file
View File

@ -0,0 +1,35 @@
CREATE TABLE `jfy_gift` (
`id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT,
`gift_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '礼品名称',
`at_least_recharge_count` int(0) UNSIGNED NOT NULL DEFAULT 1 COMMENT '最少兑换数量',
`comments` json NOT NULL COMMENT '礼品留言',
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '礼品详情',
`cost_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '成本价',
`cover` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '封面',
`covers` json NOT NULL COMMENT '礼品图片库',
`description` varchar(62) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '礼品备注',
`express_type` json NOT NULL COMMENT '配送方式',
`gift_type` int(0) NOT NULL COMMENT '礼品类型1实物2虚拟3优惠券4积分券',
`has_props` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否有多个属性',
`is_limit_sell` tinyint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否限购',
`limit_customer_tags` json NOT NULL COMMENT '语序购买的会员标签',
`limit_sell_custom` tinyint(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否开启允许购买的会员标签',
`limit_sell_cycle` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '限购周期',
`limit_sell_cycle_count` int(0) NOT NULL COMMENT '限购期内允许购买的数量',
`limit_sell_type` tinyint(0) NOT NULL COMMENT '限购类型',
`market_price` decimal(10, 2) NOT NULL COMMENT '市场价',
`out_sn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '内部编码',
`props` json NOT NULL COMMENT '规格',
`skus` json NOT NULL COMMENT 'SKU',
`score_price` decimal(10, 2) NOT NULL COMMENT '兑换所需积分',
`stock` int(0) NOT NULL COMMENT '库存',
`create_at` datetime(0) NOT NULL COMMENT '创建日期',
`store_id` int(0) NOT NULL COMMENT '所属商城',
`status` int(0) UNSIGNED NULL DEFAULT 1 COMMENT '1下架20审核中30复审中99上架',
`view_count` int(0) NOT NULL DEFAULT 0 COMMENT '访问量',
`sell_count` int(0) NULL DEFAULT 0 COMMENT '销量',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `jfy_gift` VALUES (17, 'GIFT', 1, '[{\"name\": \"身份证\", \"field\": \"idcard\", \"required\": false}, {\"name\": \"留言2\", \"field\": \"text\", \"required\": false}]', '<p>礼品详情</p>', 0.00, '', '{\"list\": [{\"uid\": \"vc-upload-1629292486099-3\", \"url\": \"https://cdn.taobao.com/sULsYiwaOPjsKGoBXwKtuewPzACpBDfQ.jpg\", \"name\": \"O1CN01OH6PIP1Oc5ot06U17_!!922361725.jpg\", \"status\": \"done\"}, {\"uid\": \"vc-upload-1629292486099-4\", \"url\": \"https://cdn.taobao.com/lqLHDcrFTgNvlWyXfLYZwmsrODzIBtFH.jpg\", \"name\": \"O1CN018hBckI1Oc5ouc8ppl_!!922361725.jpg\", \"status\": \"done\"}, {\"uid\": \"vc-upload-1629292486099-5\", \"url\": \"https://cdn.taobao.com/pvqyutXckICmHhbPBQtrVLHuMlXuGxUg.jpg\", \"name\": \"O1CN0185Ubp91Oc5osQTTcc_!!922361725.jpg\", \"status\": \"done\"}]}', '支持个性定制的父亲节老师长辈的专属礼物', '[\"快递包邮\", \"同城配送\"]', 1, 0, 0, '[]', 0, 'day', 0, 1, 0.00, '259402', '[{\"name\": \"颜色\", \"values\": [\"红色\", \"蓝色\"]}]', '[{\"name\": \"red\", \"stock\": 10, \"gift_id\": 1, \"cost_price\": 80, \"score_price\": 188, \"market_price\": 388}, {\"name\": \"blue\", \"stock\": 100, \"gift_id\": 2, \"cost_price\": 81, \"score_price\": 200, \"market_price\": 288}]', 10.00, 0, '2021-08-18 21:26:13', 100004, 99, 0, 0);