

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 표현식 사용
<a name="ddb-mapper-expressions"></a>

****  
**DynamoDB Mapper는 개발자 미리 보기 릴리스입니다. 기능이 완전하지 않으며 변경될 수 있습니다.**

특정 DynamoDB 작업은 제약 조건 지정에 사용할 수 있는 [표현식을](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.html) 허용합니다. DynamoDB Mapper는 표현식을 생성하기 위해 관용적인 Kotlin DSL을 제공합니다. DSL은 코드의 구조와 가독성을 높이고 표현식을 더 쉽게 작성할 수 있습니다.

이 섹션에서는 DSL 구문을 설명하고 다양한 예제를 제공합니다.

## 작업에 표현식 사용
<a name="ddb-mapper-expressions-basic-usage"></a>

와 같은 작업에서 표현식을 사용합니다. `scan`여기서 표현식은 정의한 기준에 따라 반환된 항목을 필터링합니다. DynamoDB Mapper에서 표현식을 사용하려면 작업 요청에 표현식 구성 요소를 추가합니다.

다음 코드 조각은 `scan` 작업에 사용되는 필터 표현식의 예를 보여줍니다. Lambda 인수를 사용하여 반환할 항목을 `year` 속성 값이 2001인 항목으로 제한하는 필터 기준을 설명합니다.

```
val table = // A table instance.

table.scanPaginated {
    filter {
        attr("year") eq 2001
    }
}
```

다음 예제는 정렬 키 필터링과 비키 필터링이라는 두 위치에서 표현식을 지원하는 `query` 작업을 보여줍니다.

```
table.queryPaginated {
    keyCondition = KeyFilter(partitionKey = 1000) { sortKey startsWith "M" }
    filter {
        attr("year") eq 2001
    }
}
```

이전 코드는 세 가지 기준을 모두 충족하는 결과를 필터링합니다.
+ 파티션 키 속성 값은 1000 *-AND-*입니다.
+ 정렬 키 속성 값은 문자 *M* *-AND-*로 시작합니다.
+ year 속성 값은 2001입니다.

## DSL 구성 요소
<a name="ddb-mapper-expressions-dsl"></a>

DSL 구문은 표현식을 빌드하는 데 사용하는 아래에 설명된 여러 유형의 구성 요소를 노출합니다.

### 속성
<a name="ddb-mapper-expressions-dsl-attrs"></a>

대부분의 조건은 키 또는 문서 경로로 식별되는 속성을 참조합니다. DSK를 사용하면 `attr` 함수를 사용하여 모든 속성 참조를 생성하고 선택적으로 추가 수정을 수행할 수 있습니다.

다음 코드는 인덱스별 목록 디 참조 및 키별 맵 디 참조와 같이 간단한 속성 참조부터 복잡한 속성 참조까지 다양한 예제를 보여줍니다.

```
attr("foo")           // Refers to the value of top-level attribute `foo`.

attr("foo")[3]        // Refers to the value at index 3 in the list value of
                      // attribute `foo`.

attr("foo")[3]["bar"] // Refers to the value of key `bar` in the map value at
                      // index 3 of the list value of attribute `foo`.
```

### 같음 및 비형평성
<a name="ddb-mapper-expressions-dsl-eq-and-ineq"></a>

등식과 불평등을 기준으로 표현식의 속성 값을 비교할 수 있습니다. 속성 값을 리터럴 값 또는 기타 속성 값과 비교할 수 있습니다. 조건을 지정하는 데 사용하는 함수는 다음과 같습니다.
+ `eq`:는와 같습니다(에 해당`==`).
+ `neq`:가와 같지 않음(에 해당`!=`)
+ `gt`: 보다 큼(에 해당`>`)
+ `gte`:가 보다 크거나 같음(에 해당`>=`)
+ `lt`: 보다 작음(에 해당`<`)
+ `lte`:이 보다 작거나 같음(에 해당`<=`)

다음 예제와 같이 인픽스 표기법을 사용하여 비교 함수를 인수와 결합합니다.

```
attr("foo") eq 42           // Uses a literal. Specifies that the attribute value `foo` must be
                            // equal to 42.

attr("bar") gte attr("baz") // Uses another attribute value. Specifies that the attribute 
                            // value `bar` must be greater than or equal to the
                            // attribute value of `baz`.
```

### 범위 및 세트
<a name="ddb-mapper-expressions-dsl-ranges-sets"></a>

단일 값 외에도 속성 값을 범위 또는 집합의 여러 값과 비교할 수 있습니다. 다음 예제와 같이 infix `[isIn](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/is-in.html)` 함수를 사용하여 비교를 수행합니다.

```
attr("foo") isIn 0..99  // Specifies that the attribute value `foo` must be
                        // in the range of `0` to `99` (inclusive).

attr("foo") isIn setOf( // Specifies that the attribute value `foo` must be
    "apple",            // one of `apple`, `banana`, or `cherry`.
    "banana",
    "cherry",
)
```

`isIn` 함수는 컬렉션(예: `Set<String>`) 및 Kotlin으로 표현할 수 있는 경계`[ClosedRange<T>](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/-closed-range/)`(예: )에 대한 오버로드를 제공합니다`[IntRange](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/-int-range/)`. 로 표현할 수 없는 경계`ClosedRange<T>`(예: 바이트 배열 또는 기타 속성 참조)의 경우 `[isBetween](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/is-between.html)` 함수를 사용할 수 있습니다.

```
val lowerBytes = byteArrayOf(0x48, 0x65, 0x6c)  // Specifies that the attribute value
val upperBytes = byteArrayOf(0x6c, 0x6f, 0x21)  // `foo` is between the values
attr("foo").isBetween(lowerBytes, upperBytes)   // `0x48656c` and `0x6c6f21`

attr("foo").isBetween(attr("bar"), attr("baz")) // Specifies that the attribute value
                                                // `foo` is between the values of
                                                // attributes `bar` and `baz`.
```

### 부울 로직
<a name="ddb-mapper-expressions-dsl-boolean"></a>

다음 함수를 사용하여 부울 로직을 사용하여 개별 조건을 결합하거나 변경할 수 있습니다.
+ `and`: 모든 조건이 true(같음` &&`)여야 합니다.
+ `or`: 하나 이상의 조건이 true여야 합니다(에 해당`||`).
+ `not`: 지정된 조건이 false여야 합니다(에 해당`!`).

다음 예제에서는 각 함수를 보여줍니다.

```
and(                           // Both conditions must be met:
    attr("foo") eq "banana",   // * attribute value `foo` must equal `banana`
    attr("bar") isIn 0..99,    // * attribute value `bar` must be between
)                              //   0 and 99 (inclusive)

or(                            // At least one condition must be met:
    attr("foo") eq "cherry",   // * attribute value `foo` must equal `cherry`
    attr("bar") isIn 100..199, // * attribute value `bar` must be between
)                              //   100 and 199 (inclusive)

not(                           // The attribute value `foo` must *not* be
    attr("baz") isIn setOf(    // one of `apple`, `banana`, or `cherry`.
        "apple",               // Stated another way, the attribute value
        "banana",              // must be *anything except* `apple`, `banana`,
        "cherry",              // or `cherry`--including potentially a
    ),                         // non-string value or no value at all.
)
```

다음 표현식과 같이 부울 함수별로 부울 조건을 추가로 결합하여 중첩 로직을 생성할 수 있습니다.

```
or(
    and(
        attr("foo") eq 123,
        attr("bar") eq "abc",
    ),
    and(
        attr("foo") eq 234,
        attr("bar") eq "bcd",
    ),
)
```

이전 표현식은 다음 조건 중 하나를 충족하는 결과를 필터링합니다.
+  다음 두 조건이 모두 참입니다.
  + `foo` 속성 값은 123 *-AND-*입니다.
  + `bar` 속성 값은 "abc"입니다.
+ 다음 두 조건이 모두 참입니다.
  + `foo` 속성 값은 234 *-AND-*입니다.
  + `bar` 속성 값은 "bcd"입니다.

이는 다음 Kotlin 부울 표현식과 같습니다.

```
(foo == 123 && bar == "abc") || (foo == 234 && bar == "bcd")
```

### 함수 및 속성
<a name="ddb-mapper-expressions-dsl-functions"></a>

다음 함수 및 속성은 추가 표현식 기능을 제공합니다.
+ `[contains](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/contains.html)`: 문자열/목록 속성 값에 지정된 값이 포함되어 있는지 확인합니다.
+ `[exists](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/exists.html)`: 속성이 정의되었는지 확인하고 값을 보유합니다( 포함`null`).
+ `[notExists](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/not-exists.html)`: 속성이 정의되지 않았는지 확인
+ `[isOfType](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/is-of-type.html)`: 속성 값이 문자열, 숫자, 부울 등과 같은 지정된 유형인지 확인합니다.
+ `[size](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/size.html)`: 모음의 요소 수 또는 문자열 길이와 같은 속성의 크기를 가져옵니다.
+ `[startsWith](https://docs.aws.amazon.com/sdk-for-kotlin/api/latest/dynamodb-mapper/aws.sdk.kotlin.hll.dynamodbmapper.expressions/-filter/starts-with.html)`: 문자열 속성 값이 지정된 하위 문자열로 시작하는지 확인합니다.

다음 예제에서는 표현식에 사용할 수 있는 추가 함수 및 속성의 사용을 보여줍니다.

```
attr("foo") contains "apple" // Specifies that the attribute value `foo` must be
                             // a list that contains an `apple` element or a string
                             // which contains the substring `apple`.

attr("bar").exists()         // Specifies that the `bar` must exist and have a
                             // value (including potentially `null`).

attr("baz").size lt 100      // Specifies that the attribute value `baz` must have
                             // a size of less than 100.

attr("qux") isOfType AttributeType.String // Specifies that the attribute `qux`
                                          // must have a string value.
```

### 정렬 키 필터
<a name="ddb-mapper-expressions-dsl-sort-key"></a>

정렬 키의 필터 표현식(예: `query` 작업의 `keyCondition` 파라미터)은 명명된 속성 값을 사용하지 않습니다. 필터에서 정렬 키를 사용하려면 모든 비교`sortKey`에서 키워드를 사용해야 합니다. 키워드는 다음 예제와 `attr("<sort key name>")` 같이를 `sortKey` 대체합니다.

```
sortKey startsWith "abc" // The sort key attribute value must begin with the
                         // substring `abc`.

sortKey isIn 0..99       // The sort key attribute value must be between 0
                         // and 99 (inclusive).
```

정렬 키 필터를 부울 로직과 결합할 수 없으며 위에서 설명한 비교의 하위 집합만 지원합니다.
+ [같음 및 비형평성](#ddb-mapper-expressions-dsl-eq-and-ineq): 지원되는 모든 비교
+ [범위 및 세트](#ddb-mapper-expressions-dsl-ranges-sets): 지원되는 모든 비교
+ [부울 로직](#ddb-mapper-expressions-dsl-boolean): 지원되지 않음
+ [함수 및 속성](#ddb-mapper-expressions-dsl-functions): 만 지원`startsWith`됩니다.