-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
127 lines (99 loc) · 2.82 KB
/
parse.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ts2go
import (
"strings"
"github.com/armsnyder/typescript-ast-go/ast"
)
func parseSourceFile(sourceFile *ast.SourceFile) (*TemplateData, error) {
data := &TemplateData{
PackageName: DefaultPackageName,
}
ast.Inspect(sourceFile, func(node ast.Node) bool {
switch node := node.(type) {
case *ast.TypeAliasDeclaration:
handleTypeAliasDeclaration(data, node)
case *ast.InterfaceDeclaration:
handleInterfaceDeclaration(data, node)
}
return true
})
return data, nil
}
func handleTypeAliasDeclaration(data *TemplateData, node *ast.TypeAliasDeclaration) {
switch typ := node.Type.(type) {
case *ast.TypeLiteral:
data.Structs = append(data.Structs, createStruct(node.Name.Text, node.LeadingComment, typ.Members))
case *ast.TypeReference:
switch typeName := typ.TypeName.(type) {
case *ast.Identifier:
data.TypeAliases = append(data.TypeAliases, createTypeAlias(node.Name.Text, typeName.Text, node.LeadingComment))
case *ast.TypeLiteral:
data.Structs = append(data.Structs, createStruct(node.Name.Text, node.LeadingComment, typeName.Members))
}
}
}
func handleInterfaceDeclaration(data *TemplateData, node *ast.InterfaceDeclaration) {
stru := createStruct(node.Name.Text, node.LeadingComment, node.Members)
for _, h := range node.HeritageClauses {
for _, t := range h.Types {
stru.Embeds = append(stru.Embeds, t.Expression.Text)
}
}
data.Structs = append(data.Structs, stru)
}
func createStruct(name, leadingComment string, members []ast.Signature) *Struct {
stru := &Struct{
Name: name,
}
if leadingComment != "" {
stru.Doc = strings.Split(leadingComment, "\n")
}
for _, mem := range members {
switch mem := mem.(type) {
case *ast.PropertySignature:
addFieldFromPropertySignature(stru, mem)
case *ast.IndexSignature:
// TODO: Handle index signatures
}
}
return stru
}
func addFieldFromPropertySignature(stru *Struct, prop *ast.PropertySignature) {
field := &Field{
JSONName: prop.Name.Text,
Name: strings.ToUpper(prop.Name.Text[:1]) + prop.Name.Text[1:],
OmitEmpty: prop.QuestionToken,
IsPointer: prop.QuestionToken,
}
if prop.LeadingComment != "" {
field.Doc = strings.Split(prop.LeadingComment, "\n")
} else if prop.TrailingComment != "" {
field.Doc = []string{prop.TrailingComment}
}
switch t := prop.Type.(type) {
case *ast.TypeReference:
switch t := t.TypeName.(type) {
case *ast.Identifier:
switch t.Text {
case "boolean":
field.Type = "bool"
default:
field.Type = t.Text
}
default:
field.Type = "any"
}
default:
field.Type = "any"
}
stru.Fields = append(stru.Fields, field)
}
func createTypeAlias(name, referencedName, leadingComment string) *TypeAlias {
alias := &TypeAlias{
Name: name,
Type: referencedName,
}
if leadingComment != "" {
alias.Doc = strings.Split(leadingComment, "\n")
}
return alias
}