Skip to content

Commit 0c30cc1

Browse files
authored
chore: simplify api generator (#223)
1 parent e80c239 commit 0c30cc1

File tree

7 files changed

+87
-194
lines changed

7 files changed

+87
-194
lines changed

playwright/src/main/java/com/microsoft/playwright/BrowserContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class AddCookie {
130130
/**
131131
* Unix time in seconds. Optional.
132132
*/
133-
public Long expires;
133+
public Double expires;
134134
/**
135135
* Optional.
136136
*/
@@ -164,7 +164,7 @@ public AddCookie withPath(String path) {
164164
this.path = path;
165165
return this;
166166
}
167-
public AddCookie withExpires(Long expires) {
167+
public AddCookie withExpires(double expires) {
168168
this.expires = expires;
169169
return this;
170170
}
@@ -189,7 +189,7 @@ class Cookie {
189189
/**
190190
* Unix time in seconds.
191191
*/
192-
private long expires;
192+
private double expires;
193193
private boolean httpOnly;
194194
private boolean secure;
195195
private SameSite sameSite;
@@ -206,7 +206,7 @@ public String domain() {
206206
public String path() {
207207
return this.path;
208208
}
209-
public long expires() {
209+
public double expires() {
210210
return this.expires;
211211
}
212212
public boolean httpOnly() {

playwright/src/main/java/com/microsoft/playwright/Keyboard.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@
3030
public interface Keyboard {
3131
enum Modifier { ALT, CONTROL, META, SHIFT }
3232

33+
class PressOptions {
34+
/**
35+
* Time to wait between {@code keydown} and {@code keyup} in milliseconds. Defaults to 0.
36+
*/
37+
public Double delay;
38+
39+
public PressOptions withDelay(double delay) {
40+
this.delay = delay;
41+
return this;
42+
}
43+
}
44+
class TypeOptions {
45+
/**
46+
* Time to wait between key presses in milliseconds. Defaults to 0.
47+
*/
48+
public Double delay;
49+
50+
public TypeOptions withDelay(double delay) {
51+
this.delay = delay;
52+
return this;
53+
}
54+
}
3355
/**
3456
* Dispatches a {@code keydown} event.
3557
*
@@ -68,7 +90,7 @@ enum Modifier { ALT, CONTROL, META, SHIFT }
6890
*/
6991
void insertText(String text);
7092
default void press(String key) {
71-
press(key, 0);
93+
press(key, null);
7294
}
7395
/**
7496
* {@code key} can specify the intended [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
@@ -92,9 +114,9 @@ default void press(String key) {
92114
*
93115
* @param key Name of the key to press or a character to generate, such as {@code ArrowLeft} or {@code a}.
94116
*/
95-
void press(String key, int delay);
117+
void press(String key, PressOptions delay);
96118
default void type(String text) {
97-
type(text, 0);
119+
type(text, null);
98120
}
99121
/**
100122
* Sends a {@code keydown}, {@code keypress}/{@code input}, and {@code keyup} event for each character in the text.
@@ -105,7 +127,7 @@ default void type(String text) {
105127
*
106128
* @param text A text to type into a focused element.
107129
*/
108-
void type(String text, int delay);
130+
void type(String text, TypeOptions delay);
109131
/**
110132
* Dispatches a {@code keyup} event.
111133
*

playwright/src/main/java/com/microsoft/playwright/Route.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class FulfillOptions {
8686
/**
8787
* Response status code, defaults to {@code 200}.
8888
*/
89-
public int status;
89+
public Integer status;
9090

9191
public FulfillOptions withBody(byte[] body) {
9292
this.bodyBytes = body;

playwright/src/main/java/com/microsoft/playwright/impl/KeyboardImpl.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.gson.JsonObject;
2020
import com.microsoft.playwright.Keyboard;
2121

22+
import static com.microsoft.playwright.impl.Serialization.gson;
23+
2224
class KeyboardImpl extends LoggingSupport implements Keyboard {
2325
private final ChannelOwner page;
2426

@@ -45,27 +47,31 @@ public void insertText(String text) {
4547
}
4648

4749
@Override
48-
public void press(String key, int delay) {
49-
withLogging("Keyboard.press", () -> {
50-
JsonObject params = new JsonObject();
51-
params.addProperty("key", key);
52-
if (delay != 0) {
53-
params.addProperty("delay", delay);
54-
}
55-
page.sendMessage("keyboardPress", params);
56-
});
50+
public void press(String key, PressOptions options) {
51+
withLogging("Keyboard.press", () -> pressImpl(key, options));
5752
}
5853

59-
@Override
60-
public void type(String text, int delay) {
61-
withLogging("Keyboard.type", () -> {
62-
JsonObject params = new JsonObject();
63-
params.addProperty("text", text);
64-
if (delay != 0) {
65-
params.addProperty("delay", delay);
66-
}
67-
page.sendMessage("keyboardType", params);
68-
});
54+
private void pressImpl(String key, PressOptions options) {
55+
if (options == null) {
56+
options = new PressOptions();
57+
}
58+
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
59+
params.addProperty("key", key);
60+
page.sendMessage("keyboardPress", params);
61+
}
62+
63+
@Override
64+
public void type(String text, TypeOptions options) {
65+
withLogging("Keyboard.type", () -> typeImpl(text, options));
66+
}
67+
68+
private void typeImpl(String text, TypeOptions options) {
69+
if (options == null) {
70+
options = new TypeOptions();
71+
}
72+
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
73+
params.addProperty("text", text);
74+
page.sendMessage("keyboardType", params);
6975
}
7076

7177
@Override

playwright/src/main/java/com/microsoft/playwright/impl/RouteImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private void fulfillImpl(FulfillOptions options) {
7979
options = new FulfillOptions();
8080
}
8181

82-
int status = options.status == 0 ? 200 : options.status;
82+
int status = options.status == null ? 200 : options.status;
8383
String body = "";
8484
boolean isBase64 = false;
8585
int length = 0;

tools/api-generator/src/main/java/com/microsoft/playwright/tools/ApiGenerator.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,25 @@ String toJava() {
238238
return "Boolean";
239239
}
240240
}
241-
if (jsonName.replace("null|", "").contains("|")) {
242-
throw new RuntimeException("Missing mapping for type union: " + jsonPath + ": " + jsonName);
243-
}
244-
// System.out.println(jsonPath + " : " + jsonName);
245-
// if (jsonName.equals("Promise")) {
246-
// System.out.println(jsonElement);
247-
// }
248-
return convertBuiltinType(jsonElement.getAsJsonObject());
241+
return convertBuiltinType(stripNullable());
242+
}
243+
244+
private JsonObject stripNullable() {
245+
JsonObject jsonType = jsonElement.getAsJsonObject();
246+
if (!"union".equals(jsonType.get("name").getAsString())) {
247+
return jsonType;
248+
}
249+
JsonArray values = jsonType.getAsJsonArray("union");
250+
if (values.size() != 2) {
251+
throw new RuntimeException("Unexpected union without custom mapping " + jsonPath + ": " + jsonType);
252+
}
253+
for (JsonElement item : values) {
254+
JsonObject o = item.getAsJsonObject();
255+
if (!"null".equals(o.get("name").getAsString())) {
256+
return o;
257+
}
258+
}
259+
throw new RuntimeException("Unexpected union " + jsonPath + ": " + jsonType);
249260
}
250261

251262
private static String convertBuiltinType(JsonObject jsonType) {
@@ -265,6 +276,15 @@ private static String convertBuiltinType(JsonObject jsonType) {
265276
if ("path".equals(name)) {
266277
return "Path";
267278
}
279+
if ("EvaluationArgument".equals(name)) {
280+
return "Object";
281+
}
282+
if ("Serializable".equals(name)) {
283+
return "Object";
284+
}
285+
if ("Buffer".equals(name)) {
286+
return "byte[]";
287+
}
268288
if ("Array".equals(name)) {
269289
return "List<" + convertTemplateParams(jsonType) + ">";
270290
}

0 commit comments

Comments
 (0)