2016-06-27 02:21:09 +00:00
|
|
|
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2016-06-25 16:17:48 +00:00
|
|
|
|
|
|
|
// Package utils provides common utility functions.
|
|
|
|
//
|
|
|
|
// Provided functionalities:
|
|
|
|
// - sorting
|
|
|
|
// - comparators
|
|
|
|
package utils
|
2017-03-06 02:40:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToString converts a value to string.
|
|
|
|
func ToString(value interface{}) string {
|
2019-01-24 05:59:56 +00:00
|
|
|
switch value := value.(type) {
|
2017-03-06 02:40:29 +00:00
|
|
|
case string:
|
2019-01-24 05:59:56 +00:00
|
|
|
return value
|
2017-03-06 02:40:29 +00:00
|
|
|
case int8:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatInt(int64(value), 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case int16:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatInt(int64(value), 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case int32:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatInt(int64(value), 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case int64:
|
2022-04-12 03:32:28 +00:00
|
|
|
return strconv.FormatInt(value, 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case uint8:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatUint(uint64(value), 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case uint16:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatUint(uint64(value), 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case uint32:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatUint(uint64(value), 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case uint64:
|
2022-04-12 03:32:28 +00:00
|
|
|
return strconv.FormatUint(value, 10)
|
2017-03-06 02:40:29 +00:00
|
|
|
case float32:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatFloat(float64(value), 'g', -1, 64)
|
2017-03-06 02:40:29 +00:00
|
|
|
case float64:
|
2022-04-12 03:32:28 +00:00
|
|
|
return strconv.FormatFloat(value, 'g', -1, 64)
|
2017-03-06 02:40:29 +00:00
|
|
|
case bool:
|
2019-01-24 05:59:56 +00:00
|
|
|
return strconv.FormatBool(value)
|
2017-03-06 02:40:29 +00:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%+v", value)
|
|
|
|
}
|
|
|
|
}
|