GetInt returns value stored in the environment, casted to int64. If value does not exist, or is not signed or unsigned integer that can be represented as int without overflow, reports failure and returns zero. Example: value := env.GetInt("key")
(key string)
| 184 | // |
| 185 | // value := env.GetInt("key") |
| 186 | func (e *Environment) GetInt(key string) int { |
| 187 | opChain := e.chain.enter("GetInt(%q)", key) |
| 188 | defer opChain.leave() |
| 189 | |
| 190 | e.mu.RLock() |
| 191 | defer e.mu.RUnlock() |
| 192 | |
| 193 | value, ok := envValue(opChain, e.data, key) |
| 194 | if !ok { |
| 195 | return 0 |
| 196 | } |
| 197 | |
| 198 | var casted int |
| 199 | |
| 200 | const ( |
| 201 | intSize = 32 << (^uint(0) >> 63) // 32 or 64 |
| 202 | maxInt = 1<<(intSize-1) - 1 |
| 203 | minInt = -1 << (intSize - 1) |
| 204 | ) |
| 205 | |
| 206 | switch num := value.(type) { |
| 207 | case int8: |
| 208 | casted = int(num) |
| 209 | ok = (int64(num) >= minInt) && (int64(num) <= maxInt) |
| 210 | case int16: |
| 211 | casted = int(num) |
| 212 | ok = (int64(num) >= minInt) && (int64(num) <= maxInt) |
| 213 | case int32: |
| 214 | casted = int(num) |
| 215 | ok = (int64(num) >= minInt) && (int64(num) <= maxInt) |
| 216 | case int64: |
| 217 | casted = int(num) |
| 218 | ok = (int64(num) >= minInt) && (int64(num) <= maxInt) |
| 219 | case int: |
| 220 | casted = num |
| 221 | ok = (int64(num) >= minInt) && (int64(num) <= maxInt) |
| 222 | |
| 223 | case uint8: |
| 224 | casted = int(num) |
| 225 | ok = (uint64(num) <= maxInt) |
| 226 | case uint16: |
| 227 | casted = int(num) |
| 228 | ok = (uint64(num) <= maxInt) |
| 229 | case uint32: |
| 230 | casted = int(num) |
| 231 | ok = (uint64(num) <= maxInt) |
| 232 | case uint64: |
| 233 | casted = int(num) |
| 234 | ok = (uint64(num) <= maxInt) |
| 235 | case uint: |
| 236 | casted = int(num) |
| 237 | ok = (uint64(num) <= maxInt) |
| 238 | |
| 239 | default: |
| 240 | opChain.fail(AssertionFailure{ |
| 241 | Type: AssertType, |
| 242 | Actual: &AssertionValue{value}, |
| 243 | Errors: []error{ |