Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/types/interfaces.go
2070 views
1
// Taken from https://github.com/spf13/cast.
2
3
package types
4
5
import (
6
"bytes"
7
"encoding/hex"
8
"fmt"
9
"strconv"
10
"strings"
11
12
"github.com/asaskevich/govalidator"
13
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
14
)
15
16
// JSONScalarToString converts an interface coming from json to string
17
// Inspired from: https://github.com/cli/cli/blob/09b09810dd812e3ede54b59ad9d6912b946ac6c5/pkg/export/template.go#L72
18
func JSONScalarToString(input interface{}) (string, error) {
19
switch tt := input.(type) {
20
case string:
21
return ToString(tt), nil
22
case float64:
23
return ToString(tt), nil
24
case nil:
25
return ToString(tt), nil
26
case bool:
27
return ToString(tt), nil
28
default:
29
return "", fmt.Errorf("cannot convert type to string: %v", tt)
30
}
31
}
32
33
// ToString converts an interface to string in a quick way
34
func ToString(data interface{}) string {
35
switch s := data.(type) {
36
case nil:
37
return ""
38
case string:
39
return s
40
case bool:
41
return strconv.FormatBool(s)
42
case float64:
43
return strconv.FormatFloat(s, 'f', -1, 64)
44
case float32:
45
return strconv.FormatFloat(float64(s), 'f', -1, 32)
46
case int:
47
return strconv.Itoa(s)
48
case int64:
49
return strconv.FormatInt(s, 10)
50
case int32:
51
return strconv.Itoa(int(s))
52
case int16:
53
return strconv.FormatInt(int64(s), 10)
54
case int8:
55
return strconv.FormatInt(int64(s), 10)
56
case uint:
57
return strconv.FormatUint(uint64(s), 10)
58
case uint64:
59
return strconv.FormatUint(s, 10)
60
case uint32:
61
return strconv.FormatUint(uint64(s), 10)
62
case uint16:
63
return strconv.FormatUint(uint64(s), 10)
64
case uint8:
65
return strconv.FormatUint(uint64(s), 10)
66
case []byte:
67
return string(s)
68
case severity.Holder:
69
return s.Severity.String()
70
case severity.Severity:
71
return s.String()
72
case fmt.Stringer:
73
return s.String()
74
case error:
75
return s.Error()
76
default:
77
return fmt.Sprintf("%v", data)
78
}
79
}
80
81
// ToStringNSlice converts an interface to string in a quick way or to a slice with strings
82
// if the input is a slice of interfaces.
83
func ToStringNSlice(data interface{}) interface{} {
84
switch s := data.(type) {
85
case []interface{}:
86
var a []string
87
for _, v := range s {
88
a = append(a, ToString(v))
89
}
90
return a
91
default:
92
return ToString(data)
93
}
94
}
95
96
func ToHexOrString(data interface{}) string {
97
switch s := data.(type) {
98
case string:
99
if govalidator.IsASCII(s) {
100
return s
101
}
102
return hex.Dump([]byte(s))
103
case []byte:
104
return hex.Dump(s)
105
default:
106
return fmt.Sprintf("%v", data)
107
}
108
}
109
110
// ToStringSlice casts an interface to a []string type.
111
func ToStringSlice(i interface{}) []string {
112
var a []string
113
114
switch v := i.(type) {
115
case []interface{}:
116
for _, u := range v {
117
a = append(a, ToString(u))
118
}
119
return a
120
case []string:
121
return v
122
case string:
123
return strings.Fields(v)
124
default:
125
return nil
126
}
127
}
128
129
// ToByteSlice casts an interface to a []byte type.
130
func ToByteSlice(i interface{}) []byte {
131
switch v := i.(type) {
132
case []byte:
133
return v
134
case []string:
135
return []byte(strings.Join(v, ""))
136
case string:
137
return []byte(v)
138
case []interface{}:
139
var buff bytes.Buffer
140
for _, u := range v {
141
buff.WriteString(ToString(u))
142
}
143
return buff.Bytes()
144
default:
145
strValue := ToString(i)
146
return []byte(strValue)
147
}
148
}
149
150
// ToStringMap casts an interface to a map[string]interface{} type.
151
func ToStringMap(i interface{}) map[string]interface{} {
152
var m = map[string]interface{}{}
153
154
switch v := i.(type) {
155
case map[interface{}]interface{}:
156
for k, val := range v {
157
m[ToString(k)] = val
158
}
159
return m
160
case map[string]interface{}:
161
return v
162
default:
163
return nil
164
}
165
}
166
167