Skip to content

Commit 2322749

Browse files
authored
fix: cannot copy filter values which are enum (#48)
1 parent a3fb4b0 commit 2322749

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

src/main/java/io/github/thunderz99/cosmos/condition/Condition.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -810,23 +810,23 @@ Object copyValue(Object value) {
810810

811811
// if value is a condition, copy the nested condition
812812
if (value instanceof Condition) {
813-
return ((Condition) value).copy();
814-
}
813+
return ((Condition) value).copy();
814+
}
815815

816-
// if value is a collection
817-
if (value instanceof Collection<?>) {
818-
var coll = (Collection<?>) value;
819-
return coll.stream().map(item -> copyValue(item)).collect(Collectors.toList());
820-
}
816+
// if value is a collection
817+
if (value instanceof Collection<?>) {
818+
var coll = (Collection<?>) value;
819+
return coll.stream().map(item -> copyValue(item)).collect(Collectors.toList());
820+
}
821821

822-
// primitive type
823-
if (value instanceof String || Primitives.isWrapperType(value.getClass()) || value.getClass().isPrimitive()) {
824-
return value;
825-
}
822+
// primitive type
823+
if (value instanceof String || Primitives.isWrapperType(value.getClass()) || value.getClass().isPrimitive() || value.getClass().isEnum()) {
824+
return value;
825+
}
826826

827-
return JsonUtil.toMap(value);
827+
return JsonUtil.toMap(value);
828828

829-
}
829+
}
830830

831831
/**
832832
* fix enum exception in documentdb sdk

src/test/java/io/github/thunderz99/cosmos/condition/ConditionTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,16 +722,32 @@ public void copy_should_work_for_rawsql() {
722722
for (var param : params) {
723723
assertThat(param.getName()).startsWith("@param0");
724724
if (param.getName().endsWith("01")) {
725-
assertThat(param.getValue(String.class)).isEqualTo("Tom");
726-
} else {
727-
assertThat(param.getValue(Integer.class)).isEqualTo(20);
728-
}
729-
}
730-
}
731-
}
725+
assertThat(param.getValue(String.class)).isEqualTo("Tom");
726+
} else {
727+
assertThat(param.getValue(Integer.class)).isEqualTo(20);
728+
}
729+
}
730+
}
731+
}
732732

733-
@Test
734-
public void buildQuerySpec_should_work_for_is_defined() {
733+
public enum Status {
734+
enrolled, suspended, retired
735+
}
736+
737+
738+
@Test
739+
public void copy_should_work_for_enum() {
740+
741+
var cond = Condition.filter("id", "ID001", "status", Status.enrolled);
742+
743+
var copy = cond.copy();
744+
assertThat(copy.filter.get("id")).isEqualTo("ID001");
745+
assertThat(copy.filter.get("status")).isEqualTo(Status.enrolled);
746+
747+
}
748+
749+
@Test
750+
public void buildQuerySpec_should_work_for_is_defined() {
735751

736752
var q = Condition.filter("id", "Hanks", //
737753
"age IS_DEFINED", true) //

0 commit comments

Comments
 (0)