MCPcopy Create free account
hub / github.com/github/gh-aw / parseOptionalInt

Function parseOptionalInt

pkg/workflow/cache.go:206–238  ·  view source on GitHub ↗

parseOptionalInt safely converts YAML numeric values (int, float64, uint64) to *int. It returns nil when the input cannot be represented as an integer for the current architecture, including: - NaN/Inf float64 values - fractional float64 values - float64 values outside the exact-integer range [-2^5

(value any)

Source from the content-addressed store, hash-verified

204// - uint64 values larger than math.MaxInt
205// - unsupported types
206func parseOptionalInt(value any) *int {
207 // YAML unmarshaling can yield int, float64, or uint64 depending on parser/input.
208 if intValue, ok := value.(int); ok {
209 return &intValue
210 }
211 if floatValue, ok := value.(float64); ok {
212 if math.IsNaN(floatValue) || math.IsInf(floatValue, 0) {
213 return nil
214 }
215 if floatValue != math.Trunc(floatValue) {
216 return nil
217 }
218 if floatValue < float64(math.MinInt) || floatValue > float64(math.MaxInt) {
219 return nil
220 }
221 // float64 can exactly represent integers only in [-2^53, 2^53].
222 const maxExactFloatInt = float64(1 << 53)
223 if floatValue < -maxExactFloatInt || floatValue > maxExactFloatInt {
224 return nil
225 }
226 intValue := int(floatValue)
227 return &intValue
228 }
229 if uintValue, ok := value.(uint64); ok {
230 // Guard int conversion on 32-bit/64-bit architectures.
231 if uintValue > uint64(math.MaxInt) {
232 return nil
233 }
234 intValue := int(uintValue)
235 return &intValue
236 }
237 return nil
238}
239
240func parseCacheMemoryRestoreOnly(cacheMap map[string]any, entry *CacheMemoryEntry) {
241 if restoreOnlyBool, ok := cacheMap["restore-only"].(bool); ok {

Callers 2

TestParseOptionalIntFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseOptionalIntFunction · 0.68