github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/util/converter/converter.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package converter
    18  
    19  import (
    20  	"encoding/base64"
    21  	"fmt"
    22  	"math"
    23  	"reflect"
    24  	"strconv"
    25  	"strings"
    26  	"time"
    27  )
    28  
    29  func ToTrimRegexFromQueryParameter(v string) string {
    30  	return strings.Split(v, ":")[0]
    31  }
    32  
    33  func StringToInt64(s string) int64 {
    34  	i, _ := strconv.ParseInt(s, 10, 64)
    35  	return i
    36  }
    37  
    38  func StringToInt(s string) int {
    39  	i, _ := strconv.Atoi(s)
    40  	return i
    41  }
    42  
    43  func StringToFloat(s string) float64 {
    44  	i, _ := strconv.ParseFloat(s, 64)
    45  	return i
    46  }
    47  
    48  func IntToString(i int) string {
    49  	return strconv.Itoa(i)
    50  }
    51  
    52  func StringToBool(s string) bool {
    53  	s = strings.ToLower(s)
    54  	if s == "true" || s == "1" || s == "t" {
    55  		return true
    56  	}
    57  	return false
    58  }
    59  
    60  func StringToUint(s string) uint {
    61  	i, _ := strconv.ParseUint(s, 10, 64)
    62  	return uint(i)
    63  }
    64  
    65  func BoolToString(str string) (bool, error) {
    66  	switch str {
    67  	case "":
    68  		return false, nil
    69  	case "1", "t", "T", "true", "TRUE", "True":
    70  		return true, nil
    71  	case "0", "f", "F", "false", "FALSE", "False":
    72  		return false, nil
    73  	}
    74  	return false, fmt.Errorf("parse bool string: %s", str)
    75  }
    76  
    77  func Int64ToInt(i int64) int {
    78  	return StringToInt(strconv.FormatInt(i, 10))
    79  }
    80  
    81  func DecodeBase64(s string) string {
    82  	buf, _ := base64.StdEncoding.DecodeString(s)
    83  	return string(buf)
    84  }
    85  
    86  func EncodeBase64(s string) string {
    87  	return base64.StdEncoding.EncodeToString([]byte(s))
    88  }
    89  
    90  func HumanizeTimeDuration(duration time.Duration) string {
    91  	hours := int64(math.Mod(duration.Hours(), 24))
    92  	minutes := int64(math.Mod(duration.Minutes(), 60))
    93  	seconds := int64(math.Mod(duration.Seconds(), 60))
    94  
    95  	chunks := []struct {
    96  		amount int64
    97  	}{
    98  		{hours},
    99  		{minutes},
   100  		{seconds},
   101  	}
   102  
   103  	parts := make([]string, 0)
   104  
   105  	for _, chunk := range chunks {
   106  		switch chunk.amount {
   107  		case 0:
   108  			parts = append(parts, "00")
   109  			continue
   110  		default:
   111  			if chunk.amount >= 10 {
   112  				parts = append(parts, fmt.Sprintf("%d", chunk.amount))
   113  			} else {
   114  				parts = append(parts, fmt.Sprintf("0%d", chunk.amount))
   115  			}
   116  		}
   117  	}
   118  
   119  	return strings.Join(parts, ":")
   120  }
   121  
   122  func EnforcePtr(obj interface{}) (reflect.Value, error) {
   123  	v := reflect.ValueOf(obj)
   124  	if v.Kind() != reflect.Ptr {
   125  		if v.Kind() == reflect.Invalid {
   126  			return reflect.Value{}, fmt.Errorf("expected pointer, but got invalid kind")
   127  		}
   128  		return reflect.Value{}, fmt.Errorf("expected pointer, but got %v type", v.Type())
   129  	}
   130  	if v.IsNil() {
   131  		return reflect.Value{}, fmt.Errorf("expected pointer, but got nil")
   132  	}
   133  	return v.Elem(), nil
   134  }
   135  
   136  func ToBoolPointer(v bool) *bool {
   137  	return &v
   138  }
   139  
   140  func ToStringPointer(v string) *string {
   141  	return &v
   142  }
   143  
   144  func ToIntPointer(v int) *int {
   145  	return &v
   146  }
   147  
   148  func ToInt64Pointer(v int64) *int64 {
   149  	return &v
   150  }
   151  
   152  func ToDurationPointer(v time.Duration) *time.Duration {
   153  	return &v
   154  }