-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJavaHandler.go
120 lines (110 loc) · 3.07 KB
/
JavaHandler.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
package main
import (
"sort"
"strings"
)
func generateJavaCode(schema *Schema) string {
var builder strings.Builder
processSchemaForJava(&builder, schema, "")
return builder.String()
}
func getJavaType(property interface{}) string {
switch p := property.(type) {
case map[string]interface{}:
if pType, ok := p["type"].(string); ok {
switch pType {
case "string":
return "String"
case "number", "decimal":
return "double"
case "integer":
return "int"
case "boolean":
return "boolean"
case "object":
title, ok := p["title"].(string)
if ok {
return getFirstWordFromTitle(title)
}
}
}
}
return "unknown"
}
func getItemJavaType(property interface{}) string {
switch p := property.(type) {
case map[string]interface{}:
if pType, ok := p["type"].(string); ok {
switch pType {
case "string":
return "String"
case "number", "decimal":
return "double"
case "integer":
return "int"
case "object":
title, ok := p["title"].(string)
if ok {
return getFirstWordFromTitle(title)
}
}
}
}
return "unknown"
}
func processSchemaForJava(builder *strings.Builder, schema *Schema, indent string) {
if schema.Properties != nil {
var propertyNames []string
for name := range schema.Properties {
propertyNames = append(propertyNames, name)
}
sort.Strings(propertyNames)
if schema.Title != "" {
firstClassName := getFirstWordFromTitle(schema.Title)
addToTypedefStructsList(firstClassName)
}
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,
}
processSchemaForJava(builder, nestedSchema, indent)
} else if isJavaArrayType(property) {
propertyMap := property.(map[string]interface{})
nestedSchema := propertyMap["items"].(map[string]interface{})
if isJavaObjectType(nestedSchema) {
nestedTitle := nestedSchema["title"].(string)
nestedSchema = nestedSchema["properties"].(map[string]interface{})
nestedPropertyMap := nestedSchema
itemsSchema := &Schema{
Title: nestedTitle,
Properties: nestedPropertyMap,
}
processSchemaForJava(builder, itemsSchema, indent)
}
}
}
}
className := getFirstWordFromTitle(schema.Title)
builder.WriteString(indent + "class " + className + " {\n")
for _, name := range propertyNames {
property := schema.Properties[name]
if isJavaArrayType(property) {
itemType := getJavaArrayType(property)
builder.WriteString(indent + " " + "List<" + itemType + "> " + name + ";\n")
} else {
propertyType := getJavaType(property)
builder.WriteString(indent + " " + propertyType + " " + name + ";\n")
}
}
builder.WriteString(indent + "}\n\n")
}
}