From 5ac5a6c1db91b0b23984fced29c18f7d2cb18dac Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Wed, 22 Apr 2020 16:11:41 +0200 Subject: [PATCH] Parse bundles from files for list calls. Side effect: get rid of code duplication for listing extensions. --- pkg/service/v0/service.go | 10 +--------- pkg/settings/settings.go | 1 - pkg/store/filesystem/paths.go | 10 ---------- pkg/store/filesystem/store.go | 24 ++++-------------------- 4 files changed, 5 insertions(+), 40 deletions(-) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index f8fa95438d..8b87e7e79b 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -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) - } -} diff --git a/pkg/settings/settings.go b/pkg/settings/settings.go index 2f43908ad3..b6e8fcc45d 100644 --- a/pkg/settings/settings.go +++ b/pkg/settings/settings.go @@ -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) } diff --git a/pkg/store/filesystem/paths.go b/pkg/store/filesystem/paths.go index 8225c4e758..d7a58076ce 100644 --- a/pkg/store/filesystem/paths.go +++ b/pkg/store/filesystem/paths.go @@ -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], - } -} diff --git a/pkg/store/filesystem/store.go b/pkg/store/filesystem/store.go index 7bfa1ae06a..e2c6f6ab76 100644 --- a/pkg/store/filesystem/store.go +++ b/pkg/store/filesystem/store.go @@ -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) } }