-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGoGenerator.java
More file actions
80 lines (68 loc) · 2.56 KB
/
Copy pathGoGenerator.java
File metadata and controls
80 lines (68 loc) · 2.56 KB
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
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.languages.GoClientCodegen;
import shared.PostProcessFileReplace;
import java.io.File;
import java.util.Set;
public class GoGenerator extends GoClientCodegen {
private static RegionFix regionFix = new RegionFix();
private static PostProcessFileReplace postProcessFileReplace = new PostProcessFileReplace("overrides/go");
@Override
public String getName() {
return "GoClientCodegen";
}
public GoGenerator(){
super();
System.out.println("=== Custom Go Generator initialized ===");
}
@Override
public CodegenProperty fromProperty(String name, Schema p, boolean required) {
return regionFix.fromProperty(super.fromProperty(name, p, required));
}
@Override
public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) {
return regionFix.fromParameter(super.fromParameter(parameter, imports));
}
@Override
public void postProcessFile(File file, String fileType) {
postProcessFileReplace.process(file);
super.postProcessFile(file, fileType);
}
}
class RegionFix {
CodegenProperty fromProperty(CodegenProperty property) {
if (isRegionField(property.name)) {
property.dataType = "string";
property.datatypeWithEnum = "string";
property.baseType = "string";
// Force template engine to treat this as a string
property.isString = true;
property.isInteger = false;
property.isLong = false;
property.isNumber = false;
property.isNumeric = false;
}
return property;
}
CodegenParameter fromParameter(CodegenParameter parameter) {
if (isRegionField(parameter.paramName)) {
parameter.dataType = "string";
// Force template engine to treat this as a string
parameter.isString = true;
parameter.isInteger = false;
parameter.isLong = false;
parameter.isNumber = false;
// If it was previously an enum or another complex type, clear it
parameter.isEnum = false;
}
return parameter;
}
private static boolean isRegionField(String name) {
if (name == null) {
return false;
}
return name.equalsIgnoreCase("region") || name.equalsIgnoreCase("regionId");
}
}