-
Notifications
You must be signed in to change notification settings - Fork 615
Adding test utilities to create Value types #1158
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
Adding test utilities to create Value types #1158
Conversation
This will be used (and tested) in the follow-up CL that adds the FieldValue tpes.
This exposes a similar interface as wrap() and allows me to change the existing FieldValue test to use Value protos
Codecov Report
Continue to review full report at Codecov.
|
import java.util.List; | ||
|
||
/** Test helper to create Firestore Value protos from Java types. */ | ||
public class ValueHelper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of classes named with "Util" or "Helper". How about just Values
? This is a convention found in Guava, where e.g. helpers for creating/manipulating lists are in Lists
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ValueHelperUtil it is :)
I renamed it to Values. FWIW, in most APIs that use this convention, the helper library and the main library sit in the same package, which we can't do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lists
is a great example because it's a collection of Guava utilities about java.util.List
. In other cases where the library has a class and static utilities for dealing with it, it makes sense that they'd colocate them. I don't think that invalidates the static utilities as plurals convention.
See go/java-practices/utility-classes.md.
return Value.newBuilder().setMapValue(builder).build(); | ||
} | ||
|
||
public static Value wrapRef(DatabaseId dbId, DocumentKey key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest of this class doesn't really have much to do with wrapping, but does create values. How about refValue
or something like that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if (o instanceof Value) { | ||
return (Value) o; | ||
} else if (o instanceof String) { | ||
return (Value.newBuilder().setStringValue((String) o).build()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra parens, here and throughout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed. Sorry, I was quite certain googleJavaFormat
would do this for me.
com.google.protobuf.Timestamp.newBuilder() | ||
.setSeconds(timestamp.getSeconds()) | ||
.setNanos(timestamp.getNanoseconds()) | ||
.build()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This inner build()
is unnecessary. Proto setters take messages or builders and automatically call build()
for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed here and for GeoPoint.
} else if (o instanceof DocumentReference) { | ||
return (Value.newBuilder() | ||
.setReferenceValue( | ||
"projects/projectId/databases/(default)/documents/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
projectId
isn't a valid project identifier. Elsewhere we've just used "project", FWIW.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stole this from somewhere (SQLiteSchemaTest) and fixed it there as well.
@@ -0,0 +1,98 @@ | |||
// Copyright 2020 Google LLC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you currently only need this for tests, but eventually, won't the UserDataConverter need this as well? Maybe put this in with regular sources?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to punt on this for now to figure out how much of this code I will need in UserDataConverter and where to put it. I added a TODO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I think it's nice that this is a separate class that just has these static, simple functions. Moving it into the UserDataConverter
would muddy that considerably. I was proposing just moving the class wholesale into the same package, or maybe even merging with/renaming ProtoValues
and putting it in model
.
In any case, no action required for this PR for sure.
/test device-check-changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -0,0 +1,98 @@ | |||
// Copyright 2020 Google LLC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I think it's nice that this is a separate class that just has these static, simple functions. Moving it into the UserDataConverter
would muddy that considerably. I was proposing just moving the class wholesale into the same package, or maybe even merging with/renaming ProtoValues
and putting it in model
.
In any case, no action required for this PR for sure.
import java.util.List; | ||
|
||
/** Test helper to create Firestore Value protos from Java types. */ | ||
public class ValueHelper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lists
is a great example because it's a collection of Guava utilities about java.util.List
. In other cases where the library has a class and static utilities for dealing with it, it makes sense that they'd colocate them. I don't think that invalidates the static utilities as plurals convention.
See go/java-practices/utility-classes.md.
return Value.newBuilder().setStringValue((String) o).build(); | ||
} else if (o instanceof Blob) { | ||
return Value.newBuilder().setBytesValue(((Blob) o).toByteString()).build(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: excess newline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry - I forgot to address this before merging. I fixed it in the follow up PR.
return (Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()); | ||
return Value.newBuilder().setArrayValue(list).build(); | ||
} else if (o instanceof Map) { | ||
com.google.firestore.v1.MapValue.Builder builder = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: import MapValue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@schmidt-sebastian: The following test failed, say
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
This exposes a similar interface as wrap() and allows me to change the existing FieldValue test to use Value protos.
For reference: #1156