-
Notifications
You must be signed in to change notification settings - Fork 25
/
file_object.go
97 lines (83 loc) · 2.26 KB
/
file_object.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
93
94
95
96
97
package tengo
import (
"github.com/aldor007/mort/pkg/object"
tengoLib "github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/token"
)
// FileObject struct wraping objectFileObject
type FileObject struct {
tengoLib.ObjectImpl
Value *object.FileObject
}
// String returns object uri
func (o *FileObject) String() string {
return o.Value.Uri.String()
}
// BinaryOp not implemented
func (o *FileObject) BinaryOp(op token.Token, rhs tengoLib.Object) (tengoLib.Object, error) {
return nil, tengoLib.ErrInvalidOperator
}
// IsFalsy returns false if uri is empty
func (o *FileObject) IsFalsy() bool {
return o.Value.Uri.String() == ""
}
// Equals returns true if objects url are the same
func (o *FileObject) Equals(x tengoLib.Object) bool {
return o.String() == x.String()
}
// Copy create copy using object.FileObject.Copy
func (o *FileObject) Copy() tengoLib.Object {
return &FileObject{
Value: o.Value.Copy(),
}
}
func (o *FileObject) TypeName() string {
return "FileObject-object"
}
// IndexGet returns the value for the given key.
// * `uri` return object Url
// * `bucket` return bucket name string
// * `key` return object storage path
// * `transforms` return Transforms object on which you can execute image manipulations
// Usage in tengo
//
// obj.key // access to object key
func (o *FileObject) IndexGet(index tengoLib.Object) (val tengoLib.Object, err error) {
strIdx, ok := tengoLib.ToString(index)
if !ok {
err = tengoLib.ErrInvalidIndexType
return
}
val = tengoLib.UndefinedValue
switch strIdx {
case "uri":
val = &URL{Value: o.Value.Uri}
case "bucket":
val = &tengoLib.String{Value: o.Value.Bucket}
case "key":
val = &tengoLib.String{Value: o.Value.Key}
case "transforms":
val = &Transforms{Value: &o.Value.Transforms}
}
return val, nil
}
// IndexSet allow to change value on FileObject
// * `allowChangeKey`
// * `checkParent`
// * `debug`
func (o *FileObject) IndexSet(index, value tengoLib.Object) (err error) {
strIdx, ok := tengoLib.ToString(index)
if !ok {
err = tengoLib.ErrInvalidIndexType
return
}
switch strIdx {
case "allowChangeKey":
o.Value.AllowChangeKey, _ = tengoLib.ToBool(value)
case "checkParent":
o.Value.CheckParent, _ = tengoLib.ToBool(value)
case "debug":
o.Value.Debug, _ = tengoLib.ToBool(value)
}
return nil
}