-
Notifications
You must be signed in to change notification settings - Fork 0
/
idepend.go
84 lines (71 loc) · 1.97 KB
/
idepend.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
package main
import (
"bufio"
"encoding/xml"
"fmt"
"os"
"sort"
"strings"
)
type IDependModel struct {
ID int `xml:"id"`
Name string `xml:"name"`
Desc string `xml:"description"`
OrgID string `xml:"organisation_id"`
Checksum string `xml:"checksum"`
Entities []IDependNode `xml:"entities>entity"`
}
type IDependNode struct {
ID int `xml:"id"`
Name string `xml:"name"`
OrgID string `xml:"organisation_id"`
Dependencies []int `xml:"dependencies>id"`
}
/*********************************************************************/
func (dm *IDependModel) ParseFile(in string, out string) error {
rawdata, err := os.ReadFile(in)
if err != nil {
return err
}
if err := xml.Unmarshal(rawdata, &dm); err != nil {
return err
}
/* Sort the entities based on their ID. */
sort.Slice(dm.Entities, func(i, j int) bool {
return dm.Entities[i].ID < dm.Entities[j].ID
})
/* Output file */
f, err := os.Create(out)
if err != nil {
return err
}
w := bufio.NewWriter(f)
/* assume the root node is the one with the lowest ID */
rootNode := dm.Entities[0]
dm.WalkPrint(w, rootNode.Name, rootNode.Dependencies)
return nil
}
/*********************************************************************/
func (dm *IDependModel) WalkPrint(w *bufio.Writer, prefix string, dependencies []int) {
for _, d := range dependencies {
dependency := dm.GetByID(d)
name := strings.Replace(dependency.Name, "/", "\\", -1)
name = strings.Replace(name, ",", "", -1)
_, err := w.WriteString(fmt.Sprintf("%s/%s\n", prefix, name))
if err != nil {
return
}
if len(dependency.Dependencies) != 0 {
dm.WalkPrint(w, prefix+"/"+dependency.Name, dependency.Dependencies)
}
}
}
/*********************************************************************/
func (dm *IDependModel) GetByID(id int) IDependNode {
for i := range dm.Entities {
if dm.Entities[i].ID == id {
return dm.Entities[i]
}
}
return IDependNode{}
}