Skip to content

Allow passing POJOs as field values throughout API. #76

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

Merged
merged 9 commits into from
Oct 23, 2018
Merged
Changes from 1 commit
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
Expand Up @@ -72,15 +72,14 @@ public UserDataConverter(DatabaseId databaseId) {
/** Parse document data from a non-merge set() call. */
public ParsedSetData parseSetData(Object input) {
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.Set);
FieldValue updateData = convertAndParseData(input, accumulator.rootContext());

return accumulator.toSetData((ObjectValue) updateData);
ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());
return accumulator.toSetData(updateData);
}

/** Parse document data from a set() call with SetOptions.merge() set. */
public ParsedSetData parseMergeData(Object input, @Nullable FieldMask fieldMask) {
ParseAccumulator accumulator = new ParseAccumulator(UserData.Source.MergeSet);
ObjectValue updateData = (ObjectValue) convertAndParseData(input, accumulator.rootContext());
ObjectValue updateData = convertAndParseDocumentData(input, accumulator.rootContext());

if (fieldMask != null) {
// Verify that all elements specified in the field mask are part of the parsed context.
Expand All @@ -94,7 +93,6 @@ public ParsedSetData parseMergeData(Object input, @Nullable FieldMask fieldMask)
}

return accumulator.toMergeData(updateData, fieldMask);

} else {
return accumulator.toMergeData(updateData);
}
Expand Down Expand Up @@ -194,25 +192,30 @@ public FieldValue parseQueryValue(Object input) {

/** Converts a POJO to native types and then parses it into model types. */
private FieldValue convertAndParseData(Object input, ParseContext context) {
UserData.Source source = context.getDataSource();
Object converted = CustomClassMapper.convertToPlainJavaTypes(input);
return parseData(converted, context);
}

/**
* Wrapper around convertAndParseData() that expects input to conform to document data (in
* particular, must decode into an ObjectValue).
*/
private ObjectValue convertAndParseDocumentData(Object input, ParseContext context) {
String badDocReason =
"Invalid data. Data must be a Map<String, Object> or a suitable POJO object, but it was ";

// Check Array before calling CustomClassMapper since it'll give you a confusing message
// to use List instead, which also won't work in a set().
if (source == UserData.Source.Set && input.getClass().isArray()) {
if (input.getClass().isArray()) {
throw new IllegalArgumentException(badDocReason + "an array");
}

// Convert POJOs
Object converted = CustomClassMapper.convertToPlainJavaTypes(input);
FieldValue value = convertAndParseData(input, context);

if (source == UserData.Source.Set && !(converted instanceof Map)) {
if (!(value instanceof ObjectValue)) {
throw new IllegalArgumentException(badDocReason + "of type: " + Util.typeName(input));
}

// Parse to Model types.
return parseData(converted, context);
return (ObjectValue) value;
}

/**
Expand Down