-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
73 lines (64 loc) · 1.99 KB
/
parse.go
File metadata and controls
73 lines (64 loc) · 1.99 KB
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
package fmt
// KeyValue represents a key-value pair extracted from a string.
type KeyValue struct {
Key string // Key
Value string // Value
}
// TagPairs searches for a key in a Go struct tag and parses its value as multiple key-value pairs.
// Example: Convert(`options:"key1:text1,key2:text2"`).TagPairs("options") => []KeyValue{{Key: "key1", Value: "text1"}, {Key: "key2", Value: "text2"}}
func (c *Conv) TagPairs(key string) []KeyValue {
val, ok := c.TagValue(key)
if !ok || val == "" {
return nil
}
// Split by comma to get pairs
pairs := c.splitStr(val, ",")
res := make([]KeyValue, 0, len(pairs))
for _, pair := range pairs {
k, v, found := c.splitByDelimiterWithBuffer(pair, ":")
if found {
res = append(res, KeyValue{Key: k, Value: v})
}
}
return res
}
// TagValue searches for the value of a key in a Go struct tag-like string.
// Example: Convert(`json:"name" Label:"Nombre"`).TagValue("Label") => "Nombre", true
func (c *Conv) TagValue(key string) (string, bool) {
src := c.GetString(BuffOut)
// Reutilizar splitStr para dividir por espacios
parts := c.splitStr(src)
for _, part := range parts {
// Split by ':' using existing function
k, v, found := c.splitByDelimiterWithBuffer(part, ":")
if !found {
continue
}
if k == key {
// Remove quotes if present
if len(v) >= 2 && v[0] == '"' && v[len(v)-1] == '"' {
v = v[1 : len(v)-1]
}
return v, true
}
}
return "", false
}
// ExtractValue extracts the value after the first delimiter. If not found, returns an error.
// Usage: Convert("key:value").ExtractValue(":") => "value", nil
// If no delimiter is provided, uses ":" by default.
func (c *Conv) ExtractValue(delimiters ...string) (string, error) {
src := c.String()
d := ":"
if len(delimiters) > 0 && delimiters[0] != "" {
d = delimiters[0]
}
if src == d {
return "", nil
}
_, after, found := c.splitByDelimiterWithBuffer(src, d)
if !found {
return "", c.wrErr("format", "invalid", "delimiter", "not", "found")
}
return after, nil
}