// readAllStateCommon reads and unmarshals (given a function) all state into a // list of core.State objects. // XXX: move this to some common file func readAllStateCommon(d core.StateDriver, baseKey string, sType core.State, unmarshal func([]byte, interface{}) error) ([]core.State, error) { stateType := reflect.TypeOf(sType) sliceType := reflect.SliceOf(stateType) values := reflect.MakeSlice(sliceType, 0, 1) byteValues, err := d.ReadAll(baseKey) if err != nil { return nil, err } for _, byteValue := range byteValues { value := reflect.New(stateType) err = unmarshal(byteValue, value.Interface()) if err != nil { return nil, err } values = reflect.Append(values, value.Elem()) } stateValues := []core.State{} for i := 0; i < values.Len(); i++ { // sanity checks if !values.Index(i).Elem().FieldByName("CommonState").IsValid() { return nil, core.Errorf("The state structure %v is missing core.CommonState", stateType) } //the following works as every core.State is expected to embed core.CommonState struct values.Index(i).Elem().FieldByName("CommonState").FieldByName("StateDriver").Set(reflect.ValueOf(d)) stateValue := values.Index(i).Interface().(core.State) stateValues = append(stateValues, stateValue) } return stateValues, nil }