killme2008 / killme2008/aviatorscript

多级字段取值自动判空能力

Open
#561 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.2k
Forks
906
PR merge metrics
No merged PRs in 30d

Description

### 背景
在表达式使用的过程中,我们经常会通过表达式从上下文中进行取值。例如:
```java
JSONObject context = new JSONObject();
JSONObject result = new JSONObject();
Map featureMap = Maps.newHashMap();
featureMap.put("f1", "1L");
result.put("featureMap", featureMap);
context.put("result", result);
```
有一个context作为上下文,需要取其featureMap的f1字段值,我们的表达式会写成
```shell
## $=context

$.result.featureMap.f1
```

### 存在的问题

但是,这种写法需要使用方自己保证 $、result、featureMap都必须为非空的字段,否则aviator会在执行过程中抛出异常;而在使用过程中我们通常是无法确保的,所以需要进行判空操作。此时,表达式就需要写成如下形式:
```shell
if($!=nil) {
if($.result!=nil) {
if($.result.featureMap!=nil) {
return $.result.featureMap.f1;
}
}
}
```

### 需求期望

这样的写法虽然能够保障表达式的正常执行,但是在多级字段的取值使用中对用户是不太友好的,我们希望能够可以通过类似mvel的方法直接进行判空,如下:
```shell
$.?result.?featureMap.?f1
```
通过.?的语法对其左值进行判空,当其左值为空时直接返回空

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue names no files or tests. Start by locating the expression parser and evaluator entry points for nested field access, then trace how null values currently produce exceptions. Done means the proposed `.?` syntax is supported at each level and returns null when its left value is null, with coverage for the nested examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.