Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/fuzz/dataformat/kv.go
2070 views
1
package dataformat
2
3
import (
4
mapsutil "github.com/projectdiscovery/utils/maps"
5
"golang.org/x/exp/maps"
6
)
7
8
// KV is a key-value struct
9
// that is implemented or used by fuzzing package
10
// to represent a key-value pair
11
// sometimes order or key-value pair is important (query params)
12
// so we use ordered map to represent the data
13
// if it's not important/significant (ex: json,xml) we use map
14
// this also allows us to iteratively implement ordered map
15
type KV struct {
16
Map mapsutil.Map[string, any]
17
OrderedMap *mapsutil.OrderedMap[string, any]
18
}
19
20
// Clones the current state of the KV struct
21
func (kv *KV) Clone() KV {
22
newKV := KV{}
23
if kv.OrderedMap == nil {
24
newKV.Map = maps.Clone(kv.Map)
25
return newKV
26
}
27
clonedOrderedMap := kv.OrderedMap.Clone()
28
newKV.OrderedMap = &clonedOrderedMap
29
return newKV
30
}
31
32
// IsNIL returns true if the KV struct is nil
33
func (kv *KV) IsNIL() bool {
34
return kv.Map == nil && kv.OrderedMap == nil
35
}
36
37
// IsOrderedMap returns true if the KV struct is an ordered map
38
func (kv *KV) IsOrderedMap() bool {
39
return kv.OrderedMap != nil
40
}
41
42
// Set sets a value in the KV struct
43
func (kv *KV) Set(key string, value any) {
44
if kv.OrderedMap != nil {
45
kv.OrderedMap.Set(key, value)
46
return
47
}
48
if kv.Map == nil {
49
kv.Map = make(map[string]interface{})
50
}
51
kv.Map[key] = value
52
}
53
54
// Get gets a value from the KV struct
55
func (kv *KV) Get(key string) interface{} {
56
if kv.OrderedMap != nil {
57
value, ok := kv.OrderedMap.Get(key)
58
if !ok {
59
return nil
60
}
61
return value
62
}
63
return kv.Map[key]
64
}
65
66
// Iterate iterates over the KV struct in insertion order
67
func (kv *KV) Iterate(f func(key string, value any) bool) {
68
if kv.OrderedMap != nil {
69
kv.OrderedMap.Iterate(func(key string, value any) bool {
70
return f(key, value)
71
})
72
return
73
}
74
for key, value := range kv.Map {
75
if !f(key, value) {
76
break
77
}
78
}
79
}
80
81
// Delete deletes a key from the KV struct
82
func (kv *KV) Delete(key string) bool {
83
if kv.OrderedMap != nil {
84
_, ok := kv.OrderedMap.Get(key)
85
if !ok {
86
return false
87
}
88
kv.OrderedMap.Delete(key)
89
return true
90
}
91
_, ok := kv.Map[key]
92
if !ok {
93
return false
94
}
95
delete(kv.Map, key)
96
return true
97
}
98
99
// KVMap returns a new KV struct with the given map
100
func KVMap(data map[string]interface{}) KV {
101
return KV{Map: data}
102
}
103
104
// KVOrderedMap returns a new KV struct with the given ordered map
105
func KVOrderedMap(data *mapsutil.OrderedMap[string, any]) KV {
106
return KV{OrderedMap: data}
107
}
108
109
// ToMap converts the ordered map to a map
110
func ToMap(m *mapsutil.OrderedMap[string, any]) map[string]interface{} {
111
data := make(map[string]interface{})
112
m.Iterate(func(key string, value any) bool {
113
data[key] = value
114
return true
115
})
116
return data
117
}
118
119
// ToOrderedMap converts the map to an ordered map
120
func ToOrderedMap(data map[string]interface{}) *mapsutil.OrderedMap[string, any] {
121
m := mapsutil.NewOrderedMap[string, any]()
122
for key, value := range data {
123
m.Set(key, value)
124
}
125
return &m
126
}
127
128