github.com/cloudwego/kitex@v0.9.0/pkg/generic/descriptor/util.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 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 package descriptor 18 19 import ( 20 "fmt" 21 "strconv" 22 "strings" 23 ) 24 25 func convertToString(val interface{}) string { 26 switch v := val.(type) { 27 case bool: 28 return strconv.FormatBool(v) 29 case int8: 30 return strconv.FormatInt(int64(v), 10) 31 case int16: 32 return strconv.FormatInt(int64(v), 10) 33 case int32: 34 return strconv.FormatInt(int64(v), 10) 35 case int64: 36 return strconv.FormatInt(v, 10) 37 case float64: 38 return strconv.FormatFloat(v, 'f', -1, 64) 39 case string: 40 return v 41 case []interface{}: 42 strs := make([]string, len(v)) 43 for i, item := range v { 44 strs[i] = convertToString(item) 45 } 46 return strings.Join(strs, ",") 47 } 48 return fmt.Sprintf("%v", val) 49 } 50 51 func convertToInt32(val interface{}) int32 { 52 switch v := val.(type) { 53 case int8: 54 return int32(v) 55 case int16: 56 return int32(v) 57 case int32: 58 return v 59 case int64: 60 return int32(v) 61 case float64: 62 return int32(v) 63 case string: 64 i, _ := strconv.ParseInt(v, 10, 32) 65 return int32(i) 66 } 67 return 0 68 }