1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package dcl 16 17 import "fmt" 18 19 // CanonicalizeIntegerValue converts the numeric value for integer type to int64 because that's the integer type 20 // used by DCL. During json marshalling, all numeric values are converted to be of type float64; 21 // we canonicalize them to int64 before sending to DCL. 22 func CanonicalizeIntegerValue(val interface{}) (int64, error) { 23 switch val.(type) { 24 case int64: 25 return val.(int64), nil 26 case int: 27 return int64(val.(int)), nil 28 case float64: 29 return int64(val.(float64)), nil 30 default: 31 return 0, fmt.Errorf("expect to have one of the types (int, int64, float64) for the integer value, but got %T", val) 32 } 33 } 34 35 // CanonicalizeNumberValue converts the numeric value for number type to float64 because that's the double type 36 // used by DCL. 37 func CanonicalizeNumberValue(val interface{}) (float64, error) { 38 switch val.(type) { 39 case float64: 40 return val.(float64), nil 41 case float32: 42 return float64(val.(float32)), nil 43 case int64: 44 return float64(val.(int64)), nil 45 case int: 46 return float64(val.(int)), nil 47 default: 48 return 0, fmt.Errorf("expect to have one of the types (float64, float32, int64, int) for number value, but got %T", val) 49 } 50 } 51