本文整理匯總了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
}