storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/value.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package condition 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "reflect" 23 "strconv" 24 "strings" 25 ) 26 27 // Splits an incoming path into bucket and object components. 28 func path2BucketAndObject(path string) (bucket, object string) { 29 // Skip the first element if it is '/', split the rest. 30 path = strings.TrimPrefix(path, "/") 31 pathComponents := strings.SplitN(path, "/", 2) 32 33 // Save the bucket and object extracted from path. 34 switch len(pathComponents) { 35 case 1: 36 bucket = pathComponents[0] 37 case 2: 38 bucket = pathComponents[0] 39 object = pathComponents[1] 40 } 41 return bucket, object 42 } 43 44 // Value - is enum type of string, int or bool. 45 type Value struct { 46 t reflect.Kind 47 s string 48 i int 49 b bool 50 } 51 52 // GetBool - gets stored bool value. 53 func (v Value) GetBool() (bool, error) { 54 var err error 55 56 if v.t != reflect.Bool { 57 err = fmt.Errorf("not a bool Value") 58 } 59 60 return v.b, err 61 } 62 63 // GetInt - gets stored int value. 64 func (v Value) GetInt() (int, error) { 65 var err error 66 67 if v.t != reflect.Int { 68 err = fmt.Errorf("not a int Value") 69 } 70 71 return v.i, err 72 } 73 74 // GetString - gets stored string value. 75 func (v Value) GetString() (string, error) { 76 var err error 77 78 if v.t != reflect.String { 79 err = fmt.Errorf("not a string Value") 80 } 81 82 return v.s, err 83 } 84 85 // GetType - gets enum type. 86 func (v Value) GetType() reflect.Kind { 87 return v.t 88 } 89 90 // MarshalJSON - encodes Value to JSON data. 91 func (v Value) MarshalJSON() ([]byte, error) { 92 switch v.t { 93 case reflect.String: 94 return json.Marshal(v.s) 95 case reflect.Int: 96 return json.Marshal(v.i) 97 case reflect.Bool: 98 return json.Marshal(v.b) 99 } 100 101 return nil, fmt.Errorf("unknown value kind %v", v.t) 102 } 103 104 // StoreBool - stores bool value. 105 func (v *Value) StoreBool(b bool) { 106 *v = Value{t: reflect.Bool, b: b} 107 } 108 109 // StoreInt - stores int value. 110 func (v *Value) StoreInt(i int) { 111 *v = Value{t: reflect.Int, i: i} 112 } 113 114 // StoreString - stores string value. 115 func (v *Value) StoreString(s string) { 116 *v = Value{t: reflect.String, s: s} 117 } 118 119 // String - returns string representation of value. 120 func (v Value) String() string { 121 switch v.t { 122 case reflect.String: 123 return v.s 124 case reflect.Int: 125 return strconv.Itoa(v.i) 126 case reflect.Bool: 127 return strconv.FormatBool(v.b) 128 } 129 130 return "" 131 } 132 133 // UnmarshalJSON - decodes JSON data. 134 func (v *Value) UnmarshalJSON(data []byte) error { 135 var b bool 136 if err := json.Unmarshal(data, &b); err == nil { 137 v.StoreBool(b) 138 return nil 139 } 140 141 var i int 142 if err := json.Unmarshal(data, &i); err == nil { 143 v.StoreInt(i) 144 return nil 145 } 146 147 var s string 148 if err := json.Unmarshal(data, &s); err == nil { 149 v.StoreString(s) 150 return nil 151 } 152 153 return fmt.Errorf("unknown json data '%v'", data) 154 } 155 156 // NewBoolValue - returns new bool value. 157 func NewBoolValue(b bool) Value { 158 value := &Value{} 159 value.StoreBool(b) 160 return *value 161 } 162 163 // NewIntValue - returns new int value. 164 func NewIntValue(i int) Value { 165 value := &Value{} 166 value.StoreInt(i) 167 return *value 168 } 169 170 // NewStringValue - returns new string value. 171 func NewStringValue(s string) Value { 172 value := &Value{} 173 value.StoreString(s) 174 return *value 175 }