utils.ConvertStrToInt   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
package utils
2
3
import (
4
	"strconv"
5
	"time"
6
7
	"github.com/golang-jwt/jwt/v4"
8
	"github.com/memnix/memnix-rest/config"
9
	"github.com/pkg/errors"
10
	"github.com/rs/zerolog/log"
11
)
12
13
// ConvertStrToUInt converts a string to an unsigned integer
14
func ConvertStrToUInt(str string) (uint, error) {
15
	number, err := strconv.ParseUint(str, config.Base10, config.BitSize)
16
	if err != nil {
17
		log.Debug().Err(err).Msgf("Error while converting string to uint: %s", err)
18
		return 0, errors.New("Error while converting string to uint")
19
	}
20
	return uint(number), nil
21
}
22
23
// ConvertUIntToStr converts an unsigned integer to a string
24
func ConvertUIntToStr(number uint) string {
25
	return strconv.FormatUint(uint64(number), config.Base10)
26
}
27
28
// ConvertStrToInt converts a string to an integer
29
func ConvertStrToInt(str string) (int, error) {
30
	number, err := strconv.ParseInt(str, config.Base10, config.BitSize)
31
	if err != nil {
32
		log.Debug().Err(err).Msgf("Error while converting string to int: %s", err)
33
		return 0, errors.New("Error while converting string to int")
34
	}
35
	return int(number), nil
36
}
37
38
// GetExpirationTime returns the expiration time
39
func GetExpirationTime() *jwt.NumericDate {
40
	return jwt.NewNumericDate(time.Now().Add(time.Hour * config.ExpirationTimeInHours))
41
}
42
43
// GetSecretKey returns the secret key
44
func GetSecretKey() string {
45
	return config.EnvHelper.GetEnv("SECRET_KEY")
46
}
47