Parse bundles from files for list calls.

Side effect: get rid of code duplication for listing extensions.
This commit is contained in:
Benedikt Kulmann
2020-04-22 16:11:41 +02:00
parent 9355ba3d35
commit 5ac5a6c1db
4 changed files with 5 additions and 40 deletions

View File

@@ -42,18 +42,10 @@ func (g Service) GetSettingsBundle(c context.Context, req *proto.GetSettingsBund
}
func (g Service) ListSettingsBundles(c context.Context, req *proto.ListSettingsBundlesRequest, res *proto.ListSettingsBundlesResponse) error {
r, err := listSettingsBundles(g, req.Extension)
r, err := g.manager.ListByExtension(req.Extension)
if err != nil {
return err
}
res.SettingsBundles = r
return nil
}
func listSettingsBundles(g Service, extension string) ([]*proto.SettingsBundle, error) {
if len(extension) == 0 {
return g.manager.ListAll()
} else {
return g.manager.ListByExtension(extension)
}
}

View File

@@ -17,6 +17,5 @@ type RegisterFunc func(*config.Config) Manager
type Manager interface {
Read(extension string, key string) (*proto.SettingsBundle, error)
Write(bundle *proto.SettingsBundle) (*proto.SettingsBundle, error)
ListAll() ([]*proto.SettingsBundle, error)
ListByExtension(extension string) ([]*proto.SettingsBundle, error)
}

View File

@@ -2,7 +2,6 @@ package store
import (
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"strings"
)
// Builds a unique file name from the given bundle
@@ -14,12 +13,3 @@ func buildFileNameFromBundle(bundle *proto.SettingsBundle) string {
func buildFileNameFromBundleArgs(extension string, key string) string {
return extension + "__" + key + ".json"
}
// Extracts extension and key from the given fileName and builds a (minimalistic) bundle from it
func parseBundleFromFileName(fileName string) *proto.SettingsBundle {
parts := strings.Split(strings.Replace(fileName, ".json", "", 1), "__")
return &proto.SettingsBundle{
Key: parts[1],
Extension: parts[0],
}
}

View File

@@ -43,23 +43,6 @@ func New(cfg *config.Config) settings.Manager {
return &s
}
// ListAll returns all the bundles in the mountPath folder
func (s Store) ListAll() ([]*proto.SettingsBundle, error) {
records := []*proto.SettingsBundle{}
bundles, err := ioutil.ReadDir(s.mountPath)
if err != nil {
s.Logger.Err(err).Msgf("error reading %v", s.mountPath)
return nil, err
}
s.Logger.Info().Msg("listing bundles")
for _, v := range bundles {
records = append(records, parseBundleFromFileName(v.Name()))
}
return records, nil
}
// ListByExtension returns all bundles in the mountPath folder belonging to the given extension
func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error) {
records := []*proto.SettingsBundle{}
@@ -71,9 +54,10 @@ func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error
s.Logger.Info().Msgf("listing bundles by extension %v", extension)
for _, v := range bundles {
record := parseBundleFromFileName(v.Name())
if record.Extension == extension {
records = append(records, record)
record := proto.SettingsBundle{}
err = s.parseRecordFromFile(&record, path.Join(s.mountPath, v.Name()))
if err == nil && (len(extension) == 0 || extension == record.Extension) {
records = append(records, &record)
}
}