mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
[server] Add more methods to ContextValue
This commit is contained in:
parent
cea49f3c9d
commit
7e9dc9038d
@ -161,11 +161,23 @@ types:
|
|||||||
- name: html
|
- name: html
|
||||||
args: 'html'
|
args: 'html'
|
||||||
desc: sets result to the specified html string. Also sets Content-Type header to text/html
|
desc: sets result to the specified html string. Also sets Content-Type header to text/html
|
||||||
desc_ru: устанавливает указанныую html-строку в качестве результата. Также устанавливает заголовок Content-Type в text/html
|
desc_ru: устанавливает указанную html-строку в качестве результата. Также устанавливает заголовок Content-Type в text/html
|
||||||
- name: ip
|
- name: ip
|
||||||
args: ''
|
args: ''
|
||||||
desc: returns an IP address
|
desc: returns an IP address
|
||||||
desc_ru: возвращает IP адрес
|
desc_ru: возвращает IP адрес
|
||||||
|
- name: isHttpMethod
|
||||||
|
args: ''
|
||||||
|
desc: returns true if the request is http method
|
||||||
|
desc_ru: возвращает true, если запрос — http метод
|
||||||
|
- name: isMultipartFormData
|
||||||
|
args: ''
|
||||||
|
desc: returns true if the request is multipart/formdata
|
||||||
|
desc_ru: возвращает true, если запрос — multipart/formdata
|
||||||
|
- name: isMultipart
|
||||||
|
args: ''
|
||||||
|
desc: returns true if the request is multipart
|
||||||
|
desc_ru: возвращает true, если запрос — multipart
|
||||||
- name: json
|
- name: json
|
||||||
args: 'obj'
|
args: 'obj'
|
||||||
desc: serializes an object to json string and sets it as the result
|
desc: serializes an object to json string and sets it as the result
|
||||||
@ -178,10 +190,18 @@ types:
|
|||||||
args: ''
|
args: ''
|
||||||
desc: returns a matched request path
|
desc: returns a matched request path
|
||||||
desc_ru: возвращает совпавший путь запроса
|
desc_ru: возвращает совпавший путь запроса
|
||||||
|
- name: method
|
||||||
|
args: ''
|
||||||
|
desc: returns a method (GET, POST, ...)
|
||||||
|
desc_ru: возвращает метод (GET, POST, ...)
|
||||||
- name: path
|
- name: path
|
||||||
args: ''
|
args: ''
|
||||||
desc: returns a request path
|
desc: returns a request path
|
||||||
desc_ru: возвращает путь запроса
|
desc_ru: возвращает путь запроса
|
||||||
|
- name: pathParam
|
||||||
|
args: 'key'
|
||||||
|
desc: returns a request path parameter
|
||||||
|
desc_ru: возвращает параметр пути запроса
|
||||||
- name: port
|
- name: port
|
||||||
args: ''
|
args: ''
|
||||||
desc: returns a port number
|
desc: returns a port number
|
||||||
@ -190,6 +210,10 @@ types:
|
|||||||
args: ''
|
args: ''
|
||||||
desc: returns a protocol
|
desc: returns a protocol
|
||||||
desc_ru: возвращает протокол
|
desc_ru: возвращает протокол
|
||||||
|
- name: queryParam
|
||||||
|
args: 'key'
|
||||||
|
desc: returns a query parameter
|
||||||
|
desc_ru: возвращает параметр запроса
|
||||||
- name: queryString
|
- name: queryString
|
||||||
args: ''
|
args: ''
|
||||||
desc: returns a query string
|
desc: returns a query string
|
||||||
@ -210,6 +234,10 @@ types:
|
|||||||
args: 'value = ""'
|
args: 'value = ""'
|
||||||
desc: gets or sets a result. `value` can be a string or a byte array
|
desc: gets or sets a result. `value` can be a string or a byte array
|
||||||
desc_ru: получает или устанавливает результат. `value` может быть строкой или массивом байт
|
desc_ru: получает или устанавливает результат. `value` может быть строкой или массивом байт
|
||||||
|
- name: status
|
||||||
|
args: 'status = ...'
|
||||||
|
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").
|
||||||
|
desc_ru: получает или устанавливает код статуса. `status` может быть числовым кодом (404, 500) или строкой имени статуса ("NOT_FOUND", "INTERNAL_SERVER_ERROR")
|
||||||
- name: statusCode
|
- name: statusCode
|
||||||
args: ''
|
args: ''
|
||||||
desc: returns a response status code
|
desc: returns a response status code
|
||||||
|
@ -35,17 +35,24 @@ class ContextValue extends MapValue {
|
|||||||
set("host", Converters.voidToString(ctx::host));
|
set("host", Converters.voidToString(ctx::host));
|
||||||
set("html", stringToContext(ctx::html));
|
set("html", stringToContext(ctx::html));
|
||||||
set("ip", Converters.voidToString(ctx::ip));
|
set("ip", Converters.voidToString(ctx::ip));
|
||||||
|
set("isHttpMethod", Converters.voidToBoolean(() -> ctx.method().isHttpMethod()));
|
||||||
|
set("isMultipart", Converters.voidToBoolean(ctx::isMultipart));
|
||||||
|
set("isMultipartFormData", Converters.voidToBoolean(ctx::isMultipartFormData));
|
||||||
set("json", objectToContext(ctx::json));
|
set("json", objectToContext(ctx::json));
|
||||||
set("jsonStream", objectToContext(ctx::jsonStream));
|
set("jsonStream", objectToContext(ctx::jsonStream));
|
||||||
set("matchedPath", Converters.voidToString(ctx::matchedPath));
|
set("matchedPath", Converters.voidToString(ctx::matchedPath));
|
||||||
|
set("method", Converters.voidToString(() -> ctx.method().name()));
|
||||||
set("path", Converters.voidToString(ctx::path));
|
set("path", Converters.voidToString(ctx::path));
|
||||||
|
set("pathParam", Converters.stringToString(ctx::pathParam));
|
||||||
set("port", Converters.voidToInt(ctx::port));
|
set("port", Converters.voidToInt(ctx::port));
|
||||||
set("protocol", Converters.voidToString(ctx::protocol));
|
set("protocol", Converters.voidToString(ctx::protocol));
|
||||||
|
set("queryParam", Converters.stringToString(ctx::queryParam));
|
||||||
set("queryString", Converters.voidToString(ctx::queryString));
|
set("queryString", Converters.voidToString(ctx::queryString));
|
||||||
set("redirect", this::redirect);
|
set("redirect", this::redirect);
|
||||||
set("removeCookie", this::removeCookie);
|
set("removeCookie", this::removeCookie);
|
||||||
set("render", this::render);
|
set("render", this::render);
|
||||||
set("result", this::result);
|
set("result", this::result);
|
||||||
|
set("status", this::status);
|
||||||
set("statusCode", Converters.voidToInt(ctx::statusCode));
|
set("statusCode", Converters.voidToInt(ctx::statusCode));
|
||||||
set("scheme", Converters.voidToString(ctx::scheme));
|
set("scheme", Converters.voidToString(ctx::scheme));
|
||||||
set("url", Converters.voidToString(ctx::url));
|
set("url", Converters.voidToString(ctx::url));
|
||||||
@ -151,6 +158,21 @@ class ContextValue extends MapValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Value status(Value[] args) {
|
||||||
|
Arguments.checkOrOr(0, 1, args.length);
|
||||||
|
if (args.length == 0) {
|
||||||
|
return new StringValue(ctx.status().name());
|
||||||
|
} else {
|
||||||
|
final var arg = args[0];
|
||||||
|
if (arg.type() == Types.NUMBER) {
|
||||||
|
ctx.status(arg.asInt());
|
||||||
|
} else {
|
||||||
|
ctx.status(HttpStatus.valueOf(arg.asString()));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Value stringToContext(Consumer<String> consumer) {
|
private Value stringToContext(Consumer<String> consumer) {
|
||||||
return new FunctionValue(args -> {
|
return new FunctionValue(args -> {
|
||||||
Arguments.check(1, args.length);
|
Arguments.check(1, args.length);
|
||||||
|
@ -104,7 +104,7 @@ public class MapValue implements Value, Iterable<Map.Entry<Value, Value>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object asJavaObject() {
|
public Object asJavaObject() {
|
||||||
Map<Object, Object> result = new HashMap<>(map.size());
|
Map<Object, Object> result = new LinkedHashMap<>(map.size());
|
||||||
map.forEach((k, v) -> result.put(k.asJavaObject(), v.asJavaObject()));
|
map.forEach((k, v) -> result.put(k.asJavaObject(), v.asJavaObject()));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user