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

Enums serialize using .name() #304

Closed
wants to merge 2 commits into from
Closed
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
46 changes: 46 additions & 0 deletions example/src/main/java/com/example/models/EnumModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.models;

import com.orm.SugarRecord;

public class EnumModel extends SugarRecord {
public static enum DefaultEnum {
ONE, TWO
}

public static enum OverrideEnum {
ONE, TWO;

@Override
public String toString() {
return super.toString().toLowerCase();
}
}

private OverrideEnum overrideEnum;
private DefaultEnum defaultEnum;

public EnumModel() {

}

public EnumModel(OverrideEnum e1, DefaultEnum d1) {
overrideEnum = e1;
defaultEnum = d1;
}

public DefaultEnum getDefaultEnum() {
return defaultEnum;
}

public void setDefaultEnum(DefaultEnum defaultEnum) {
this.defaultEnum = defaultEnum;
}

public void setOverrideEnum(OverrideEnum overrideEnum) {
this.overrideEnum = overrideEnum;
}

public OverrideEnum getOverrideEnum() {
return overrideEnum;
}
}
34 changes: 34 additions & 0 deletions example/src/test/java/com/example/sugartest/EnumFieldTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.sugartest;

import com.example.models.EnumModel;
import com.orm.SugarRecord;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import static com.orm.SugarRecord.save;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(RobolectricGradleTestRunner.class)
@Config(emulateSdk = 18)
public class EnumFieldTests {

@Test
public void defaultEnums() {
save(new EnumModel(EnumModel.OverrideEnum.ONE, EnumModel.DefaultEnum.TWO));
EnumModel model = SugarRecord.findById(EnumModel.class, 1);
assertNotNull(model);
assertEquals(model.getDefaultEnum(), EnumModel.DefaultEnum.TWO);
}

@Test
public void overridenEnums() {
save(new EnumModel(EnumModel.OverrideEnum.ONE, EnumModel.DefaultEnum.TWO));
EnumModel model = SugarRecord.findById(EnumModel.class, 1);
assertNotNull(model);
assertEquals(model.getOverrideEnum(), EnumModel.OverrideEnum.ONE);
}

}
6 changes: 4 additions & 2 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static List<Field> getAllFields(List<Field> fields, Class<?> type) {
}

public static void addFieldValueToColumn(ContentValues values, Field column, Object object,
Map<Object, Long> entitiesMap) {
Map<Object, Long> entitiesMap) {
column.setAccessible(true);
Class<?> columnType = column.getType();
try {
Expand Down Expand Up @@ -132,6 +132,8 @@ public static void addFieldValueToColumn(ContentValues values, Field column, Obj
} else {
if (columnValue == null) {
values.putNull(columnName);
} else if (columnType.isEnum()) {
values.put(columnName, ((Enum) columnValue).name());
} else {
values.put(columnName, String.valueOf(columnValue));
}
Expand Down Expand Up @@ -217,7 +219,7 @@ public static void setFieldValueFromCursor(Cursor cursor, Field field, Object ob
Log.e("field set error", e.getMessage());
}
}

private static Field getDeepField(String fieldName, Class<?> type) throws NoSuchFieldException {
try {
Field field = type.getDeclaredField(fieldName);
Expand Down