Skip to content

Commit 7e9dc90

Browse files
committed
[server] Add more methods to ContextValue
1 parent cea49f3 commit 7e9dc90

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

docs/src/modules/server.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,23 @@ types:
161161
- name: html
162162
args: 'html'
163163
desc: sets result to the specified html string. Also sets Content-Type header to text/html
164-
desc_ru: устанавливает указанныую html-строку в качестве результата. Также устанавливает заголовок Content-Type в text/html
164+
desc_ru: устанавливает указанную html-строку в качестве результата. Также устанавливает заголовок Content-Type в text/html
165165
- name: ip
166166
args: ''
167167
desc: returns an IP address
168168
desc_ru: возвращает IP адрес
169+
- name: isHttpMethod
170+
args: ''
171+
desc: returns true if the request is http method
172+
desc_ru: возвращает true, если запрос — http метод
173+
- name: isMultipartFormData
174+
args: ''
175+
desc: returns true if the request is multipart/formdata
176+
desc_ru: возвращает true, если запрос — multipart/formdata
177+
- name: isMultipart
178+
args: ''
179+
desc: returns true if the request is multipart
180+
desc_ru: возвращает true, если запрос — multipart
169181
- name: json
170182
args: 'obj'
171183
desc: serializes an object to json string and sets it as the result
@@ -178,10 +190,18 @@ types:
178190
args: ''
179191
desc: returns a matched request path
180192
desc_ru: возвращает совпавший путь запроса
193+
- name: method
194+
args: ''
195+
desc: returns a method (GET, POST, ...)
196+
desc_ru: возвращает метод (GET, POST, ...)
181197
- name: path
182198
args: ''
183199
desc: returns a request path
184200
desc_ru: возвращает путь запроса
201+
- name: pathParam
202+
args: 'key'
203+
desc: returns a request path parameter
204+
desc_ru: возвращает параметр пути запроса
185205
- name: port
186206
args: ''
187207
desc: returns a port number
@@ -190,6 +210,10 @@ types:
190210
args: ''
191211
desc: returns a protocol
192212
desc_ru: возвращает протокол
213+
- name: queryParam
214+
args: 'key'
215+
desc: returns a query parameter
216+
desc_ru: возвращает параметр запроса
193217
- name: queryString
194218
args: ''
195219
desc: returns a query string
@@ -210,6 +234,10 @@ types:
210234
args: 'value = ""'
211235
desc: gets or sets a result. `value` can be a string or a byte array
212236
desc_ru: получает или устанавливает результат. `value` может быть строкой или массивом байт
237+
- name: status
238+
args: 'status = ...'
239+
desc: gets or sets a status code. `status` can be an integer status code (404, 500) or a string status name ("NOT_FOUND", "INTERNAL_SERVER_ERROR").
240+
desc_ru: получает или устанавливает код статуса. `status` может быть числовым кодом (404, 500) или строкой имени статуса ("NOT_FOUND", "INTERNAL_SERVER_ERROR")
213241
- name: statusCode
214242
args: ''
215243
desc: returns a response status code

modules/server/src/main/java/com/annimon/ownlang/modules/server/ContextValue.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,24 @@ private void init() {
3535
set("host", Converters.voidToString(ctx::host));
3636
set("html", stringToContext(ctx::html));
3737
set("ip", Converters.voidToString(ctx::ip));
38+
set("isHttpMethod", Converters.voidToBoolean(() -> ctx.method().isHttpMethod()));
39+
set("isMultipart", Converters.voidToBoolean(ctx::isMultipart));
40+
set("isMultipartFormData", Converters.voidToBoolean(ctx::isMultipartFormData));
3841
set("json", objectToContext(ctx::json));
3942
set("jsonStream", objectToContext(ctx::jsonStream));
4043
set("matchedPath", Converters.voidToString(ctx::matchedPath));
44+
set("method", Converters.voidToString(() -> ctx.method().name()));
4145
set("path", Converters.voidToString(ctx::path));
46+
set("pathParam", Converters.stringToString(ctx::pathParam));
4247
set("port", Converters.voidToInt(ctx::port));
4348
set("protocol", Converters.voidToString(ctx::protocol));
49+
set("queryParam", Converters.stringToString(ctx::queryParam));
4450
set("queryString", Converters.voidToString(ctx::queryString));
4551
set("redirect", this::redirect);
4652
set("removeCookie", this::removeCookie);
4753
set("render", this::render);
4854
set("result", this::result);
55+
set("status", this::status);
4956
set("statusCode", Converters.voidToInt(ctx::statusCode));
5057
set("scheme", Converters.voidToString(ctx::scheme));
5158
set("url", Converters.voidToString(ctx::url));
@@ -151,6 +158,21 @@ private Value result(Value[] args) {
151158
}
152159
}
153160

161+
private Value status(Value[] args) {
162+
Arguments.checkOrOr(0, 1, args.length);
163+
if (args.length == 0) {
164+
return new StringValue(ctx.status().name());
165+
} else {
166+
final var arg = args[0];
167+
if (arg.type() == Types.NUMBER) {
168+
ctx.status(arg.asInt());
169+
} else {
170+
ctx.status(HttpStatus.valueOf(arg.asString()));
171+
}
172+
return this;
173+
}
174+
}
175+
154176
private Value stringToContext(Consumer<String> consumer) {
155177
return new FunctionValue(args -> {
156178
Arguments.check(1, args.length);

ownlang-core/src/main/java/com/annimon/ownlang/lib/MapValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public Object raw() {
104104

105105
@Override
106106
public Object asJavaObject() {
107-
Map<Object, Object> result = new HashMap<>(map.size());
107+
Map<Object, Object> result = new LinkedHashMap<>(map.size());
108108
map.forEach((k, v) -> result.put(k.asJavaObject(), v.asJavaObject()));
109109
return result;
110110
}

0 commit comments

Comments
 (0)