github.com/Rookout/GoSDK@v0.1.48/pkg/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  	"path"
     8  	"reflect"
     9  	"strconv"
    10  	"strings"
    11  	"time"
    12  	"unsafe"
    13  
    14  	"github.com/golang/protobuf/ptypes"
    15  	"github.com/golang/protobuf/ptypes/timestamp"
    16  	"github.com/sirupsen/logrus"
    17  )
    18  
    19  const backendCompatibleIsoTimeFormat = "2006-01-02T15:04:05.000000Z"
    20  
    21  var TrueValues = []string{"y", "yes", "true", "1"}
    22  
    23  func MakeSliceFromPointer(p uintptr, length int) []byte {
    24  	return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
    25  		Data: p,
    26  		Len:  length,
    27  		Cap:  length,
    28  	}))
    29  }
    30  
    31  func BoolAsInt(b bool) int {
    32  	if b {
    33  		return 1
    34  	}
    35  	return 0
    36  }
    37  
    38  func IntAsBool(i int32) bool {
    39  	return !(0 == i)
    40  }
    41  
    42  func CreateByteArray(size int) interface{} {
    43  	array := make([]byte, size)
    44  	for i := range array {
    45  		array[i] = byte(i + 60)
    46  	}
    47  	return array
    48  }
    49  
    50  func StringToFloat(str string) (float64, error) {
    51  	return strconv.ParseFloat(str, 64)
    52  }
    53  
    54  func StringToInt(str string) (int, error) {
    55  	return strconv.Atoi(str)
    56  }
    57  
    58  func StringToBool(str string) (bool, error) {
    59  	return strconv.ParseBool(str)
    60  }
    61  
    62  func BoolToString(b bool) string {
    63  	return strconv.FormatBool(b)
    64  }
    65  
    66  func MapStringToMapInterface(orig *map[string]interface{}) *map[interface{}]interface{} {
    67  	retVal := make(map[interface{}]interface{})
    68  	for key, val := range *orig {
    69  		retVal[key] = val
    70  	}
    71  	return &retVal
    72  }
    73  
    74  func InterfaceToMap(i interface{}) *map[interface{}]interface{} {
    75  	if m, ok := i.(map[interface{}]interface{}); ok {
    76  		return &m
    77  	}
    78  
    79  	retVal := make(map[interface{}]interface{})
    80  
    81  	for _, key := range reflect.ValueOf(i).MapKeys() {
    82  		val := reflect.ValueOf(i).MapIndex(key)
    83  
    84  		retVal[key.Interface()] = val.Interface()
    85  	}
    86  
    87  	return &retVal
    88  }
    89  
    90  func GetFormattedTime() string {
    91  	return fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05"))
    92  }
    93  
    94  func ReplaceAll(original string, old string, new string) string {
    95  	return strings.Replace(original, old, new, -1)
    96  }
    97  
    98  func GetFloat(o interface{}) (float64, bool) {
    99  	if o == nil {
   100  		return 0, false
   101  	}
   102  
   103  	switch o.(type) {
   104  	case float64:
   105  		return o.(float64), true
   106  	case int:
   107  		return float64(o.(int)), true
   108  	case int32:
   109  		return float64(o.(int32)), true
   110  	case int64:
   111  		return float64(o.(int64)), true
   112  	default:
   113  		return 0, false
   114  	}
   115  }
   116  
   117  func MergeUrls(baseUrl, postfix string) (string, error) {
   118  	u, err := url.Parse(baseUrl)
   119  	if err != nil {
   120  		return "", err
   121  	}
   122  
   123  	u.Path = path.Join(u.Path, postfix)
   124  	return u.String(), nil
   125  }
   126  
   127  func CreateDirectory(dirName string) error {
   128  	if _, err := os.Stat(dirName); os.IsNotExist(err) {
   129  		err := os.MkdirAll(dirName, os.ModePerm)
   130  		if err != nil {
   131  			return err
   132  		}
   133  	}
   134  	return nil
   135  }
   136  
   137  func TimeToBackendCompatibleIsoTimeFormat(t time.Time) string {
   138  	return t.Format(backendCompatibleIsoTimeFormat)
   139  }
   140  
   141  func TimeFromBackendCompatibleIsoTimeFormat(t string) time.Time {
   142  	ret, err := time.Parse(backendCompatibleIsoTimeFormat, t)
   143  	if err != nil {
   144  		logrus.WithError(err).Error("unexpected error while converting backend time to go Time")
   145  	}
   146  	return ret
   147  }
   148  
   149  func ProtoToBackendCompatibleIsoTimeFormat(t *timestamp.Timestamp) string {
   150  	protoAsTime := time.Unix(t.Seconds, (int64)(t.Nanos))
   151  	return TimeToBackendCompatibleIsoTimeFormat(protoAsTime)
   152  }
   153  
   154  func TimeToProtobufTimestamp(t *time.Time) *timestamp.Timestamp {
   155  	if t != nil {
   156  		out, _ := ptypes.TimestampProto(*t)
   157  		return out
   158  	}
   159  	return nil
   160  }
   161  
   162  
   163  func StrSliceDiff(base, other []string) (diff, equal []string) {
   164  	otherAsMap := make(map[string]struct{}, len(other))
   165  	for _, value := range other {
   166  		otherAsMap[value] = struct{}{}
   167  	}
   168  	for _, value := range base {
   169  		if _, found := otherAsMap[value]; !found {
   170  			diff = append(diff, value)
   171  		} else {
   172  			equal = append(equal, value)
   173  		}
   174  	}
   175  	return diff, equal
   176  }
   177  
   178  func MSToNS(milliseconds int64) time.Duration {
   179  	return time.Duration(milliseconds * 1000000)
   180  }
   181  
   182  func StringMSToNS(milliseconds string) (time.Duration, error) {
   183  	i, err := strconv.Atoi(milliseconds)
   184  	if err != nil {
   185  		return -1, err
   186  	}
   187  
   188  	return MSToNS(int64(i)), nil
   189  }
   190  
   191  func FloatMSToNS(milliseconds float64) int64 {
   192  	return int64(milliseconds) * 1000000
   193  }
   194  
   195  func NowInNS() int64 {
   196  	return time.Now().UnixNano()
   197  }
   198  
   199  func Int64Max(a int64, b int64) int64 {
   200  	if a > b {
   201  		return a
   202  	}
   203  
   204  	return b
   205  }
   206  
   207  func UnsafePointer(value reflect.Value) unsafe.Pointer {
   208  	return unsafe.Pointer(value.Pointer())
   209  }