improve gdb.Model.ScanList

This commit is contained in:
John 2020-09-28 23:52:02 +08:00
parent c931032f08
commit 3617e51c01
4 changed files with 35 additions and 11 deletions

View File

@ -181,7 +181,7 @@ func (r Result) ScanList(listPointer interface{}, attributeName string, relation
}
}
if len(relationDataMap) > 0 && !relationValue.IsValid() {
return fmt.Errorf(`invalid relation: %s, %s`, relation[0], relation[1])
return fmt.Errorf(`invalid relation: "%s:%s"`, relation[0], relation[1])
}
switch attrKind {
case reflect.Array, reflect.Slice:
@ -196,7 +196,7 @@ func (r Result) ScanList(listPointer interface{}, attributeName string, relation
}
} else {
// May be the attribute does not exist yet.
return fmt.Errorf(`invalid relation: %s, %s`, relation[0], relation[1])
return fmt.Errorf(`invalid relation: "%s:%s"`, relation[0], relation[1])
}
} else {
return fmt.Errorf(`relationKey should not be empty as field "%s" is slice`, attributeName)
@ -207,15 +207,25 @@ func (r Result) ScanList(listPointer interface{}, attributeName string, relation
if len(relationDataMap) > 0 {
relationField = relationValue.FieldByName(relationAttrName)
if relationField.IsValid() {
if err = gconv.Struct(relationDataMap[gconv.String(relationField.Interface())], e); err != nil {
v := relationDataMap[gconv.String(relationField.Interface())]
if v == nil {
// There's no relational data.
continue
}
if err = gconv.Struct(v, e); err != nil {
return err
}
} else {
// May be the attribute does not exist yet.
return fmt.Errorf(`invalid relation: %s, %s`, relation[0], relation[1])
return fmt.Errorf(`invalid relation: "%s:%s"`, relation[0], relation[1])
}
} else {
if err = gconv.Struct(r[i], e); err != nil {
v := r[i]
if v == nil {
// There's no relational data.
continue
}
if err = gconv.Struct(v, e); err != nil {
return err
}
}
@ -226,15 +236,25 @@ func (r Result) ScanList(listPointer interface{}, attributeName string, relation
if len(relationDataMap) > 0 {
relationField = relationValue.FieldByName(relationAttrName)
if relationField.IsValid() {
if err = gconv.Struct(relationDataMap[gconv.String(relationField.Interface())], e); err != nil {
v := relationDataMap[gconv.String(relationField.Interface())]
if v == nil {
// There's no relational data.
continue
}
if err = gconv.Struct(v, e); err != nil {
return err
}
} else {
// May be the attribute does not exist yet.
return fmt.Errorf(`invalid relation: %s, %s`, relation[0], relation[1])
return fmt.Errorf(`invalid relation: "%s:%s"`, relation[0], relation[1])
}
} else {
if err = gconv.Struct(r[i], e); err != nil {
v := r[i]
if v == nil {
// There's no relational data.
continue
}
if err = gconv.Struct(v, e); err != nil {
return err
}
}

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/gqcn/structs v1.1.1
github.com/grokify/html-strip-tags-go v0.0.0-20190921062105-daaa06bf1aaf
github.com/json-iterator/go v1.1.10
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.1
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
golang.org/x/text v0.3.2

View File

@ -50,7 +50,8 @@ func StructDeep(params interface{}, pointer interface{}, mapping ...map[string]s
// doStruct is the core internal converting function for any data to struct recursively or not.
func doStruct(params interface{}, pointer interface{}, recursive bool, mapping ...map[string]string) (err error) {
if params == nil {
return gerror.New("params cannot be nil")
// If <params> is nil, no conversion.
return nil
}
if pointer == nil {
return gerror.New("object pointer cannot be nil")
@ -73,7 +74,8 @@ func doStruct(params interface{}, pointer interface{}, recursive bool, mapping .
// DO NOT use MapDeep here.
paramsMap := Map(params)
if paramsMap == nil {
return gerror.Newf("invalid params: %v", params)
//return gerror.Newf("invalid params: %v", params)
return nil
}
// Using reflect to do the converting,

View File

@ -30,7 +30,8 @@ func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string]
// it will create the struct/pointer internally.
func doStructs(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) (err error) {
if params == nil {
return gerror.New("params cannot be nil")
// If <params> is nil, no conversion.
return nil
}
if pointer == nil {
return gerror.New("object pointer cannot be nil")