Skip to content

Commit

Permalink
support writing enums in DynamoDB AWS SDK v1 (#228)
Browse files Browse the repository at this point in the history
* support writing enums in DynamoDB AWS SDK v1

* fixed CodeNarc

* license
  • Loading branch information
musketyr authored May 7, 2024
1 parent 850f65c commit 23d58a9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ class DefaultDynamoDBService<TItemClass> implements DynamoDBService<TItemClass>

}
)
.addFirstType(Enum,
new ArgumentMarshaller() {

@Override
AttributeValue marshall(Object obj) {
return obj == null ? null : new AttributeValue().withS(obj.toString())
}

},
new ArgumentUnmarshaller() {

@Override
void typeCheck(AttributeValue value, Method setter) { }

@Override
Object unmarshall(AttributeValue value) throws ParseException {
// should not happen
throw new IllegalStateException('Cannot unmarshall Enum, add @DynamoDBConvertedEnum annotation to the field')
}

}
)
.build()

// Specific ranges ending with 'Index' are String concatenated indexes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class DynamoDBEntity {

Integer number = 0;

@DynamoDBTypeConvertedEnum
State state = State.UNKNOWN;

public String getParentId() {
return parentId;
}
Expand Down Expand Up @@ -92,6 +95,14 @@ public void setGlobalIndex(String globalIndex) {
// ignore
}

public State getState() {
return state;
}

public void setState(State state) {
this.state = state;
}

//CHECKSTYLE:OFF
@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2024 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.aws.dynamodb;

public enum State {

OPEN,
CLOSED,
IN_PROGRESS,
CANCELLED,
UNKNOWN;

}

0 comments on commit 23d58a9

Please sign in to comment.