Assisted-by: GLM 5.3 Flash
This commit is contained in:
@@ -309,31 +309,58 @@ func (d *decoder) assignMap(tbl map[string]any, dst reflect.Value) error {
|
||||
}
|
||||
|
||||
func (d *decoder) assignSlice(items []any, dst reflect.Value) error {
|
||||
if dst.Kind() != reflect.Slice {
|
||||
switch dst.Kind() {
|
||||
case reflect.Slice:
|
||||
out := reflect.MakeSlice(dst.Type(), len(items), len(items))
|
||||
for i, item := range items {
|
||||
if err := d.assign(item, out.Index(i)); err != nil {
|
||||
return newDecodeError(fmt.Sprintf("[%d]", i), err)
|
||||
}
|
||||
}
|
||||
dst.Set(out)
|
||||
return nil
|
||||
case reflect.Array:
|
||||
// A fixed-size array takes the elements in place; a length mismatch is
|
||||
// the error, because a TOML array carries no way to name a default for
|
||||
// the elements it is short of, and the surplus has nowhere to go.
|
||||
if dst.Len() != len(items) {
|
||||
return fmt.Errorf("interpres: cannot assign %d elements to %s", len(items), dst.Type())
|
||||
}
|
||||
for i, item := range items {
|
||||
if err := d.assign(item, dst.Index(i)); err != nil {
|
||||
return newDecodeError(fmt.Sprintf("[%d]", i), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("interpres: cannot assign array to %s", dst.Type())
|
||||
}
|
||||
out := reflect.MakeSlice(dst.Type(), len(items), len(items))
|
||||
for i, item := range items {
|
||||
if err := d.assign(item, out.Index(i)); err != nil {
|
||||
return newDecodeError(fmt.Sprintf("[%d]", i), err)
|
||||
}
|
||||
}
|
||||
dst.Set(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *decoder) assignTableSlice(items []map[string]any, dst reflect.Value) error {
|
||||
if dst.Kind() != reflect.Slice {
|
||||
switch dst.Kind() {
|
||||
case reflect.Slice:
|
||||
out := reflect.MakeSlice(dst.Type(), len(items), len(items))
|
||||
for i, item := range items {
|
||||
if err := d.assign(item, out.Index(i)); err != nil {
|
||||
return newDecodeError(fmt.Sprintf("[%d]", i), err)
|
||||
}
|
||||
}
|
||||
dst.Set(out)
|
||||
return nil
|
||||
case reflect.Array:
|
||||
if dst.Len() != len(items) {
|
||||
return fmt.Errorf("interpres: cannot assign %d elements to %s", len(items), dst.Type())
|
||||
}
|
||||
for i, item := range items {
|
||||
if err := d.assign(item, dst.Index(i)); err != nil {
|
||||
return newDecodeError(fmt.Sprintf("[%d]", i), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("interpres: cannot assign array of tables to %s", dst.Type())
|
||||
}
|
||||
out := reflect.MakeSlice(dst.Type(), len(items), len(items))
|
||||
for i, item := range items {
|
||||
if err := d.assign(item, out.Index(i)); err != nil {
|
||||
return newDecodeError(fmt.Sprintf("[%d]", i), err)
|
||||
}
|
||||
}
|
||||
dst.Set(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- low-level setters -----------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user