func getData(dataType enum.Datatype, dataOwner enum.DataOwner) (interface{}, error) { var data interface{} switch dataType { case enum.STATS: data = GruStats{} dataStats, err := storage.GetData(dataOwner.ToString(), dataType) if err != nil { return nil, err } else { data, err = ByteToStats(dataStats) if err != nil { return nil, err } } case enum.ANALYTICS: data = GruAnalytics{} dataAnalytics, err := storage.GetData(dataOwner.ToString(), dataType) if err != nil { return nil, err } else { data, err = ByteToAnalytics(dataAnalytics) if err != nil { return nil, err } } case enum.POLICIES: data = Policy{} dataPolicy, err := storage.GetData(dataOwner.ToString(), dataType) if err != nil { return nil, err } else { data, err = ByteToPolicy(dataPolicy) if err != nil { return nil, err } } case enum.SHARED: data = Shared{} dataInfo, err := storage.GetData(dataOwner.ToString(), dataType) if err != nil { return nil, err } else { data, err = ByteToShared(dataInfo) if err != nil { return nil, err } } } return data, nil }
func saveData(data interface{}, dataType enum.Datatype, dataOwner enum.DataOwner) error { var encoded []byte var err error switch dataType { case enum.STATS: stats := data.(GruStats) encoded, err = json.Marshal(stats) if err != nil { return err } case enum.ANALYTICS: analytics := data.(GruAnalytics) encoded, err = json.Marshal(analytics) if err != nil { return err } case enum.POLICIES: policy := data.(Policy) encoded, err = json.Marshal(policy) if err != nil { return err } case enum.SHARED: info := data.(Shared) encoded, err = json.Marshal(info) if err != nil { return err } default: return errors.New("Cannot save data: unknown data type") } storage.StoreData(dataOwner.ToString(), encoded, dataType) return nil }