github.com/alexandrestein/gods@v1.0.1/utils/utils.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package utils provides common utility functions.
     6  //
     7  // Provided functionalities:
     8  // - sorting
     9  // - comparators
    10  package utils
    11  
    12  import (
    13  	"fmt"
    14  	"strconv"
    15  	"time"
    16  )
    17  
    18  // ToString converts a value to string.
    19  func ToString(value interface{}) string {
    20  	switch value.(type) {
    21  	case string:
    22  		return value.(string)
    23  	case int8:
    24  		return strconv.FormatInt(int64(value.(int8)), 10)
    25  	case int16:
    26  		return strconv.FormatInt(int64(value.(int16)), 10)
    27  	case int32:
    28  		return strconv.FormatInt(int64(value.(int32)), 10)
    29  	case int64:
    30  		return strconv.FormatInt(int64(value.(int64)), 10)
    31  	case uint8:
    32  		return strconv.FormatUint(uint64(value.(uint8)), 10)
    33  	case uint16:
    34  		return strconv.FormatUint(uint64(value.(uint16)), 10)
    35  	case uint32:
    36  		return strconv.FormatUint(uint64(value.(uint32)), 10)
    37  	case uint64:
    38  		return strconv.FormatUint(uint64(value.(uint64)), 10)
    39  	case float32:
    40  		return strconv.FormatFloat(float64(value.(float32)), 'g', -1, 64)
    41  	case float64:
    42  		return strconv.FormatFloat(float64(value.(float64)), 'g', -1, 64)
    43  	case time.Time:
    44  		return value.(time.Time).Format(time.RFC3339Nano)
    45  	case bool:
    46  		return strconv.FormatBool(value.(bool))
    47  	default:
    48  		return fmt.Sprintf("%+v", value)
    49  	}
    50  }
    51  
    52  // FromString converts to string a value.
    53  func FromString(value string, t ComparatorType) interface{} {
    54  	switch t {
    55  	case StringComparatorType:
    56  		return value
    57  	case IntComparatorType:
    58  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    59  		return int(typedKey)
    60  	case Int8ComparatorType:
    61  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    62  		return int8(typedKey)
    63  	case Int16ComparatorType:
    64  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    65  		return int16(typedKey)
    66  	case Int32ComparatorType:
    67  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    68  		return int32(typedKey)
    69  	case Int64ComparatorType:
    70  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    71  		return int64(typedKey)
    72  	case UIntComparatorType:
    73  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    74  		return uint(typedKey)
    75  	case UInt8ComparatorType:
    76  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    77  		return uint8(typedKey)
    78  	case UInt16ComparatorType:
    79  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    80  		return uint16(typedKey)
    81  	case UInt32ComparatorType:
    82  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    83  		return uint32(typedKey)
    84  	case UInt64ComparatorType:
    85  		typedKey, _ := strconv.ParseInt(value, 10, 64)
    86  		return uint64(typedKey)
    87  	case Float32ComparatorType:
    88  		typedKey, _ := strconv.ParseFloat(value, 64)
    89  		return float32(typedKey)
    90  	case Float64ComparatorType:
    91  		typedKey, _ := strconv.ParseFloat(value, 64)
    92  		return float64(typedKey)
    93  	case ByteComparatorType:
    94  		return []byte(value)
    95  	case TimeComparatorType:
    96  		t, _ := time.Parse(time.RFC3339Nano, value)
    97  		return t
    98  	default:
    99  		return value
   100  	}
   101  }