创建自定义类型后,使用reflectutils.Set失效
soease opened this issue · comments
使用如文档中: https://docs.qor5.com/presets-guide/editing-customizations.html ,
type MyFile string
type Product struct {
ID int
Title string
MainImage MyFile
abc string
}
其余基本不变,发现 reflectutils.Set(obj, “abc”, "aaaa") 并不生效。交换位置为
type Product struct {
ID int
Title string
abc string
MainImage MyFile
}
就成功了.
或者直接 reflectutils.Set(obj, “Title”, "aaaa") 也是成功的
这是我巧合了啥?
你这里的abc字段是unexported字段,reflect没法给它设值的。你把它改成Abc再试试
谢谢你的回答。
可能是我举例不够注意,这里abc实际中已是大写。现象依旧。
看你描述是reflectutils.Set
有问题,但我试了下并没有问题 https://go.dev/play/p/9NYvCIlc8PC 。你能给下完整的代码吗
// 文章 type Document struct { gorm.Model
list:"-" edit:"-"Categorize DocumentCategorize
list:"-" edit:"-"// 分类 CategorizeId uint
list:"-" edit:"-"// Title string
name:"标题" // 文章标题 PicUrl string
list:"-"Pic PicFile
list:"-" name:"图片" // 自定义类型之后的,使用 reflectutils.Set 修改其值无效 Author string
name:"作者" list:"-" // 作者 Source string
name:"来源" list:"-" // 来源 Keyword string
name:"关键词" // 关键词 Content string
name:"内容" list:"-" // 内容 OrderBy uint
name:"排序" list:"-" // 排序 UserCategorize UserCategorize
list:"-" edit:"-" // 用户分类 UserCategorizeId uint
list:"-" edit:"-"` //
}
`
`
b.FieldDefaults(presets.WRITE).FieldType(PicFile("")).
ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
//val := field.Value(obj).(PicFile)
doc := obj.(*Document)
val := doc.PicUrl
var img []h.HTMLComponent
if len(string(val)) > 0 { // 有内容则显示为img
for _, n := range strings.Split(string(val), ",") { // 可以保存多个图片
imgCode := h.Img(sysSet["DocUpUrl"]+n).
Attr("onclick", "javascript:removePic(this)").
Attr("title", "点击可删除")
img = append(img, imgCode)
}
}
var er h.HTMLComponent
if len(field.Errors) > 0 {
er = h.Div().Text(field.Errors[0]).Style("color:red")
}
return h.Div(
h.Div(img...).Class("pic"),
er, // 存在错误时才显示错误信息
h.Div(h.Input("").Type("file").Attr(web.VFieldName(fmt.Sprintf("%s_NewFile", field.Name))...)),
).Style("margin-bottom:20px")
}).
SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
user := login.GetCurrentUser(ctx.R).(*User) // 获取用户ID
ff, fh, err := ctx.R.FormFile(fmt.Sprintf("%s_NewFile", field.Name)) // ff为上传文件编码 fh包含文件名及类型
if ff == nil || err != nil {
// err = reflectutils.Set(obj, field.Name, PicFile(doc.PicUrl))
return nil
}
// fh.Header["Content-Type"] 文件类型, fh.Filename 文件名
req, err := http.NewRequest("POST", sysSet["Url_DocPic"], ff) // 发送文件内容
if err != nil {
return
}
req.Header.Add("Filename", fh.Filename) // 通过header传输文件名
req.Header.Add("UserID", fmt.Sprintf("%d", user.ID)) // 通过header传输用户ID , 注意:接收的Header为首字母大写,其它均为小写
var res *http.Response
res, err = http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
// 获取返回信息
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode == 500 {
err = fmt.Errorf("%s", string(b))
return err
}
//err = reflectutils.Set(obj, field.Name, PicFile(b))
if PicFile(b) != "" {
doc := obj.(*Document)
if doc.PicUrl == "" {
err = reflectutils.Set(obj, "PicUrl", string(b))
} else {
err = reflectutils.Set(obj, "PicUrl", fmt.Sprintf("%s,%s", doc.PicUrl, string(b)))
}
}
return nil
})`