2022-07-07 20:11:50 +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.
|
|
|
|
|
|
|
|
// Package utils provides common utility functions.
|
|
|
|
//
|
|
|
|
// Provided functionalities:
|
|
|
|
// - sorting
|
|
|
|
// - comparators
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToString converts a value to string.
|
|
|
|
func ToString(value interface{}) string {
|
2024-10-16 10:54:40 +00:00
|
|
|
switch value := value.(type) {
|
2022-07-07 20:11:50 +00:00
|
|
|
case string:
|
2024-10-16 10:54:40 +00:00
|
|
|
return value
|
2022-07-07 20:11:50 +00:00
|
|
|
case int8:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatInt(int64(value), 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case int16:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatInt(int64(value), 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case int32:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatInt(int64(value), 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case int64:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatInt(value, 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case uint8:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatUint(uint64(value), 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case uint16:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatUint(uint64(value), 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case uint32:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatUint(uint64(value), 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case uint64:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatUint(value, 10)
|
2022-07-07 20:11:50 +00:00
|
|
|
case float32:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatFloat(float64(value), 'g', -1, 64)
|
2022-07-07 20:11:50 +00:00
|
|
|
case float64:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatFloat(value, 'g', -1, 64)
|
2022-07-07 20:11:50 +00:00
|
|
|
case bool:
|
2024-10-16 10:54:40 +00:00
|
|
|
return strconv.FormatBool(value)
|
2022-07-07 20:11:50 +00:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%+v", value)
|
|
|
|
}
|
|
|
|
}
|