将传递为空接口的 golang 切片追加内容。

huangapple go评论85阅读模式
英文:

Append to golang slice passed as empty interface

问题

如何向空接口(已验证为* []struct)追加内容?

func main() {
  var mySlice []myStruct // myStruct可以是任何结构体(动态的)
  decode(&mySlice, "...")
}

func decode(dest interface{}, src string) {
  // 假设已验证dest为* []struct
  var modelType reflect.Type = getStructType(dest)
  rows, fields := getRows(src)
  for _, row := range rows {
    // 创建类型为modelType的新结构体并分配所有字段
    model := reflect.New(modelType)
    for field := fields {
      fieldValue := getRowValue(row, field)
      model.Elem().FieldByName(field).Set(fieldValue)
    }
    castedModelRow := model.Elem().Interface()
      
    // 将model追加到dest;如何实现?
    // dest = append(dest, castedModelRow)
  }
}

我尝试过的方法:

这样做会导致恐慌:reflect: call of reflect.Append on ptr Value(因为我们传递的是&mySlice而不是mySlice)

dest = reflect.Append(reflect.ValueOf(dest), reflect.ValueOf(castedModelRow))

这个方法可以工作,但在调用decode函数后,main函数中的len(mySlice)仍然为0。

func decode(dest interface{}, src string) {
  ...
  result := reflect.MakeSlice(reflect.SliceOf(modelType), rowCount, rowCount)
  for _, row : range rows {
    ...
    result = reflect.Append(result, reflect.ValueOf(castedModelRow))
  }
  dest = reflect.ValueOf(result)
}
英文:

How to append to empty interface (that has been verified to be a *[]struct)?

func main() {
  var mySlice []myStruct // myStruct can be any struct (dynamic)
  decode(&mySlice, "...")
}

func decode(dest interface{}, src string) {
  // assume dest has been verified to be *[]struct
  var modelType reflect.Type = getStructType(dest)
  rows, fields := getRows(src)
  for _, row := range rows {
    // create new struct of type modelType and assign all fields
    model := reflect.New(modelType)
    for field := fields {
      fieldValue := getRowValue(row, field)
      model.Elem().FieldByName(field).Set(fieldValue)
    }
    castedModelRow := model.Elem().Interface()
      
    // append model to dest; how to do this?
    // dest = append(dest, castedModelRow)
  }
}

Things I've tried:

This simply panics: reflect: call of reflect.Append on ptr Value (as we pass &mySlice instead of mySlice)

dest = reflect.Append(reflect.ValueOf(dest), reflect.ValueOf(castedModelRow))

This works but doesn't set the value back to dest... in main func, len(mySlice) remains 0 after decode function is called.

func decode(dest interface{}, src string) {
  ...
  result := reflect.MakeSlice(reflect.SliceOf(modelType), rowCount, rowCount)
  for _, row : range rows {
    ...
    result = reflect.Append(result, reflect.ValueOf(castedModelRow))
  }
  dest = reflect.ValueOf(result)
}

答案1

得分: 2

以下是修复问题中第二个decode函数的方法。语句

dest = reflect.ValueOf(result)

修改的是局部变量dest,而不是调用者的值。使用以下语句来修改调用者的切片:

reflect.ValueOf(dest).Elem().Set(result)

问题中的代码在reflect.MakeSlice创建的元素之后追加了解码后的元素。结果切片中有len(rows)个零值,然后是len(rows)个解码后的值。通过将

result = reflect.Append(result, reflect.ValueOf(castedModelRow))

改为:

result.Index(i).Set(model)

来进行修复。

以下是问题中第二个decode函数的更新版本:

func decode(dest interface{}, src string) {
    var modelType reflect.Type = getStructType(dest)
    rows, fields := getRows(src)
    result := reflect.MakeSlice(reflect.SliceOf(modelType), len(rows), len(rows))
    for i, row := range rows {
        model := reflect.New(modelType).Elem()
        for _, field := range fields {
            fieldValue := getRowValue(row, field)
            model.FieldByName(field).Set(fieldValue)
        }
        result.Index(i).Set(model)
    }
    reflect.ValueOf(dest).Elem().Set(result)
}

在 Playground 上运行

英文:

Here's how to fix the second decode function shown in the question. The statement

dest = reflect.ValueOf(result)

modifies local variable dest, not the caller's value. Use the following statement to modify the caller's slice:

reflect.ValueOf(dest).Elem().Set(result)

The code in the question appends decoded elements after the elements created in reflect.MakeSlice. The resulting slice has len(rows) zero values followed by len(rows) decoded values. Fix by changing

result = reflect.Append(result, reflect.ValueOf(castedModelRow))

to:

result.Index(i).Set(model)

Here's the update version of the second decode function in the question:

func decode(dest interface{}, src string) {
    var modelType reflect.Type = getStructType(dest)
    rows, fields := getRows(src)
    result := reflect.MakeSlice(reflect.SliceOf(modelType), len(rows), len(rows))
    for i, row := range rows {
        model := reflect.New(modelType).Elem()
        for _, field := range fields {
            fieldValue := getRowValue(row, field)
            model.FieldByName(field).Set(fieldValue)
        }
        result.Index(i).Set(model)
    }
    reflect.ValueOf(dest).Elem().Set(result)
}

Run it on the Playground.

答案2

得分: 1

你的原始解决方案非常接近。在调用追加操作之前,你需要对指针进行解引用。如果你的目标(dest)已经有一些现有元素,并且你不想通过创建一个newSlice来丢失它们,这个解决方案将非常有帮助。

tempDest := reflect.ValueOf(dest).Elem()
tempDest = reflect.Append(tempDest, reflect.ValueOf(model.Interface()))

就像@I Love Reflection指出的那样,最后你需要将新的切片设置回指针。

reflect.ValueOf(dest).Elem().Set(tempDest)

总体解码:

var modelType reflect.Type = getStructType(dest)
rows, fields := getRows(src)
tempDest := reflect.ValueOf(dest).Elem()
for _, row := range rows {
    model := reflect.New(modelType).Elem()
    for _, field := range fields {
        fieldValue := getRowValue(row, field)
        model.FieldByName(field).Set(fieldValue)
    }
    tempDest = reflect.Append(tempDest, reflect.ValueOf(model.Interface()))
}
reflect.ValueOf(dest).Elem().Set(tempDest)
英文:

You were very close with your original solution. You had to de-reference the pointer before calling the append operation. This solution would be helpful if your dest already had some existing elements and you don't want to lose them by creating a newSlice.

tempDest := reflect.ValueOf(dest).Elem()
tempDest = reflect.Append(tempDest, reflect.ValueOf(model.Interface()))

Similar to how @I Love Reflection pointed out, you finally need to set the new slice back to the pointer.

reflect.ValueOf(dest).Elem().Set(tempDest)

Overall Decode:

var modelType reflect.Type = getStructType(dest)
rows, fields := getRows(src)
tempDest := reflect.ValueOf(dest).Elem()
for _, row := range rows {
	model := reflect.New(modelType).Elem()
	for _, field := range fields {
		fieldValue := getRowValue(row, field)
		model.FieldByName(field).Set(fieldValue)
	}
	tempDest = reflect.Append(tempDest, reflect.ValueOf(model.Interface()))
}
reflect.ValueOf(dest).Elem().Set(tempDest)

huangapple
  • 本文由 发表于 2021年11月7日 07:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69868784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定