parsequery.go (4257B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package util 19 20 import ( 21 "fmt" 22 "strconv" 23 24 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 25 ) 26 27 const ( 28 /* Common keys */ 29 30 LimitKey = "limit" 31 LocalKey = "local" 32 MaxIDKey = "max_id" 33 MinIDKey = "min_id" 34 35 /* Search keys */ 36 37 SearchExcludeUnreviewedKey = "exclude_unreviewed" 38 SearchFollowingKey = "following" 39 SearchLookupKey = "acct" 40 SearchOffsetKey = "offset" 41 SearchQueryKey = "q" 42 SearchResolveKey = "resolve" 43 SearchTypeKey = "type" 44 ) 45 46 // parseError returns gtserror.WithCode set to 400 Bad Request, to indicate 47 // to the caller that a key was set to a value that could not be parsed. 48 func parseError(key string, value, defaultValue any, err error) gtserror.WithCode { 49 err = fmt.Errorf("error parsing key %s with value %s as %T: %w", key, value, defaultValue, err) 50 return gtserror.NewErrorBadRequest(err, err.Error()) 51 } 52 53 func requiredError(key string) gtserror.WithCode { 54 err := fmt.Errorf("required key %s was not set or had empty value", key) 55 return gtserror.NewErrorBadRequest(err, err.Error()) 56 } 57 58 /* 59 Parse functions for *OPTIONAL* parameters with default values. 60 */ 61 62 func ParseLimit(value string, defaultValue int, max, min int) (int, gtserror.WithCode) { 63 key := LimitKey 64 65 if value == "" { 66 return defaultValue, nil 67 } 68 69 i, err := strconv.Atoi(value) 70 if err != nil { 71 return defaultValue, parseError(key, value, defaultValue, err) 72 } 73 74 if i > max { 75 i = max 76 } else if i < min { 77 i = min 78 } 79 80 return i, nil 81 } 82 83 func ParseLocal(value string, defaultValue bool) (bool, gtserror.WithCode) { 84 key := LimitKey 85 86 if value == "" { 87 return defaultValue, nil 88 } 89 90 i, err := strconv.ParseBool(value) 91 if err != nil { 92 return defaultValue, parseError(key, value, defaultValue, err) 93 } 94 95 return i, nil 96 } 97 98 func ParseSearchExcludeUnreviewed(value string, defaultValue bool) (bool, gtserror.WithCode) { 99 key := SearchExcludeUnreviewedKey 100 101 if value == "" { 102 return defaultValue, nil 103 } 104 105 i, err := strconv.ParseBool(value) 106 if err != nil { 107 return defaultValue, parseError(key, value, defaultValue, err) 108 } 109 110 return i, nil 111 } 112 113 func ParseSearchFollowing(value string, defaultValue bool) (bool, gtserror.WithCode) { 114 key := SearchFollowingKey 115 116 if value == "" { 117 return defaultValue, nil 118 } 119 120 i, err := strconv.ParseBool(value) 121 if err != nil { 122 return defaultValue, parseError(key, value, defaultValue, err) 123 } 124 125 return i, nil 126 } 127 128 func ParseSearchOffset(value string, defaultValue int, max, min int) (int, gtserror.WithCode) { 129 key := SearchOffsetKey 130 131 if value == "" { 132 return defaultValue, nil 133 } 134 135 i, err := strconv.Atoi(value) 136 if err != nil { 137 return defaultValue, parseError(key, value, defaultValue, err) 138 } 139 140 if i > max { 141 i = max 142 } else if i < min { 143 i = min 144 } 145 146 return i, nil 147 } 148 149 func ParseSearchResolve(value string, defaultValue bool) (bool, gtserror.WithCode) { 150 key := SearchResolveKey 151 152 if value == "" { 153 return defaultValue, nil 154 } 155 156 i, err := strconv.ParseBool(value) 157 if err != nil { 158 return defaultValue, parseError(key, value, defaultValue, err) 159 } 160 161 return i, nil 162 } 163 164 /* 165 Parse functions for *REQUIRED* parameters. 166 */ 167 168 func ParseSearchLookup(value string) (string, gtserror.WithCode) { 169 key := SearchLookupKey 170 171 if value == "" { 172 return "", requiredError(key) 173 } 174 175 return value, nil 176 } 177 178 func ParseSearchQuery(value string) (string, gtserror.WithCode) { 179 key := SearchQueryKey 180 181 if value == "" { 182 return "", requiredError(key) 183 } 184 185 return value, nil 186 }