storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/name.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 ) 23 24 type name string 25 26 const ( 27 stringEquals name = "StringEquals" 28 stringNotEquals = "StringNotEquals" 29 stringEqualsIgnoreCase = "StringEqualsIgnoreCase" 30 stringNotEqualsIgnoreCase = "StringNotEqualsIgnoreCase" 31 stringLike = "StringLike" 32 stringNotLike = "StringNotLike" 33 binaryEquals = "BinaryEquals" 34 ipAddress = "IpAddress" 35 notIPAddress = "NotIpAddress" 36 null = "Null" 37 boolean = "Bool" 38 numericEquals = "NumericEquals" 39 numericNotEquals = "NumericNotEquals" 40 numericLessThan = "NumericLessThan" 41 numericLessThanEquals = "NumericLessThanEquals" 42 numericGreaterThan = "NumericGreaterThan" 43 numericGreaterThanEquals = "NumericGreaterThanEquals" 44 dateEquals = "DateEquals" 45 dateNotEquals = "DateNotEquals" 46 dateLessThan = "DateLessThan" 47 dateLessThanEquals = "DateLessThanEquals" 48 dateGreaterThan = "DateGreaterThan" 49 dateGreaterThanEquals = "DateGreaterThanEquals" 50 ) 51 52 var supportedConditions = []name{ 53 stringEquals, 54 stringNotEquals, 55 stringEqualsIgnoreCase, 56 stringNotEqualsIgnoreCase, 57 binaryEquals, 58 stringLike, 59 stringNotLike, 60 ipAddress, 61 notIPAddress, 62 null, 63 boolean, 64 numericEquals, 65 numericNotEquals, 66 numericLessThan, 67 numericLessThanEquals, 68 numericGreaterThan, 69 numericGreaterThanEquals, 70 dateEquals, 71 dateNotEquals, 72 dateLessThan, 73 dateLessThanEquals, 74 dateGreaterThan, 75 dateGreaterThanEquals, 76 // Add new conditions here. 77 } 78 79 // IsValid - checks if name is valid or not. 80 func (n name) IsValid() bool { 81 for _, supn := range supportedConditions { 82 if n == supn { 83 return true 84 } 85 } 86 87 return false 88 } 89 90 // MarshalJSON - encodes name to JSON data. 91 func (n name) MarshalJSON() ([]byte, error) { 92 if !n.IsValid() { 93 return nil, fmt.Errorf("invalid name %v", n) 94 } 95 96 return json.Marshal(string(n)) 97 } 98 99 // UnmarshalJSON - decodes JSON data to condition name. 100 func (n *name) UnmarshalJSON(data []byte) error { 101 var s string 102 if err := json.Unmarshal(data, &s); err != nil { 103 return err 104 } 105 106 parsedName, err := parseName(s) 107 if err != nil { 108 return err 109 } 110 111 *n = parsedName 112 return nil 113 } 114 115 func parseName(s string) (name, error) { 116 n := name(s) 117 118 if n.IsValid() { 119 return n, nil 120 } 121 122 return n, fmt.Errorf("invalid condition name '%v'", s) 123 }