github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/convert/convert.go (about)

     1  package convert
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  )
     8  
     9  func F64Slice(in []interface{}) ([]float64, error) {
    10  	var ret []float64
    11  	for _, e := range in {
    12  		if item, ok := e.(float64); ok {
    13  			ret = append(ret, item)
    14  		} else {
    15  			return nil, fmt.Errorf("expected slice of float64 but got: %v", in)
    16  		}
    17  	}
    18  
    19  	return ret, nil
    20  }
    21  
    22  func ItfToStrSlice(in interface{}) ([]string, error) {
    23  	ret := []string{}
    24  	raw, ok := in.([]interface{})
    25  	if !ok {
    26  		return ret, nil
    27  	}
    28  
    29  	for _, e := range raw {
    30  		item, ok := e.(string)
    31  		if !ok {
    32  			return nil, fmt.Errorf("expected slice of strings, got: %v", in)
    33  		}
    34  		ret = append(ret, item)
    35  	}
    36  
    37  	return ret, nil
    38  }
    39  
    40  // ToInt converts various types to integer. If fails, returns 0
    41  func ToInt(in interface{}) int {
    42  	var out int
    43  
    44  	switch v := in.(type) {
    45  	case string:
    46  		if i, err := strconv.Atoi(v); err == nil {
    47  			out = i
    48  		}
    49  	case float64:
    50  		out = int(v)
    51  	default:
    52  		if val, ok := in.(int); ok {
    53  			out = val
    54  		}
    55  	}
    56  
    57  	return out
    58  }
    59  
    60  func ToInterfaceArray(i []interface{}) [][]interface{} {
    61  	newArr := make([][]interface{}, len(i))
    62  	for index, item := range i {
    63  		newArr[index] = item.([]interface{})
    64  	}
    65  	return newArr
    66  }
    67  
    68  func ToInterface(flt []float64) []interface{} {
    69  	data := make([]interface{}, len(flt))
    70  	for j, f := range flt {
    71  		data[j] = f
    72  	}
    73  	return data
    74  }
    75  
    76  func ToFloat64Array(i [][]interface{}) ([][]float64, error) {
    77  	newArr := make([][]float64, len(i))
    78  	for index, item := range i {
    79  		s, err := F64Slice(item)
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  		newArr[index] = s
    84  	}
    85  	return newArr, nil
    86  }
    87  
    88  func FloatToJsonNumber(i interface{}) json.Number {
    89  	if r, ok := i.(json.Number); ok {
    90  		return r
    91  	}
    92  	return json.Number(strconv.FormatFloat(i.(float64), 'f', -1, 64))
    93  }
    94  
    95  func I64ValOrZero(in interface{}) (out int64) {
    96  	switch v := in.(type) {
    97  	case int:
    98  		out = int64(v)
    99  	default:
   100  		if v, ok := in.(float64); ok {
   101  			out = int64(v)
   102  		}
   103  	}
   104  
   105  	return out
   106  }
   107  
   108  func IValOrZero(i interface{}) int {
   109  	if r, ok := i.(float64); ok {
   110  		return int(r)
   111  	}
   112  	return 0
   113  }
   114  
   115  func F64ValOrZero(in interface{}) (out float64) {
   116  	switch v := in.(type) {
   117  	case int:
   118  		out = float64(v)
   119  	default:
   120  		if v, ok := in.(float64); ok {
   121  			out = v
   122  		}
   123  	}
   124  
   125  	return out
   126  }
   127  
   128  func SiMapOrEmpty(i interface{}) map[string]interface{} {
   129  	if m, ok := i.(map[string]interface{}); ok {
   130  		return m
   131  	}
   132  	return make(map[string]interface{})
   133  }
   134  
   135  func BValOrFalse(in interface{}) (out bool) {
   136  	switch v := in.(type) {
   137  	case int:
   138  		if v == 1 {
   139  			out = true
   140  		}
   141  	case string:
   142  		if v == "1" {
   143  			out = true
   144  		}
   145  	default:
   146  		if v, ok := in.(bool); ok {
   147  			out = v
   148  		}
   149  	}
   150  	return
   151  }
   152  
   153  func SValOrEmpty(i interface{}) string {
   154  	if r, ok := i.(string); ok {
   155  		return r
   156  	}
   157  	return ""
   158  }