-
Notifications
You must be signed in to change notification settings - Fork 64
/
util.go
52 lines (41 loc) · 1.03 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package util
import (
"context"
"errors"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"go.uber.org/zap"
)
type ctxKey string
const (
STRING_CORRELATION_ID string = "correlationId"
STRING_LOG ctxKey = "log"
)
func NewUuid() string {
return uuid.New().String()
}
func SetLogToCtx(c *gin.Context, logger *zap.Logger) {
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), STRING_LOG, logger))
}
func GetLogFromCtx(c *gin.Context) *zap.Logger {
logWithCid := c.Request.Context().Value(STRING_LOG).(*zap.Logger)
return logWithCid
}
func ConvertAnyToStr(input any) (string, error) {
converted := ""
if str, ok := input.(string); ok {
converted += str
} else if arr, ok := input.([]interface{}); ok {
for _, unknown := range arr {
str, ok := unknown.(string)
if ok {
converted += str
continue
}
return "", errors.New("input array contains a non string entry")
}
} else {
return "", errors.New("input is neither string nor an array of strings")
}
return converted, nil
}