-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathUndo.java
More file actions
120 lines (106 loc) · 4.15 KB
/
Undo.java
File metadata and controls
120 lines (106 loc) · 4.15 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
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 com.datadog.api;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/** Describes undo action. */
public class Undo {
/** Holds information about undo operation. */
public static class UndoMethod {
/** Defines how to populate operation parameters. */
public static class Parameter {
public String name;
public String source;
public String template;
public String origin;
}
public String tag;
public String type;
public String operationId;
public List<Parameter> parameters;
public Map<String, Object> getRequestParameters(
Object responseData,
Object requestData,
Method requestBuilder,
ObjectMapper mapper,
Map<String, Object> pathParameters) {
Map<String, Object> requestParams = new HashMap<String, Object>();
for (int i = 0; i < parameters.size(); i++) {
Undo.UndoMethod.Parameter p = parameters.get(i);
Object data;
if (p.origin != null && p.origin.equals("path")) {
data = pathParameters;
} else if (p.origin == null) {
data = responseData;
} else if (p.origin.equals("request")) {
data = requestData;
} else {
data = responseData;
}
try {
if (p.source != null) {
if (p.origin != null && p.origin.equals("path")) {
// For path parameters, try both original parameter name and property name
String propertyName = World.toPropertyName(p.name);
if (pathParameters.containsKey(p.source)) {
requestParams.put(propertyName, pathParameters.get(p.source));
} else if (pathParameters.containsKey(World.toPropertyName(p.source))) {
requestParams.put(propertyName, pathParameters.get(World.toPropertyName(p.source)));
} else {
throw new RuntimeException("Path parameter '" + p.source + "' not found");
}
} else {
requestParams.put(World.toPropertyName(p.name), World.lookup(data, p.source));
}
} else if (p.template != null) {
Class<?>[] types = requestBuilder.getParameterTypes();
Object param = World.fromJSON(mapper, types[i], World.templated(p.template, data));
requestParams.put(World.toPropertyName(p.name), param);
} else if (p.origin != null && p.origin.equals("path")) {
throw new RuntimeException("Path origin requires 'source' field");
}
} catch (java.lang.IllegalAccessException e) {
throw new RuntimeException(e);
} catch (java.lang.NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
throw new RuntimeException(e);
}
}
return requestParams;
}
}
public String tag;
public UndoMethod undo;
/** Load requests undo definition from a configuration file. */
public static Map<String, Undo> loadRequestsUndo(File file) throws IOException {
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, Undo>> typeRef = new TypeReference<HashMap<String, Undo>>() {};
Map<String, Undo> result = mapper.readValue(file, typeRef);
Map<String, Undo> clean = new HashMap<String, Undo>();
for (String k : result.keySet()) {
clean.put(toOperationName(k), result.get(k));
}
return clean;
}
public static String toOperationName(String string) {
if (string == null || string.length() == 0) {
return string;
}
char c[] = string.toCharArray();
c[0] = Character.toLowerCase(c[0]);
return new String(c);
}
public String getAPIName() {
if (undo.tag != null) return Pattern.compile("[- ]").matcher(undo.tag).replaceAll("");
return Pattern.compile("[- ]").matcher(tag).replaceAll("");
}
public String getOperationName() {
return toOperationName(undo.operationId);
}
}