use jsonpb Marshaler/unmarshal

This commit is contained in:
A.Unger
2020-04-16 12:49:51 +02:00
parent e8bc35ae03
commit 99c372a8ef
2 changed files with 33 additions and 19 deletions
+5 -4
View File
@@ -2,23 +2,24 @@ package svc
import (
"context"
"github.com/owncloud/ocis-settings/pkg/settings"
store "github.com/owncloud/ocis-settings/pkg/store/filesystem"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
)
type Service struct {
config *config.Config
config *config.Config
manager settings.Manager
}
// NewService returns a service implementation for Service.
func NewService(cfg *config.Config) Service {
return Service{
config:cfg,
manager:store.New(cfg),
config: cfg,
manager: store.New(cfg),
}
}
+28 -15
View File
@@ -2,16 +2,17 @@
package store
import (
"encoding/json"
"fmt"
olog "github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"github.com/owncloud/ocis-settings/pkg/settings"
"io/ioutil"
"os"
"path"
"strings"
"github.com/golang/protobuf/jsonpb"
olog "github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"github.com/owncloud/ocis-settings/pkg/settings"
)
var (
@@ -84,14 +85,14 @@ func (s Store) ListByExtension(extension string) ([]*proto.SettingsBundle, error
// Read tries to find a bundle by the given extension and key within the mountPath
func (s Store) Read(extension string, key string) (*proto.SettingsBundle, error) {
fileName := buildFileNameFromData(extension, key)
contents, err := ioutil.ReadFile(path.Join(s.mountPath, fileName))
contents, err := os.Open(path.Join(s.mountPath, fileName))
if err != nil {
s.Logger.Err(err).Msgf("error reading contents for extension %v and key %v: file not found", extension, key)
return nil, err
}
record := proto.SettingsBundle{}
if err = json.Unmarshal(contents, &record); err != nil {
if err = jsonpb.Unmarshal(contents, &record); err != nil {
s.Logger.Err(err).Msg("error unmarshalling record")
return nil, err
}
@@ -106,18 +107,30 @@ func (s Store) Write(rec *proto.SettingsBundle) (*proto.SettingsBundle, error) {
return nil, fmt.Errorf(emptyKeyError)
}
contents, err := json.Marshal(rec)
if err != nil {
s.Logger.Err(err).Msg("record could not be marshalled")
return nil, err
}
marshaler := jsonpb.Marshaler{}
recordPath := path.Join(s.mountPath, buildFileNameFromBundle(rec))
if err := ioutil.WriteFile(recordPath, contents, 0644); err != nil {
if err := ioutil.WriteFile(recordPath, []byte{}, 0644); err != nil {
return nil, err
}
s.Logger.Info().Msgf("%v bytes written to %v", len(contents), recordPath)
fd, err := os.OpenFile(recordPath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
s.Logger.Err(err).
Str(
"finding file",
fmt.Sprintf("file `%v` not found on store: `%v`", recordPath, s.mountPath),
)
}
if err = marshaler.Marshal(fd, rec); err != nil {
s.Logger.Err(err).
Str(
"marshaling record",
fmt.Sprintf("error marshaling record: %+v", rec),
)
}
s.Logger.Info().Msgf("request contents written to file: %v", recordPath)
return rec, nil
}