-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
86 lines (77 loc) · 2.37 KB
/
option.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
package gcs
import (
"bytes"
"context"
"encoding/base64"
"io"
"strconv"
"strings"
"time"
)
type Option func(*model)
// Deprecated
func WithBearerToken(value string) Option {
return func(this *model) { this.credentials = Credentials{BearerToken: value} }
}
func WithCredentials(credentials Credentials) Option {
return func(this *model) { this.credentials = credentials }
}
func WithEndpoint(scheme, host string) Option {
return func(this *model) { this.scheme = scheme; this.host = host }
}
func WithBucket(value string) Option {
return func(this *model) { this.bucket = strings.TrimSpace(value) }
}
func WithResource(value string) Option {
return func(this *model) { this.resource = strings.TrimPrefix(strings.TrimSpace(value), "/") }
}
// Deprecated
func WithExpiration(value time.Time) Option {
return WithSignedExpiration(value)
}
func WithSignedExpiration(value time.Time) Option {
return func(this *model) { this.epoch = strconv.FormatInt(value.Unix(), 10) }
}
func WithContext(value context.Context) Option {
return func(this *model) { this.context = value }
}
func GetWithETag(value string) Option {
return func(this *model) { this.etag = strings.TrimSpace(value) }
}
func PutWithGeneration(value string) Option {
return func(this *model) { this.generation = strings.TrimSpace(value) }
}
func PutWithContentString(value string) Option {
return func(this *model) { PutWithContentBytes([]byte(value))(this) }
}
func PutWithContentBytes(value []byte) Option {
return func(this *model) {
this.content = bytes.NewReader(value)
this.contentLength = int64(len(value))
}
}
func PutWithContent(value io.Reader) Option {
return func(this *model) { this.content = value }
}
func PutWithContentType(value string) Option {
return func(this *model) { this.contentType = strings.TrimSpace(value) }
}
func PutWithContentLength(value int64) Option {
return func(this *model) { this.contentLength = value }
}
func PutWithContentMD5(value []byte) Option {
return func(this *model) { this.contentMD5 = base64.StdEncoding.EncodeToString(value) }
}
func PutWithContentEncoding(value string) Option {
return func(this *model) { this.contentEncoding = value }
}
func WithCompositeOption(options ...Option) Option {
return func(this *model) { this.applyOptions(options) }
}
func WithConditionalOption(option Option, condition bool) Option {
if condition {
return option
} else {
return nil
}
}