本文整理汇总了Golang中github.com/contiv/netplugin/core.StateDriver.ReadAll方法的典型用法代码示例。如果您正苦于以下问题:Golang StateDriver.ReadAll方法的具体用法?Golang StateDriver.ReadAll怎么用?Golang StateDriver.ReadAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/contiv/netplugin/core.StateDriver
的用法示例。
在下文中一共展示了StateDriver.ReadAll方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readAllStateCommon
// 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
}