-
Notifications
You must be signed in to change notification settings - Fork 6
/
type_converters.go
132 lines (110 loc) · 3.18 KB
/
type_converters.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (C) 2018 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package util
import (
"crypto/rand"
"encoding/base64"
"io"
"strings"
)
// StringToPtr will convert string to pointer
func StringToPtr(i string) *string {
return &i
}
// IntToPtr will convert int to pointer
func IntToPtr(i int) *int {
return &i
}
// BoolToPtr will convert bool to pointer
func BoolToPtr(b bool) *bool {
return &b
}
// Int32ToInt will convert from int32 to int
func Int32ToInt(i *int32) int {
return int(*i)
}
// IntToInt32 will convert from int to int32
func IntToInt32(i int) *int32 {
j := int32(i)
return &j
}
// IntToInt64 will convert from int to int64
func IntToInt64(i int) *int64 {
j := int64(i)
return &j
}
// IntToUInt32 will convert from int to uint32
func IntToUInt32(i int) uint32 {
return uint32(i)
}
func getBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
// Base64Encode will return an encoded string using a URL-compatible base64 format
func Base64Encode(data []byte) string {
return base64.URLEncoding.EncodeToString(data)
}
// Base64Decode will return a decoded string using a URL-compatible base64 format;
// decoding may return an error, which you can check if you don’t already know the input to be well-formed.
func Base64Decode(data string) (string, error) {
uDec, err := base64.URLEncoding.DecodeString(data)
return string(uDec), err
}
// RandomString will generate the random string
func RandomString(n int) (string, error) {
b, err := getBytes(n)
return Base64Encode(b), err
}
// NewStringReader will convert string array to string reader object
func NewStringReader(ss []string) io.Reader {
formattedString := strings.Join(ss, "\n")
reader := strings.NewReader(formattedString)
return reader
}
// StringToStringSlice slices s into all substrings separated by sep
func StringToStringSlice(s string, sep string) []string {
if len(s) > 0 {
return strings.Split(s, sep)
}
return make([]string, 0)
}
// MapKeyToStringArray will return map keys
func MapKeyToStringArray(maps map[string]string) []string {
keys := make([]string, 0)
for key := range maps {
keys = append(keys, key)
}
return keys
}
// UniqueValues returns a unique subset of the string slice provided.
func UniqueValues(input []string) []string {
u := make([]string, 0, len(input))
m := make(map[string]bool)
for _, val := range input {
if _, ok := m[val]; !ok {
m[val] = true
u = append(u, val)
}
}
return u
}