mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2026-10-20 08:16:40 +00:00
95 lines
1.6 KiB
Go
95 lines
1.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
func NewBool(t bool) *bool {
|
|
return &t
|
|
}
|
|
|
|
func NewString(t string) *string {
|
|
if t == "" {
|
|
return nil
|
|
}
|
|
return &t
|
|
}
|
|
|
|
func NewInt(t int) *int {
|
|
return &t
|
|
}
|
|
|
|
func NewBoolFromInterface(val interface{}) bool {
|
|
switch val.(type) {
|
|
case bool:
|
|
return val.(bool)
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func NewInt64pFromInterface(val interface{}) *int64 {
|
|
switch val.(type) {
|
|
case int64:
|
|
v := val.(int64)
|
|
return &v
|
|
case float64:
|
|
v := int64(val.(float64))
|
|
return &v
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func NewStringpFromInterface(val interface{}) *string {
|
|
switch val.(type) {
|
|
case string:
|
|
v := val.(string)
|
|
return &v
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func NewStringFromInterface(val interface{}) string {
|
|
switch val.(type) {
|
|
case string:
|
|
return val.(string)
|
|
case float64:
|
|
return fmt.Sprintf("%d", int64(val.(float64)))
|
|
case bool:
|
|
return fmt.Sprintf("%t", val.(bool))
|
|
case []interface{}:
|
|
list := val.([]interface{})
|
|
strSlice := make([]string, len(list))
|
|
for i, el := range list {
|
|
strSlice[i] = NewStringFromInterface(el)
|
|
}
|
|
return strings.Join(strSlice, ",")
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func NewReadCloserFromBytes(t []byte) io.ReadCloser {
|
|
return io.NopCloser(bytes.NewReader(t))
|
|
}
|
|
|
|
func NewReadCloserFromReader(r io.Reader) io.ReadCloser {
|
|
return io.NopCloser(r)
|
|
}
|
|
|
|
func MapStringInterfaceToMapStringString(m map[string]interface{}) map[string]string {
|
|
res := make(map[string]string)
|
|
for key, value := range m {
|
|
res[key] = fmt.Sprintf("%v", value)
|
|
if res[key] == "<nil>" {
|
|
res[key] = ""
|
|
}
|
|
}
|
|
return res
|
|
}
|