github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/primitive/impl.go (about)

     1  /**
     2   * Copyright 2023 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 primitive
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  func ToBool(v interface{}) (bool, error) {
    26  	switch v := v.(type) {
    27  	case bool:
    28  		return v, nil
    29  	case int:
    30  		return v != 0, nil
    31  	case int8:
    32  		return v != 0, nil
    33  	case int16:
    34  		return v != 0, nil
    35  	case int32:
    36  		return v != 0, nil
    37  	case int64:
    38  		return v != 0, nil
    39  	case uint:
    40  		return v != 0, nil
    41  	case uint8:
    42  		return v != 0, nil
    43  	case uint16:
    44  		return v != 0, nil
    45  	case uint32:
    46  		return v != 0, nil
    47  	case uint64:
    48  		return v != 0, nil
    49  	case float32:
    50  		return v != 0, nil
    51  	case float64:
    52  		return v != 0, nil
    53  	case string:
    54  		return v != "", nil
    55  	case []byte:
    56  		return len(v) != 0, nil
    57  	default:
    58  		return false, fmt.Errorf("unsupported type %T", v)
    59  	}
    60  }
    61  
    62  func ToInt64(v interface{}) (int64, error) {
    63  	switch v := v.(type) {
    64  	case bool:
    65  		if v {
    66  			return 1, nil
    67  		} else {
    68  			return 0, nil
    69  		}
    70  	case int:
    71  		return int64(v), nil
    72  	case int8:
    73  		return int64(v), nil
    74  	case int16:
    75  		return int64(v), nil
    76  	case int32:
    77  		return int64(v), nil
    78  	case int64:
    79  		return v, nil
    80  	case uint:
    81  		return int64(v), nil
    82  	case uint8:
    83  		return int64(v), nil
    84  	case uint16:
    85  		return int64(v), nil
    86  	case uint32:
    87  		return int64(v), nil
    88  	case uint64:
    89  		return int64(v), nil
    90  	case float32:
    91  		return int64(v), nil
    92  	case float64:
    93  		return int64(v), nil
    94  	case string:
    95  		return strconv.ParseInt(v, 10, 64)
    96  	case []byte:
    97  		return strconv.ParseInt(string(v), 10, 64)
    98  	default:
    99  		return 0, fmt.Errorf("unsupported type %T", v)
   100  	}
   101  }
   102  
   103  func ToFloat64(v interface{}) (float64, error) {
   104  	switch v := v.(type) {
   105  	case bool:
   106  		if v {
   107  			return 1, nil
   108  		} else {
   109  			return 0, nil
   110  		}
   111  	case int:
   112  		return float64(v), nil
   113  	case int8:
   114  		return float64(v), nil
   115  	case int16:
   116  		return float64(v), nil
   117  	case int32:
   118  		return float64(v), nil
   119  	case int64:
   120  		return float64(v), nil
   121  	case uint:
   122  		return float64(v), nil
   123  	case uint8:
   124  		return float64(v), nil
   125  	case uint16:
   126  		return float64(v), nil
   127  	case uint32:
   128  		return float64(v), nil
   129  	case uint64:
   130  		return float64(v), nil
   131  	case float32:
   132  		return float64(v), nil
   133  	case float64:
   134  		return v, nil
   135  	case string:
   136  		return strconv.ParseFloat(v, 64)
   137  	case []byte:
   138  		return strconv.ParseFloat(string(v), 64)
   139  	default:
   140  		return 0, fmt.Errorf("unsupported type %T", v)
   141  	}
   142  }
   143  
   144  func ToString(v interface{}) (string, error) {
   145  	switch v := v.(type) {
   146  	case bool:
   147  		return strconv.FormatBool(v), nil
   148  	case int:
   149  		return strconv.Itoa(v), nil
   150  	case int8:
   151  		return strconv.Itoa(int(v)), nil
   152  	case int16:
   153  		return strconv.Itoa(int(v)), nil
   154  	case int32:
   155  		return strconv.Itoa(int(v)), nil
   156  	case int64:
   157  		return strconv.FormatInt(v, 10), nil
   158  	case uint:
   159  		return strconv.FormatUint(uint64(v), 10), nil
   160  	case uint8:
   161  		return strconv.FormatUint(uint64(v), 10), nil
   162  	case uint16:
   163  		return strconv.FormatUint(uint64(v), 10), nil
   164  	case uint32:
   165  		return strconv.FormatUint(uint64(v), 10), nil
   166  	case uint64:
   167  		return strconv.FormatUint(v, 10), nil
   168  	case float32:
   169  		return strconv.FormatFloat(float64(v), 'g', -1, 32), nil
   170  	case float64:
   171  		return strconv.FormatFloat(v, 'g', -1, 64), nil
   172  	case string:
   173  		return v, nil
   174  	case []byte:
   175  		return string(v), nil
   176  	// case []interface{}:
   177  	// 	out := make([]string, len(v))
   178  	// 	for i, v := range v {
   179  	// 		s, err := ToString(v)
   180  	// 		if err != nil {
   181  	// 			return "", err
   182  	// 		}
   183  	// 		out[i] = s
   184  	// 	}
   185  	// 	return strings.Join(out, ","), nil
   186  	default:
   187  		return fmt.Sprintf("%v", v), nil
   188  	}
   189  }
   190  
   191  func KitexToString(val interface{}) string {
   192  	switch v := val.(type) {
   193  	case bool:
   194  		return strconv.FormatBool(v)
   195  	case int8:
   196  		return strconv.FormatInt(int64(v), 10)
   197  	case int16:
   198  		return strconv.FormatInt(int64(v), 10)
   199  	case int32:
   200  		return strconv.FormatInt(int64(v), 10)
   201  	case int64:
   202  		return strconv.FormatInt(v, 10)
   203  	case float64:
   204  		return strconv.FormatFloat(v, 'f', -1, 64)
   205  	case string:
   206  		return v
   207  	case []interface{}:
   208  		strs := make([]string, len(v))
   209  		for i, item := range v {
   210  			strs[i] = KitexToString(item)
   211  		}
   212  		return strings.Join(strs, ",")
   213  	}
   214  	return fmt.Sprintf("%v", val)
   215  }