samedi 6 novembre 2021

Append to golang slice passed as empty interface

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)
}




Aucun commentaire:

Enregistrer un commentaire