github.com/aquasecurity/trivy-iac@v0.8.1-0.20240127024015-3d8e412cf0ab/pkg/scanners/azure/arm/parser/armjson/decode_number.go (about)

     1  package armjson
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  func (n *node) decodeNumber(v reflect.Value) error {
     9  
    10  	switch v.Kind() {
    11  	case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
    12  		if i64, ok := n.raw.(int64); ok {
    13  			v.SetInt(i64)
    14  			return nil
    15  		}
    16  		if f64, ok := n.raw.(float64); ok {
    17  			v.SetInt(int64(f64))
    18  			return nil
    19  		}
    20  	case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
    21  		if i64, ok := n.raw.(int64); ok {
    22  			v.SetUint(uint64(i64))
    23  			return nil
    24  		}
    25  		if f64, ok := n.raw.(float64); ok {
    26  			v.SetUint(uint64(f64))
    27  			return nil
    28  		}
    29  	case reflect.Float32, reflect.Float64:
    30  		if i64, ok := n.raw.(int64); ok {
    31  			v.SetFloat(float64(i64))
    32  			return nil
    33  		}
    34  		if f64, ok := n.raw.(float64); ok {
    35  			v.SetFloat(f64)
    36  			return nil
    37  		}
    38  	case reflect.Interface:
    39  		v.Set(reflect.ValueOf(n.raw))
    40  		return nil
    41  	default:
    42  		return fmt.Errorf("cannot decode number value to %s target", v.Kind())
    43  	}
    44  
    45  	return fmt.Errorf("internal value is not numeric")
    46  }