Skip to content

Commit

Permalink
Added reference field name and field assert.
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed May 3, 2018
1 parent 5a5566b commit 525bb68
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 50 deletions.
28 changes: 28 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@
}
```

- Added reference field name by syntax `$.valueof:field`, e.g.

```json
{
"method": ["=email", "=password"],
"$.valueof:method": "string"
}
```

will `$.valueof:method` means using value of field `method` as the name of
a field. Thus it matches

```json
{
"method": "email",
"email": "aaa@sample.com"
}
```

and

```json
{
"method": "password",
"password": "xxxxxxxxx"
}
```

- Added advanced type `$.dict` to limit keys of `$.map`:

```json
Expand Down
36 changes: 36 additions & 0 deletions docs/zh-CN/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ interface Map<T> {

## 进阶使用

### 混合使用

`$.map``$.dict``$.array` 修饰符后面可以有多个元素,例如:

```json
Expand Down Expand Up @@ -620,3 +622,37 @@ interface Map<T> {
```

进一步简化为:`"string[]{}"`

### 对象的属性名引用

在对象中,可以通过 `$.valueof` 语法将一个属性的值作为另一个属性的名称,例如:

```json
{
"a": ["=hello", "=world"],
"$.valueof:a": "boolean"
}
```

表示第二个属性的名称必须是属性 a 的值,也就是说,如果 a == "hello",则必须有一个名为
"hello" 的布尔型属性,如果 a == "world",则必须有一个名为 "world" 的布尔型属性。

### 对象的属性存在性断言

在对象中,可以通过 `$.valueof` 语法,将一个属性的值作为另一个属性的名称,并断言该属性
必须存在(不为 `undefined`):

```json
{
"a": ["=hello", "=world"],
"$.valueof:a": "exists",
"$.virtual:hello": "string",
"$.virtual:world": "uint32"
}
```

此处将 `$.valueof:a` 的值设置为 `exists`,表示属性不能为 `undefined`
因此 `hello` 或者 `world` 中必须有一个存在。

> `hello``world` 设置为 `virtual` 属性,这样表示他们被用于存在性断言。
> `virtual` 属性默认是可选的。
36 changes: 19 additions & 17 deletions sources/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export interface CompileResult {

export const BuiltInTypes = {
"void": "void",
"undefined": "undefined",
"optional": "optional",
"exists": "exists",
"array": "array",
"any": "any",
"int": "int",
Expand All @@ -65,8 +67,7 @@ export const BuiltInTypes = {
"false": "false",
"null": "null",
"float": "float",
"numeric": "numeric",
"undefined": "undefined"
"numeric": "numeric"
};

function freezeObject(obj: any): void {
Expand Down Expand Up @@ -102,17 +103,19 @@ export const FILTER_ON = {
VALUE: "value"
};

export const ADV_TYPE_REL_PREFIX = "$.";
export const ADV_TYPE_PREFIX = "$.";

export const AdvancedTypes = {

$AND: `${ADV_TYPE_REL_PREFIX}and`,
$OR: `${ADV_TYPE_REL_PREFIX}or`,
$TUPLE: `${ADV_TYPE_REL_PREFIX}tuple`,
$ARRAY: `${ADV_TYPE_REL_PREFIX}array`,
$MAP: `${ADV_TYPE_REL_PREFIX}map`,
$DICT: `${ADV_TYPE_REL_PREFIX}dict`,
$STRUCT: `${ADV_TYPE_REL_PREFIX}struct`
$AND: `${ADV_TYPE_PREFIX}and`,
$OR: `${ADV_TYPE_PREFIX}or`,
$TUPLE: `${ADV_TYPE_PREFIX}tuple`,
$ARRAY: `${ADV_TYPE_PREFIX}array`,
$MAP: `${ADV_TYPE_PREFIX}map`,
$DICT: `${ADV_TYPE_PREFIX}dict`,
$STRUCT: `${ADV_TYPE_PREFIX}struct`,
$VALUEOF: `${ADV_TYPE_PREFIX}valueof:`,
$VIRTUAL: `${ADV_TYPE_PREFIX}virtual:`
};

export const IMPLICIT_SYMBOL = "?";
Expand Down Expand Up @@ -189,14 +192,13 @@ export interface Language {
getConstantDefinition(constName: string, val: string): string;

/**
* Check if the elements in array variable are insides the array.
*
* @param arrVarName The name of array vairable.
* @param arr The array.
* Check if the keys of object are equal.
*/
getStringArrayContainsCondition(
arrVarName: string,
arr: string[]
getCheckKeysEqualCondition(
objVar: string,
objKeysVar: string,
literalKeys: string[],
referKeys: string[]
): string;

/**
Expand Down
Loading

0 comments on commit 525bb68

Please sign in to comment.