UCAST 문법 (UCAST Syntax)
데이터 필터링 지원은 데이터를 필터링하는 데 적용할 수 있는 보편적인 조건 집합을 (JSON으로) 표현하기 위해 Universal Conditions AST (UCAST) 프로젝트를 활용해요.
UCAST는 트리에서 3가지 유형의 노드를 허용해요:
- 문서 수준 조건(Document-level Condition) 노드: 전체 문서/테이블에 연산자를 적용할 때 사용해요. 예를 들어 SQL의 EXISTS 연산자가 있어요. 이 유형의 노드는 어떤 인터프리터에서도 사용되지 않아요.
- 복합 조건(Compound Condition) 노드: N개의 자식 노드에 걸쳐 연산자를 적용할 때 사용해요.
- 필드 조건(Field Condition) 노드: 필드와 선택적인 값에 연산자를 적용할 때 사용해요.
출처: OPA 문서
본문
확장 문법 (Expanded Syntax)
START := EXPRSEXPRS := COMPOUND_EXPR | FIELD_EXPRCOMPOUND_EXPR := {"type": "compound", "operator": COMPOUND_OP_NAME, "value": [EXPRS]}COMPOUND_OP_NAME := "and" | "or" | "not"FIELD_EXPR := {"type": "field", "field": FIELD_NAME, "operator": FIELD_OP_NAME, "value": VALUE}FIELD_NAME := <string>VALUE := <string> | <number> | ... | {"field": COMPARED_FIELD_NAME}COMPARED_FIELD_NAME := <string>
FIELD_NAME은 데이터베이스에서 참조되는 필드에 해당해요. COMPARED_FIELD_NAME은 한 컬럼을 다른 컬럼과 비교할 때 사용돼요.
공식 JSON Schema
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "UCAST expanded syntax", "description": "UCAST expanded syntax that is compatible with EOPA supported integrations", "$defs": { "compoundExpression": { "type": "object", "properties": { "type": { "const": "compound" }, "operator": { "anyOf": [ { "const": "and" }, { "const": "or" }, { "const": "not" } ] }, "value": { "type": "array", "items": { "$ref": "#" } } }, "required": ["type", "operator", "value"] }, "fieldExpression": { "type": "object", "properties": { "type": { "const": "field" }, "field": { "type": "string" }, "operator": { "enum": ["eq", "ne", "lt", "lte", "gt", "gte", "in", "nin", "contains", "startswith", "endswith"] } }, "required": ["type", "field", "operator", "value"], "allOf": [ { "if": { "properties": { "operator": { "const": "eq" }} }, "then": { "properties": { "value": { "type": ["string", "number", "boolean", "null"] }}} }, { "if": { "properties": { "operator": { "const": "ne" }} }, "then": { "properties": { "value": { "type": ["string", "number", "boolean", "null"] }}} }, { "if": { "properties": { "operator": { "const": "lt" }} }, "then": { "properties": { "value": { "type": "number" }}} }, { "if": { "properties": { "operator": { "const": "lte" }} }, "then": { "properties": { "value": { "type": "number" }}} }, { "if": { "properties": { "operator": { "const": "gt" }} }, "then": { "properties": { "value": { "type": "number" }}} }, { "if": { "properties": { "operator": { "const": "gte" }} }, "then": { "properties": { "value": { "type": "number" }}} }, { "if": { "properties": { "operator": { "const": "in" }} }, "then": { "properties": { "value": { "type": ["array"] }}} }, { "if": { "properties": { "operator": { "const": "nin" }} }, "then": { "properties": { "value": { "type": ["array"] }}} }, { "if": { "properties": { "operator": { "const": "contains" }} }, "then": { "properties": { "value": { "type": ["string", "number", "boolean"] }}} }, { "if": { "properties": { "operator": { "const": "startswith" }} }, "then": { "properties": { "value": { "type": "string" }}} }, { "if": { "properties": { "operator": { "const": "endswith" }} }, "then": { "properties": { "value": { "type": "string" }}} } ] } }, "type": "object", "anyOf": [ { "$ref": "#/$defs/compoundExpression" }, { "$ref": "#/$defs/fieldExpression" } ]}
간결 문법 (Concise syntax)
UCAST는 노드에서 자주 중복되는 "type"과 "operation" 필드를 생략할 수 있는 축약된 "concise" 문법을 지원해요. 흔한 조건 모음을 더 쉽게 작성할 수 있도록 몇 가지 암시적 생성 규칙을 사용해요.
간결 형식에서 가정하는 두 가지는 다음과 같아요:
- 기본 복합 연산은 and예요.
- 기본 필드 연산은 eq예요.
START := IMPLICIT_ANDEXPRS := IMPLICIT_AND | COMPOUND_EXPR | FIELD_EXPRIMPLICIT_AND := '{' (FIELD_EXPR | COMPOUND_EXPR)... '}'FIELD_EXPR := (FIELD_NAME ':' VALUE) | (FIELD_NAME ':' '{' FIELD_OP_NAME ':' VALUE '}')FIELD_NAME := <string>VALUE := <string> | <number> | ... | {"field": COMPARED_FIELD_NAME}COMPARED_FIELD_NAME := <string>COMPOUND_EXPR := COMPOUND_OP_NAME ':' [EXPRS...]COMPOUND_OP_NAME := 'and' | 'or' | 'not'
FIELD_NAME은 데이터베이스에서 참조되는 필드에 해당해요.
복합 연산 (Compound Operations)
not 연산은 일반적으로 모든 클라이언트에서 지원되지는 않아요.
| 연산 | COMPOUND_OP_NAME |
지원 타입 | @open-policy-agent/ucast-prisma |
OpenPolicyAgent.Ucast.Linq |
|---|---|---|---|---|
| And | and |
array |
✅ | ✅ |
| Or | or |
array |
✅ | ✅ |
| Not | not |
항목 1개짜리 array |
✅ | ❌ |
필드 연산 (Field Operations)
모든 필드 연산이 모든 데이터베이스 통합에서 지원되는 건 아니에요. 다음은 EOPA 지원 UCAST 인터프리터에 대한 (전체적이지는 않은) 호환성 매트릭스예요.
| 연산 | FIELD_OP_NAME |
지원 VALUE 타입 |
@open-policy-agent/ucast-prisma |
OpenPolicyAgent.Ucast.Linq |
|---|---|---|---|---|
| Equals | eq |
string, number, boolean, null |
✅ | ✅ |
| Not Equals | ne |
string, number, boolean, null |
✅ | ✅ |
| Less Than | lt |
number |
✅ | ✅ |
| Less Than or Equals | lte |
number |
✅ | ✅ |
| Greater Than | gt |
number |
✅ | ✅ |
| Greater Than or Equals | gte |
number |
✅ | ✅ |
| In | in |
array |
✅ | ✅ |
| Not In | nin |
array |
✅ | ✅ |
| Contains | contains |
string, number, boolean |
✅ | ❌ |
| Starts With | startswith |
string |
✅ | ❌ |
| Ends With | endswith |
string |
✅ | ❌ |
예제 (Examples)
간단한 예: 상품 가격 (Simple: Product Prices)
유스 케이스: 가격이 $500 이하인 상품 보여주기
확장 형식(Expanded Format):
{ "type": "field", "field": "products.price", "operator": "lte", "value": 500}
간결 형식(Concise Format):
{ "products.price": { "lte": 500 }}
복합 예: 지원 티켓 (Compound: Support Tickets)
유스 케이스: "Alice Zimmerman"에게 배정되고 심각도(severity)가 1 또는 2인 티켓 보여주기.
확장 형식(Expanded Format):
{ "type": "compound", "operator": "and", "value": [ { "type": "field", "field": "tickets.assignee", "operator": "eq", "value": "Alice Zimmerman" }, { "type": "field", "field": "tickets.severity", "operator": "in", "value": [1, 2] } ]}
간결 형식(Concise Format):
{ "tickets.assignee": "Alice Zimmerman", "tickets.severity": { "in": [1, 2] }}
Not과 컬럼 비교: 지원 티켓 (Nots and Column Comparisons: Support Tickets)
유스 케이스: 담당자(assignee)가 해결자(resolver)가 아닌 티켓 보여주기.
확장 형식(Expanded Format):
{ "type": "compound", "operator": "not", "value": [ { "type": "field", "field": "tickets.assignee", "operator": "eq", "value": { "field": "tickets.resolver" } } ]}
간결 형식(Concise Format):
{ "not": [{ "tickets.assignee": { "field": "tickets.resolver" } }]}
더 알아보기 (Learn more)
- 간결 문법에서는 복합 연산의 기본값이
and, 필드 연산의 기본값이eq라는 점을 기억하세요. not복합 연산과contains,startswith,endswith필드 연산은 모든 인터프리터에서 지원되지 않으므로 호환성 매트릭스를 확인하세요.{"field": ...}값을 사용하면 한 컬럼을 다른 컬럼과 비교할 수 있어요.