Skip to content

Commit

Permalink
Add ignoreNull property
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitin-da committed Jul 21, 2016
1 parent dffc45d commit 7d6aa12
Show file tree
Hide file tree
Showing 7 changed files with 633 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public StorIOColumnMeta(
@NotNull Element enclosingElement,
@NotNull Element element,
@NotNull String fieldName,
@NotNull JavaType javaType, @NotNull ColumnAnnotation storIOColumn) {
@NotNull JavaType javaType,
@NotNull ColumnAnnotation storIOColumn) {
this.enclosingElement = enclosingElement;
this.element = element;
this.fieldName = fieldName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static javax.lang.model.element.Modifier.PROTECTED;
import static javax.lang.model.element.Modifier.PUBLIC;

public class PutResolverGenerator implements Generator<StorIOContentResolverTypeMeta> {
public class PutResolverGenerator implements Generator<StorIOContentResolverTypeMeta> {

private static final String SUFFIX = "StorIOContentResolverPutResolver";

Expand Down Expand Up @@ -115,11 +115,20 @@ private MethodSpec createMapToContentValuesMethodSpec(@NotNull final StorIOConte
.addCode("\n");

for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
final boolean primitive = columnMeta.element.asType().getKind().isPrimitive();
final boolean needIgnoreNull = columnMeta.storIOColumn.ignoreNull();
final boolean ignoreNull = !primitive && needIgnoreNull; // do not check primitive
if (ignoreNull) {
builder.beginControlFlow("if($L != null)", "object." + columnMeta.fieldName);
}
builder.addStatement(
"contentValues.put($S, $L)",
columnMeta.storIOColumn.name(),
"object." + columnMeta.fieldName
);
if (ignoreNull) {
builder.endControlFlow();
}
}

return builder
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
* @return true if column is key, false otherwise
*/
boolean key() default false;

/**
* Optional: indicates that field should not be serialized when it hasn't value
*
* @return true if column with {@code null} value should be ignored, false otherwise
*/
boolean ignoreNull() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,20 @@ private MethodSpec createMapToContentValuesMethodSpec(@NotNull StorIOSQLiteTypeM
.addCode("\n");

for (StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
final boolean primitive = columnMeta.element.asType().getKind().isPrimitive();
final boolean needIgnoreNull = columnMeta.storIOColumn.ignoreNull();
final boolean ignoreNull = !primitive && needIgnoreNull; // do not check primitive
if (ignoreNull) {
builder.beginControlFlow("if($L != null)", "object." + columnMeta.fieldName);
}
builder.addStatement(
"contentValues.put($S, $L)",
columnMeta.storIOColumn.name(),
"object." + columnMeta.fieldName
);
if (ignoreNull) {
builder.endControlFlow();
}
}

return builder
Expand Down
Loading

0 comments on commit 7d6aa12

Please sign in to comment.