Skip to content

Commit

Permalink
refactor(asset): update ID methods and improve error handling in deco…
Browse files Browse the repository at this point in the history
…mpression
  • Loading branch information
kasugamirai committed Jan 26, 2025
1 parent ae883ae commit f06a63e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions asset/domain/entity/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (g *Group) Validate(ctx context.Context) validation.Result {
return validationCtx.Validate(ctx, fields)
}

// Getters
// ID Getters
func (g *Group) ID() id.GroupID { return g.id }
func (g *Group) Name() string { return g.name }
func (g *Group) Policy() string { return g.policy }
func (g *Group) Description() string { return g.description }
func (g *Group) CreatedAt() time.Time { return g.createdAt }
func (g *Group) UpdatedAt() time.Time { return g.updatedAt }

// Setters
// UpdateName Setters
func (g *Group) UpdateName(name string) error {
if name == "" {
return domain.ErrEmptyGroupName
Expand Down
8 changes: 4 additions & 4 deletions asset/domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package domain
import "errors"

var (
// Asset errors
// ErrEmptyWorkspaceID Asset errors
ErrEmptyWorkspaceID = errors.New("workspace id is required")
ErrEmptyURL = errors.New("url is required")
ErrEmptySize = errors.New("size must be greater than 0")
ErrAssetNotFound = errors.New("asset not found")
ErrInvalidAsset = errors.New("invalid asset")

// Group errors
// ErrEmptyGroupName Group errors
ErrEmptyGroupName = errors.New("group name is required")
ErrEmptyPolicy = errors.New("policy is required")
ErrGroupNotFound = errors.New("group not found")
ErrInvalidGroup = errors.New("invalid group")

// Storage errors
// ErrUploadFailed Storage errors
ErrUploadFailed = errors.New("failed to upload asset")
ErrDownloadFailed = errors.New("failed to download asset")
ErrDeleteFailed = errors.New("failed to delete asset")

// Extraction errors
// ErrExtractionFailed Extraction errors
ErrExtractionFailed = errors.New("failed to extract asset")
ErrNotExtractable = errors.New("asset is not extractable")
)
4 changes: 2 additions & 2 deletions asset/domain/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ var (
MustProjectID = idx.Must[idProject]
MustWorkspaceID = idx.Must[idWorkspace]

IDFrom = idx.From[idAsset]
From = idx.From[idAsset]
GroupIDFrom = idx.From[idGroup]
ProjectIDFrom = idx.From[idProject]
WorkspaceIDFrom = idx.From[idWorkspace]

IDFromRef = idx.FromRef[idAsset]
FromRef = idx.FromRef[idAsset]
GroupIDFromRef = idx.FromRef[idGroup]
ProjectIDFromRef = idx.FromRef[idProject]
WorkspaceIDFromRef = idx.FromRef[idWorkspace]
Expand Down
14 changes: 7 additions & 7 deletions asset/graphql/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion asset/infrastructure/decompress/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ func (d *ZipDecompressor) CompressWithContent(ctx context.Context, files map[str

buf := new(bytes.Buffer)
zipWriter := zip.NewWriter(buf)
defer zipWriter.Close()
defer func(zipWriter *zip.Writer) {
err := zipWriter.Close()
if err != nil {
log.Warn("failed to close zip writer", zap.Error(err))
}
}(zipWriter)

// Use a mutex to protect concurrent writes to the zip writer
var mu sync.Mutex
Expand Down
6 changes: 3 additions & 3 deletions asset/infrastructure/gcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *Client) List(ctx context.Context) ([]*entity.Asset, error) {
return nil, fmt.Errorf("%w: %v", ErrFailedToListAssets, err)
}

id, err := id.IDFrom(path.Base(attrs.Name))
id, err := id.From(path.Base(attrs.Name))
if err != nil {
continue // skip invalid IDs
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (c *Client) GetIDFromURL(urlStr string) (id.ID, error) {
urlPath = strings.TrimPrefix(urlPath, c.basePath)
urlPath = strings.TrimPrefix(urlPath, "/")

return id.IDFrom(urlPath)
return id.From(urlPath)
}

func (c *Client) getObject(id id.ID) *storage.ObjectHandle {
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *Client) FindByGroup(ctx context.Context, groupID id.GroupID) ([]*entity
return nil, fmt.Errorf("%w: %v", ErrFailedToListAssets, err)
}

assetID, err := id.IDFrom(path.Base(attrs.Name))
assetID, err := id.From(path.Base(attrs.Name))
if err != nil {
continue // skip invalid IDs
}
Expand Down
2 changes: 1 addition & 1 deletion asset/infrastructure/gcs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (c *testClient) Move(ctx context.Context, fromID, toID id.ID) error {
func (c *testClient) List(ctx context.Context) ([]*entity.Asset, error) {
var assets []*entity.Asset
for _, obj := range c.mockBucket.objects {
id, err := id.IDFrom(path.Base(obj.name))
id, err := id.From(path.Base(obj.name))
if err != nil {
continue
}
Expand Down

0 comments on commit f06a63e

Please sign in to comment.