-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetadata.go
92 lines (86 loc) · 3 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package storage
import (
"fmt"
"github.com/kubemq-io/kubemq-targets/types"
)
var methodsMap = map[string]string{
"upload": "upload",
"create_bucket": "create_bucket",
"download": "download",
"delete": "delete",
"rename": "rename",
"copy": "copy",
"move": "move",
"list": "list",
}
type metadata struct {
method string
object string
renameObject string
bucket string
dstBucket string
path string
projectID string
storageClass string
location string
}
func parseMetadata(meta types.Metadata) (metadata, error) {
m := metadata{}
var err error
m.method, err = meta.ParseStringMap("method", methodsMap)
if err != nil {
return metadata{}, meta.GetValidMethodTypes(methodsMap)
}
if m.method == "create_bucket" {
m.bucket, err = meta.MustParseString("bucket")
if err != nil {
return metadata{}, fmt.Errorf("bucket is required for method:%s , error on parsing bucket, %w", m.method, err)
}
m.projectID, err = meta.MustParseString("project_id")
if err != nil {
return metadata{}, fmt.Errorf("project_id is required for method:%s, error on project_id, %w", m.method, err)
}
m.storageClass, err = meta.MustParseString("storage_class")
if err != nil {
return metadata{}, fmt.Errorf("storage_class is required for method:%s, error on storage_class, %w", m.method, err)
}
m.location, err = meta.MustParseString("location")
if err != nil {
return metadata{}, fmt.Errorf("location is required for method:%s, error on location, %w", m.method, err)
}
}
if m.method == "upload" || m.method == "download" || m.method == "delete" || m.method == "rename" || m.method == "copy" || m.method == "move" {
m.object, err = meta.MustParseString("object")
if err != nil {
return metadata{}, fmt.Errorf("object is required for method:%s, error on parsing upload, %w", m.method, err)
}
m.bucket, err = meta.MustParseString("bucket")
if err != nil {
return metadata{}, fmt.Errorf("bucket is required for method:%s, error on parsing bucket, %w", m.method, err)
}
if m.method == "upload" {
m.path, err = meta.MustParseString("path")
if err != nil {
return metadata{}, fmt.Errorf("path is required for method:%s,error on path, %w", m.method, err)
}
} else if m.method == "rename" || m.method == "copy" || m.method == "move" {
m.renameObject, err = meta.MustParseString("rename_object")
if err != nil {
return metadata{}, fmt.Errorf("rename_object is required for method:%s,error on rename_object, %w", m.method, err)
}
if m.method == "copy" || m.method == "move" {
m.dstBucket, err = meta.MustParseString("dst_bucket")
if err != nil {
return metadata{}, fmt.Errorf("dst_bucket is required for method:%s,error on dst_bucket, %w", m.method, err)
}
}
}
}
if m.method == "list" {
m.bucket, err = meta.MustParseString("bucket")
if err != nil {
return metadata{}, fmt.Errorf("bucket is required for method:%s,error on parsing bucket, %w", m.method, err)
}
}
return m, nil
}