-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathxj2s.go
59 lines (53 loc) · 1.35 KB
/
xj2s.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
package xj2s
import (
"github.com/clbanning/mxj"
// "fmt"
// "regexp"
"strings"
)
type StructNode struct {
Name string
Type string
Path string
}
func Xml2Struct(xdata []byte, Nesting bool) string {
m, err := mxj.NewMapXml(xdata)
if err != nil {
panic(err)
}
paths := m.LeafPaths()
if Nesting {
return "Not implement yet..."
} else {
RootName, RootStruct, RestStructs := XmlPath2SrtructLinesNoNesting(paths)
return RootDatas2Struct(RootName, RootStruct, RestStructs)
}
}
func Json2Struct(jdata []byte, RootName string, Nesting bool) string {
m, err := mxj.NewMapJson(jdata)
if err != nil {
panic(err)
}
paths := m.LeafPaths()
if Nesting {
return "Not implement yet..."
} else {
RootStruct, RestStructs := JsonPath2SrtructLinesNoNesting(paths)
return RootDatas2Struct(strings.Title(RootName), RootStruct, RestStructs)
}
}
func RootDatas2Struct(RootName string, RootLines map[string]StructNode, RestStructs map[string]map[string]StructNode) string {
Structs := "type " + RootName + " struct {\n"
for _, v := range RootLines {
Structs += "\t" + v.Name + "\t" + v.Type + "\t" + v.Path + "\n"
}
Structs += "}\n\n"
for NodeName, v1 := range RestStructs {
Structs += "type " + NodeName + " struct {\n"
for _, v2 := range v1 {
Structs += "\t" + v2.Name + "\t" + v2.Type + "\t" + v2.Path + "\n"
}
Structs += "}\n"
}
return Structs
}