github.com/openshift-online/ocm-sdk-go@v0.1.473/helpers/json_helpers.go (about) 1 /* 2 Copyright (c) 2020 Red Hat, 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 // IMPORTANT: This file has been generated automatically, refrain from modifying it manually as all 18 // your changes will be lost when the file is generated again. 19 20 package helpers // github.com/openshift-online/ocm-sdk-go/helpers 21 22 import ( 23 "fmt" 24 "io" 25 "net/url" 26 "strconv" 27 "time" 28 29 jsoniter "github.com/json-iterator/go" 30 ) 31 32 // NewIterator creates a new JSON iterator that will read to the given source, which 33 // can be a slice of bytes, a string, a reader or an existing iterator. 34 func NewIterator(source interface{}) (iterator *jsoniter.Iterator, err error) { 35 config := jsoniter.Config{} 36 api := config.Froze() 37 switch typed := source.(type) { 38 case []byte: 39 iterator = jsoniter.ParseBytes(api, typed) 40 case string: 41 iterator = jsoniter.ParseString(api, typed) 42 case io.Reader: 43 iterator = jsoniter.Parse(api, typed, 4096) 44 case *jsoniter.Iterator: 45 iterator = typed 46 default: 47 err = fmt.Errorf( 48 "expected slice of bytes, string, reader or iterator but got '%T'", 49 source, 50 ) 51 } 52 return 53 } 54 55 // NewStream creates a new JSON stream that will write to the given writer. 56 func NewStream(writer io.Writer) *jsoniter.Stream { 57 config := jsoniter.Config{ 58 IndentionStep: 2, 59 } 60 api := config.Froze() 61 return jsoniter.NewStream(api, writer, 0) 62 } 63 64 // NewBoolean allocates a new bool in the heap and returns a pointer to it. 65 func NewBoolean(value bool) *bool { 66 return &value 67 } 68 69 // NewInteger allocates a new integer in the heap and returns a pointer to it. 70 func NewInteger(value int) *int { 71 return &value 72 } 73 74 // NewFloat allocates a new floating point value in the heap and returns an pointer 75 // to it. 76 func NewFloat(value float64) *float64 { 77 return &value 78 } 79 80 // NewString allocates a new string in the heap and returns a pointer to it. 81 func NewString(value string) *string { 82 return &value 83 } 84 85 // NewDate allocates a new date in the heap and returns a pointer to it. 86 func NewDate(value time.Time) *time.Time { 87 return &value 88 } 89 90 // ParseInteger reads a string and parses it to integer, 91 // if an error occurred it returns a non-nil error. 92 func ParseInteger(query url.Values, parameterName string) (*int, error) { 93 values := query[parameterName] 94 count := len(values) 95 if count == 0 { 96 return nil, nil 97 } 98 if count > 1 { 99 err := fmt.Errorf( 100 "expected at most one value for parameter '%s' but got %d", 101 parameterName, count, 102 ) 103 return nil, err 104 } 105 value := values[0] 106 parsedInt64, err := strconv.ParseInt(value, 10, 64) 107 if err != nil { 108 return nil, fmt.Errorf( 109 "value '%s' isn't valid for the '%s' parameter because it isn't an integer: %v", 110 value, parameterName, err, 111 ) 112 } 113 parsedInt := int(parsedInt64) 114 return &parsedInt, nil 115 } 116 117 // ParseFloat reads a string and parses it to float, 118 // if an error occurred it returns a non-nil error. 119 func ParseFloat(query url.Values, parameterName string) (*float64, error) { 120 values := query[parameterName] 121 count := len(values) 122 if count == 0 { 123 return nil, nil 124 } 125 if count > 1 { 126 err := fmt.Errorf( 127 "expected at most one value for parameter '%s' but got %d", 128 parameterName, count, 129 ) 130 return nil, err 131 } 132 value := values[0] 133 parsedFloat, err := strconv.ParseFloat(value, 64) 134 if err != nil { 135 return nil, fmt.Errorf( 136 "value '%s' isn't valid for the '%s' parameter because it isn't a float: %v", 137 value, parameterName, err, 138 ) 139 } 140 return &parsedFloat, nil 141 } 142 143 // ParseString returns a pointer to the string and nil error. 144 func ParseString(query url.Values, parameterName string) (*string, error) { 145 values := query[parameterName] 146 count := len(values) 147 if count == 0 { 148 return nil, nil 149 } 150 if count > 1 { 151 err := fmt.Errorf( 152 "expected at most one value for parameter '%s' but got %d", 153 parameterName, count, 154 ) 155 return nil, err 156 } 157 return &values[0], nil 158 } 159 160 // ParseBoolean reads a string and parses it to boolean, 161 // if an error occurred it returns a non-nil error. 162 func ParseBoolean(query url.Values, parameterName string) (*bool, error) { 163 values := query[parameterName] 164 count := len(values) 165 if count == 0 { 166 return nil, nil 167 } 168 if count > 1 { 169 err := fmt.Errorf( 170 "expected at most one value for parameter '%s' but got %d", 171 parameterName, count, 172 ) 173 return nil, err 174 } 175 value := values[0] 176 parsedBool, err := strconv.ParseBool(value) 177 if err != nil { 178 return nil, fmt.Errorf( 179 "value '%s' isn't valid for the '%s' parameter because it isn't a boolean: %v", 180 value, parameterName, err, 181 ) 182 } 183 return &parsedBool, nil 184 } 185 186 // ParseDate reads a string and parses it to a time.Time, 187 // if an error occurred it returns a non-nil error. 188 func ParseDate(query url.Values, parameterName string) (*time.Time, error) { 189 values := query[parameterName] 190 count := len(values) 191 if count == 0 { 192 return nil, nil 193 } 194 if count > 1 { 195 err := fmt.Errorf( 196 "expected at most one value for parameter '%s' but got %d", 197 parameterName, count, 198 ) 199 return nil, err 200 } 201 value := values[0] 202 parsedTime, err := time.Parse(time.RFC3339, value) 203 if err != nil { 204 return nil, fmt.Errorf( 205 "value '%s' isn't valid for the '%s' parameter because it isn't a date: %v", 206 value, parameterName, err, 207 ) 208 } 209 return &parsedTime, nil 210 }