Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use generators for cloning step as well #648

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Attribute, AttributeValue } from 'v1/schema'
import { parseAttributeClonedInput } from 'v1/validation/parseClonedInput'
import { cloneAttributeInputAndAddDefaults } from 'v1/validation/cloneInputAndAddDefaults'

import type { ConditionParser } from './parser'

Expand All @@ -14,12 +13,13 @@ export const appendAttributeValue = (
): void => {
const { transform = false } = options

const clonedInput = cloneAttributeInputAndAddDefaults(
const inputParser = parseAttributeClonedInput(
attribute,
expressionAttributeValue as AttributeValue
expressionAttributeValue as AttributeValue,
{ transform }
)
const inputParser = parseAttributeClonedInput(attribute, clonedInput, { transform })
inputParser.next()
inputParser.next() // cloned
inputParser.next() // parsed
const collapsedInput = inputParser.next().value

const expressionAttributeValueIndex = conditionParser.expressionAttributeValues.push(
Expand Down
3 changes: 2 additions & 1 deletion src/v1/operations/expression/expressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export const appendAttributePath = (
case 'record': {
const keyAttribute = parentAttribute.keys
const keyParser = parseAttributeClonedInput(keyAttribute, childAttributeAccessor)
keyParser.next()
keyParser.next() // cloned
keyParser.next() // parsed
const collapsedKey = keyParser.next().value as string

const expressionAttributeNameIndex = parser.expressionAttributeNames.push(collapsedKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { EntityV2 } from 'v1/entity'
import type { Item, RequiredOption } from 'v1/schema'
import { cloneSchemaInputAndAddDefaults } from 'v1/validation/cloneInputAndAddDefaults'
import { parseSchemaClonedInput } from 'v1/validation/parseClonedInput'

type EntityPutCommandInputParser = (entity: EntityV2, input: Item) => Generator<Item, Item>

const requiringOptions = new Set<RequiredOption>(['always', 'atLeastOnce'])

export const parseEntityPutCommandInput: EntityPutCommandInputParser = (entity, input) => {
const clonedInputWithDefaults = cloneSchemaInputAndAddDefaults(entity.schema, input, {
const parser = parseSchemaClonedInput(entity.schema, input, {
requiringOptions,
operationName: 'put'
})

return parseSchemaClonedInput(entity.schema, clonedInputWithDefaults, { requiringOptions })
parser.next() // cloned

return parser
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ export const parseUpdateExtension: ExtensionParser<UpdateItemInputExtension> = (
options
) => {
if (input === $REMOVE) {
if (attribute.required !== 'never') {
throw new DynamoDBToolboxError('parsing.attributeRequired', {
message: `Attribute ${attribute.path} is required and cannot be removed`,
path: attribute.path
})
}

return {
isExtension: true,
*extensionParser() {
yield $REMOVE
return $REMOVE
const clonedValue: typeof $REMOVE = input
yield clonedValue

if (attribute.required !== 'never') {
throw new DynamoDBToolboxError('parsing.attributeRequired', {
message: `Attribute ${attribute.path} is required and cannot be removed`,
path: attribute.path
})
}

const parsedValue: typeof $REMOVE = clonedValue
yield parsedValue

const collapsedValue: typeof $REMOVE = parsedValue
return collapsedValue
}
}
}
Expand All @@ -39,12 +45,9 @@ export const parseUpdateExtension: ExtensionParser<UpdateItemInputExtension> = (
* @debt refactor "Maybe we can simply parse a super-extension here, and continue if is(Super)Extension is false. Would be neat."
*/
if (hasGetOperation(input)) {
// Omit parseExtension & requiringOptions for non-extended values
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { parseExtension: _, requiringOptions: __, ...restOptions } = options

return parseReferenceExtension(attribute, input, {
...restOptions,
...options,
// Can be a reference
parseExtension: parseReferenceExtension
})
}
Expand Down
Loading