the big refactor 2: return of the ID struct
This commit is contained in:
@@ -2,6 +2,7 @@ package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
@@ -13,15 +14,14 @@ var (
|
||||
// ID an id of a file consists of at least a Bucket and a Key. Version is optional.
|
||||
// Can also be used as an ID for a directory. When the key ends with "/" it is treated as dir.
|
||||
type ID struct {
|
||||
Bucket string // Name of the bucket
|
||||
Key string // Key of the object
|
||||
Version string // Version of the object. For now we ignore it
|
||||
Bucket string `json:"bucket"` // Name of the bucket
|
||||
Key string `json:"key"` // Key of the object
|
||||
Version string `json:"version"` // Version of the object. For now we ignore it
|
||||
}
|
||||
|
||||
// String Return String representation of an ID
|
||||
// Looks like this: "bucketName@version:/id/of/obj" or "bucketName:/id/of/obj"
|
||||
func (i *ID) String() string {
|
||||
i.Normalize()
|
||||
func (i ID) String() string {
|
||||
if i.Version == "" {
|
||||
return fmt.Sprintf("%s:%s", i.Bucket, i.Key)
|
||||
} else {
|
||||
@@ -31,7 +31,9 @@ func (i *ID) String() string {
|
||||
|
||||
// Normalize normalzes the key to have a "/" prefix
|
||||
func (i *ID) Normalize() {
|
||||
if !strings.HasPrefix(i.Key, "/") {
|
||||
if i.Key == "." {
|
||||
i.Key = "/"
|
||||
} else if !strings.HasPrefix(i.Key, "/") {
|
||||
i.Key = "/" + i.Key
|
||||
}
|
||||
}
|
||||
@@ -41,6 +43,32 @@ func (i *ID) Valid() bool {
|
||||
return i.Bucket != "" && i.Key != ""
|
||||
}
|
||||
|
||||
func (i *ID) IsDirectory() bool {
|
||||
return strings.HasSuffix(i.Key, "/")
|
||||
}
|
||||
|
||||
// Raw for the Key interface for the dataloaders so ID can be used as a dataloader key
|
||||
func (i ID) Raw() interface{} {
|
||||
return i
|
||||
}
|
||||
|
||||
// Parent returns the parent dir ID.
|
||||
func (i ID) Parent() *ID {
|
||||
if i.Key == "/" {
|
||||
// Already at root. We dont have a parent
|
||||
return nil
|
||||
}
|
||||
|
||||
parent := &ID{
|
||||
Bucket: i.Bucket,
|
||||
Key: filepath.Dir(i.Key),
|
||||
}
|
||||
|
||||
parent.Normalize()
|
||||
|
||||
return parent
|
||||
}
|
||||
|
||||
// ParseID parses a string to an ID. Null if invalid
|
||||
func ParseID(id string) *ID {
|
||||
match := idRegex.FindStringSubmatch(id)
|
||||
|
||||
@@ -21,7 +21,7 @@ type AppConfig struct {
|
||||
|
||||
// File represents a file with its metadata
|
||||
type File struct {
|
||||
ID string `json:"id"`
|
||||
ID ID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
ContentType string `json:"contentType"`
|
||||
@@ -31,7 +31,7 @@ type File struct {
|
||||
|
||||
// Directory represents a directory with its metadata
|
||||
type Directory struct {
|
||||
ID string `json:"id"`
|
||||
ID ID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Files []File `json:"files"`
|
||||
Directorys []Directory `json:"directorys"`
|
||||
|
||||
Reference in New Issue
Block a user