Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Empty Files Before Unmarshalling to Prevent Unmarshal Errors #163

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pkg/host/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type CreateStrategy[E api.Object] interface {
PrepareForCreate(obj E)
}

func (s *Store[E]) Create(ctx context.Context, obj E) (E, error) {
func (s *Store[E]) Create(_ context.Context, obj E) (E, error) {
s.idMu.Lock(obj.GetID())
defer s.idMu.Unlock(obj.GetID())

Expand Down Expand Up @@ -100,7 +100,10 @@ func (s *Store[E]) Create(ctx context.Context, obj E) (E, error) {
return obj, nil
}

func (s *Store[E]) Get(ctx context.Context, id string) (E, error) {
func (s *Store[E]) Get(_ context.Context, id string) (E, error) {
s.idMu.Lock(id)
defer s.idMu.Unlock(id)

object, err := s.get(id)
if err != nil {
return utils.Zero[E](), fmt.Errorf("failed to read object: %w", err)
Expand All @@ -109,7 +112,7 @@ func (s *Store[E]) Get(ctx context.Context, id string) (E, error) {
return object, nil
}

func (s *Store[E]) Update(ctx context.Context, obj E) (E, error) {
func (s *Store[E]) Update(_ context.Context, obj E) (E, error) {
s.idMu.Lock(obj.GetID())
defer s.idMu.Unlock(obj.GetID())

Expand Down Expand Up @@ -143,7 +146,7 @@ func (s *Store[E]) Update(ctx context.Context, obj E) (E, error) {
return obj, nil
}

func (s *Store[E]) Delete(ctx context.Context, id string) error {
func (s *Store[E]) Delete(_ context.Context, id string) error {
s.idMu.Lock(id)
defer s.idMu.Unlock(id)

Expand Down Expand Up @@ -183,7 +186,7 @@ func (s *Store[E]) List(ctx context.Context) ([]E, error) {
continue
}

object, err := s.get(entry.Name())
object, err := s.Get(ctx, entry.Name())
if err != nil {
return nil, fmt.Errorf("failed to read object: %w", err)
}
Expand All @@ -194,7 +197,7 @@ func (s *Store[E]) List(ctx context.Context) ([]E, error) {
return objs, nil
}

func (s *Store[E]) Watch(ctx context.Context) (store.Watch[E], error) {
func (s *Store[E]) Watch(_ context.Context) (store.Watch[E], error) {
//TODO make configurable
const bufferSize = 10
s.watchesMu.Lock()
Expand Down Expand Up @@ -222,7 +225,7 @@ func (s *Store[E]) get(id string) (E, error) {

obj := s.newFunc()
if err := json.Unmarshal(file, &obj); err != nil {
return utils.Zero[E](), fmt.Errorf("failed to unmarshal object: %w", err)
return utils.Zero[E](), fmt.Errorf("failed to unmarshal object from file %s: %w", id, err)
}

return obj, err
Expand Down
Loading