This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
properties.go
90 lines (79 loc) · 2.4 KB
/
properties.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
package internal
import (
"fmt"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
func Properties2Yaml(propertiesData string) (string, error) {
// Parse properties
dataMap := make(map[string]interface{})
lines := strings.Split(propertiesData, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return "", fmt.Errorf("invalid properties line: %s", line)
}
key, value := parts[0], parts[1]
err := addToMap(dataMap, key, value)
if err != nil {
return "", err
}
}
// Convert map to YAML
yamlData, _ := yaml.Marshal(dataMap)
return string(yamlData), nil
}
func addToMap(dataMap map[string]interface{}, key string, value string) error {
parts := strings.Split(key, ".")
lastMap := dataMap
for i, part := range parts {
if strings.HasSuffix(part, "]") {
err := handleArrayKey(lastMap, part, value)
if err != nil {
return err
}
} else if i == len(parts)-1 {
handleNormalKey(lastMap, part, value)
} else {
lastMap = handleNestedKey(lastMap, part)
}
}
return nil
}
// handle the logic when the properties key is an array example -> child[0]=value
func handleArrayKey(lastMap map[string]interface{}, part string, value string) error {
// extra the name of the array example name[0] --> name
arrayKey := part[:strings.Index(part, "[")]
// extra the index of the array example name[0] --> 0
indexStr := part[strings.Index(part, "[")+1 : len(part)-1]
index, err := strconv.Atoi(indexStr)
if err != nil {
return err
}
if _, ok := lastMap[arrayKey]; !ok {
lastMap[arrayKey] = make([]interface{}, index+1)
} else if len(lastMap[arrayKey].([]interface{})) <= index {
oldArray := lastMap[arrayKey].([]interface{})
newArray := make([]interface{}, index+1)
copy(newArray, oldArray)
lastMap[arrayKey] = newArray
}
lastMap[arrayKey].([]interface{})[index] = value
return nil
}
// handle the logic when the properties key is a normal key example -> root=value
func handleNormalKey(lastMap map[string]interface{}, part string, value string) {
lastMap[part] = value
}
// handle the logic when the properties key is a nested key example -> root.child=value
func handleNestedKey(lastMap map[string]interface{}, part string) map[string]interface{} {
if _, ok := lastMap[part]; !ok {
lastMap[part] = make(map[string]interface{})
}
return lastMap[part].(map[string]interface{})
}