Patrick
Patrick

Reputation: 139

golang reflect into []interface{}

i wanna create a mini framework which takes a simple struct and creates a full crud out of it. i already started and the "findOne, update,create,delete" is working. not i have a problem to create a findAll method. to be more clear, i dont know how to use reflect to address my ptr to an array of struct.

Here a small example for the findOne function.

type company struct {
Id          int
Name        string
}

comp.InitModel(newDbConnection(), &comp)

In InitModel i can fill the pointer to the company with the following:

//m.caller = pointer to the ptr to comp (struct)
callerV := reflect.ValueOf(m.caller)
CallerField := callerV.Elem()
var values []interface{}
for _, e := range m.columns {
    values = append(values, CallerField.FieldByName(e.name).Addr().Interface())
}
err := r.Scan(values...)
if err != nil {
    return err
}

Now i wanna create a findAll method which would be called like this

var companies []company
comp.InitModel(newDbConnection(), &comp)
comp.FindAll(&companies) //in this is the db query and scan
fmt.Println(companies) //here should be the result

But i have a problem to get the reflect with a []interface working.

func (m *Model) FindAll(test []interface{}, c *Condition) error {

//get the sql statement from the struct
stmt := PrepairStmt(m, c)

rows, err := m.db.Query(stmt.selectParse(), c.arguments...)
if err != nil {
    return err
}
defer rows.Close()

callerV := reflect.ValueOf(m.caller)
CallerField := callerV.Elem()
for rows.Next() {

    var values []interface{}
    for _, e := range m.columns {
        values = append(values, CallerField.FieldByName(e.name).Addr().Interface())
    }

    err = rows.Scan(values...)
    if err != nil {
        return err
    }

    valuePtr := reflect.New(reflect.TypeOf(test).Elem())
    test = reflect.Append(test,reflect.ValueOf(values))
}

return nil
}

Thats were my latest tries. maybe someone can help me with this. i would be really thankful

Upvotes: 0

Views: 756

Answers (1)

Thundercat
Thundercat

Reputation: 120941

Use interface{} instead of []interface{} as the argument type:

func (m *Model) FindAll(result interface{}, c *Condition) error {
  stmt := PrepairStmt(m, c)
  rows, err := m.db.Query(stmt.selectParse(), c.arguments...)
  if err != nil {
    return err
  }
  defer rows.Close()

  // resultv is the result slice
  resultv := reflect.ValueOf(result).Elem()

  // rowt is the struct type
  rowt := resultv.Type().Elem()

  // allocate a value for the row
  rowv := reflect.New(rowt).Elem()

  // collect values for scan
  var values []interface{}
  for _, e := range m.columns {
      values = append(values, rowv.FieldByName(e.name).Addr().Interface())
  }

  for rows.Next() {

    err = rows.Scan(values...)
    if err != nil {
      return err
    }

    // Append struct to result slice. Because the struct
    // is copied in append, we can reuse the struct in 
    // this loop. 
    resultv.Set(reflect.Append(resultv, rowv))
  }
  return nil
}

Use it like this:

var companies []company
comp.InitModel(newDbConnection(), &comp)
comp.FindAll(&companies) //in this is the db query and scan

Upvotes: 3

Related Questions