Skip to content

Commit

Permalink
Add the Path structure (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev authored Oct 30, 2023
1 parent 0f7e92e commit b0c4c66
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
51 changes: 51 additions & 0 deletions core/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package core

import "strings"

type Path struct {
origin string
fsType VFSType
bucket string
base string
}

func newPath(path string, fsType VFSType) Path {
p := Path{
origin: path,
fsType: fsType,
}
p.parse()
return p
}

// Bucket returns bucket name
func (p Path) Bucket() string {
return p.bucket
}

// Base returns base path
func (p Path) Base() string {
return p.base
}

// String return the origin path
func (p Path) String() string {
return p.origin
}

func (p *Path) parse() {
p.base = p.origin

if p.fsType == MinIO {
// protocol => bucket:path
// example => mybucket:/workspace
if strings.Contains(p.origin, ":") && !strings.HasPrefix(p.origin, "/") {
list := strings.Split(p.origin, ":")
p.bucket = list[0]
p.base = list[1]
} else {
p.bucket = p.origin
p.base = ""
}
}
}
36 changes: 36 additions & 0 deletions core/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package core

import (
"testing"
)

func TestNewPath(t *testing.T) {
testCases := []struct {
path string
fsType VFSType
expectBucket string
expectBasePath string
}{
{"/workspace", Disk, "", "/workspace"},
{"/workspace", SFTP, "", "/workspace"},
{"myBucket:/workspace", MinIO, "myBucket", "/workspace"},
{"myBucket", MinIO, "myBucket", ""},
}

for _, tc := range testCases {
t.Run(tc.path, func(t *testing.T) {
p := newPath(tc.path, tc.fsType)
if p.Bucket() != tc.expectBucket {
t.Errorf("test new path error, expect bucket:%s, actual:%s", tc.expectBucket, p.Bucket())
return
}
if p.Base() != tc.expectBasePath {
t.Errorf("test new path error, expect base path:%s, actual:%s", tc.expectBasePath, p.Base())
return
}
if p.String() != tc.path {
t.Errorf("test new path error, expect path:%s, actual:%s", tc.path, p.String())
}
})
}
}

0 comments on commit b0c4c66

Please sign in to comment.