-
Notifications
You must be signed in to change notification settings - Fork 1
/
TSHandler.go
128 lines (115 loc) · 3.44 KB
/
TSHandler.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
128
package main
import (
"sort"
"strings"
)
func generateTSCode(schema *Schema) string {
var builder strings.Builder
processSchemaForTS(&builder, schema, "")
return builder.String()
}
func getTSType(data interface{}) string {
switch t := data.(type) {
case *Schema:
if t.Properties != nil {
return t.Title
} else if t.Items != nil {
return getTSType(t.Items) + "[]"
}
case map[string]interface{}:
dataType, ok := t["type"].(string)
if !ok {
return "unknown"
}
switch dataType {
case "integer":
return "number"
case "number":
return "number"
case "boolean":
return "boolean"
case "string":
return "string"
case "array":
items, ok := t["items"].(map[string]interface{})
if ok {
return getTSType(items) + "[]"
}
case "object":
title, ok := t["title"].(string)
if ok {
return getFirstWordFromTitle(title)
}
}
}
return "unknown"
}
func processSchemaForTS(builder *strings.Builder, schema *Schema, indent string) {
if schema.Properties != nil {
builder.WriteString(indent + "interface " + getFirstWordFromTitle(schema.Title) + " {\n")
var propertyNames []string
for name := range schema.Properties {
propertyNames = append(propertyNames, name)
}
sort.Strings(propertyNames)
for _, name := range propertyNames {
property := schema.Properties[name]
builder.WriteString(indent + "\t" + name + ": " + getTSType(property) + ",\n")
}
builder.WriteString(indent + "}\n\n")
for _, name := range propertyNames {
property := schema.Properties[name]
if propertyMap, ok := property.(map[string]interface{}); ok {
if nestedSchema, ok := propertyMap["properties"].(map[string]interface{}); ok {
nestedTitle, ok := propertyMap["title"].(string)
if !ok {
nestedTitle = name
}
nestedPropertyMap := nestedSchema
nestedSchema := &Schema{
Title: nestedTitle,
Properties: nestedPropertyMap,
}
processNestedObjectsForTS(builder, nestedSchema, indent+"", nestedTitle)
}
}
}
} else if schema.Items != nil {
builder.WriteString(indent + "interface " + getFirstWordFromTitle(schema.Title) + " {\n")
builder.WriteString(indent + "\t" + getTSType(schema.Items) + "[]" + "\n")
builder.WriteString(indent + "}\n\n")
processNestedObjectsForTS(builder, schema.Items, indent+"", schema.Items.Title)
}
}
func processNestedObjectsForTS(builder *strings.Builder, schema *Schema, indent string, structName string) {
if schema.Properties != nil {
builder.WriteString(indent + "interface " + getFirstWordFromTitle(structName) + " {\n")
var propertyNames []string
for name := range schema.Properties {
propertyNames = append(propertyNames, name)
}
sort.Strings(propertyNames)
for _, name := range propertyNames {
property := schema.Properties[name]
builder.WriteString(indent + "\t" + name + ": " + getTSType(property) + ",\n")
}
builder.WriteString(indent + "}\n\n")
for _, name := range propertyNames {
property := schema.Properties[name]
if propertyMap, ok := property.(map[string]interface{}); ok {
if nestedSchema, ok := propertyMap["properties"].(map[string]interface{}); ok {
nestedTitle, ok := propertyMap["title"].(string)
if !ok {
nestedTitle = name
}
nestedPropertyMap := nestedSchema
nestedSchema := &Schema{
Title: nestedTitle,
Properties: nestedPropertyMap,
}
processNestedObjectsForTS(builder, nestedSchema, indent+"", nestedTitle)
}
}
}
}
}