Repository navigation
JAVA-1316: Naming strategy support in mapper configuration level - #778
avivcarmis wants to merge 13 commits into
Conversation
…ate set[unordered] to list[ordered]) -fix an issue with initializing tests
fixed default enabled of naming strategy
|
Regarding the 2nd note (column quoting) it raised issues within the driver so I added the regexp validation. Column names will be quoted only when non lowercase alphanumeric or underscore (as mentioned in the docs) |
|
@olim7t you've closed this one too, still relevant? |
|
Re-opening (was closed by mistake). |
|
|
||
| /** | ||
| * Represent a naming convention (e.g. snake_case, camelCase, etc...) to be used when | ||
| * auto-translating java property names to cassandra column names and vise-versa. |
| private NamingConvention cassandraConvention; | ||
|
|
||
| public NamingStrategy(NamingConvention javaConvention, NamingConvention cassandraConvention) { | ||
| setJavaConvention(javaConvention); |
There was a problem hiding this comment.
Don't call public setters from a constructor, assign to fields directly.
| * @param input value to translate | ||
| * @return Java naming convention translation of the input | ||
| */ | ||
| public String toJava(String input) { |
There was a problem hiding this comment.
This is not covered by tests currently.
There was a problem hiding this comment.
Nor is it used anywhere in the production code. Do we really need it?
There was a problem hiding this comment.
I went for designing an independent interface for handling translation of naming conventions to it seemed trivial to implement both ways. If we prefer to avoid unused code we can remove it. If in the future we find a need to translate Cassandra column names to java fields, as you can see, the implementation is 3 lines.
So i think we can go for removing right now.
There was a problem hiding this comment.
So i think we can go for removing right now.
Yes please. We need to be a bit conservative at this stage because once a line of code is contributed to the driver, it means it needs to be maintained :)
| * Enables the naming strategy, allowing it to auto translate properties that haven't been | ||
| * explicitly translated. | ||
| */ | ||
| public void enable() { |
There was a problem hiding this comment.
Sorry but isn't this going a bit too far? Why would someone enable then disable the naming strategy? IMO it should be always enabled.
| * across different naming conventions. | ||
| */ | ||
| @SuppressWarnings("unused") | ||
| public class PropertyNamingStrategyTest extends CCMTestsSupport { |
There was a problem hiding this comment.
The following conventions seem untested: UpperCamelCase, UpperSnakeCase, LowerKebabCase, UpperKebabCase.
I have a mixed feeling about the "kebab" variants.
First of all, this "kebab" name: as per this SO question it seems we could call it "lisp-case" or "spinal-case" as well. If for nothing else, I'd rather pay hommage to a beautiful programming language than to some greasy – yet savory – meat skewers :)
Secondly, as you said, the kebab variants are not possible in Java. They could be possible in Cassandra, provided that the column name is double-quoted: "MY-COLUMN" is a valid CQL identifier. This should be explicitly tested for.
I'd suggest here the use of a @DataProvider annotation that would test all the possible valid combinations of Java + Cassandra naming conventions.
There was a problem hiding this comment.
I think we can go for UPPER-LIPS-CASE and lower-lisp-case
Yes – provided you spell it LISP, not LIPS :D
|
Yes, sorry for closing, it was again because it's targeted to my now-obsolete java1310 branch. Btw this will probably bothersome when we merge the PRs, but no need to close and re-open against 3.x, we'll handle it manually. |
| } | ||
| } | ||
| return caseSensitive ? Metadata.quote(columnName) : columnName.toLowerCase(); | ||
| return columnName.matches(UNQUOTED_COLUMN_NAME_REGEXP) ? columnName : Metadata.quote(columnName); |
There was a problem hiding this comment.
I wonder if we could quote everything by default here, I think these names are only used for internal requests.
There was a problem hiding this comment.
I initially tried to as i wrote in my first comment, but it broke some other places within the mapper (i can look them up although i can't recall right now) that were expecting lower cases not to be quoted.
| properties.put(property.getName(), property); | ||
| if (!properties.containsKey(property.getName())) { | ||
| properties.put(property.getName(), property); | ||
| } |
| * @param input value to translate | ||
| * @return Java naming convention translation of the input | ||
| */ | ||
| public String toJava(String input) { |
There was a problem hiding this comment.
Nor is it used anywhere in the production code. Do we really need it?
|
|
||
| private NamingConvention javaConvention; | ||
|
|
||
| private NamingConvention cassandraConvention; |
There was a problem hiding this comment.
I suggest making these fields final, and removing the getters and setters. The naming strategy is something that users will set at the beginning, and never worry about later. I see little value in accessing the conventions, let alone changing them at runtime.
There was a problem hiding this comment.
So we can remove setters and enable, disable methods.
One thing is design how should users disable naming strategy all together (in case they have their cassandra column names exactly like their java fields). As you suggested below we probably want to define a NamingStrategy singleton to bypass naming. Only question is whether this is a trivial approach for the user, i.e.:
config.setNamingStrategy(NamingStrategy.PASS_THROUGH);
What do you think?
There was a problem hiding this comment.
disable naming strategy all together
I don't think users would actually want that, there should always be a naming strategy in place.
The question is: what should be the default strategy? I think we need to prioritize continuity and favor a strategy that actually yields the same results as currently, without any need to customize the default settings. In other words: favor convention over configuration. So I think indeed we need a "passthrough" strategy as our default.
There was a problem hiding this comment.
@adutra To my understanding the default today is just converting to lower-case (i.e. converting myXmlParser simply to myxmlparser), this means that the default should be LowerCamelCase to LowerCase which will provide the same result. if we pass through we are to output myXmlParser which might break some code for migrating developers. Right?
| * @return whether or not the given naming convention returns exactly | ||
| * the same output as this | ||
| */ | ||
| boolean equals(NamingConvention other); |
There was a problem hiding this comment.
It seems that this is only used to optimize the conversions in NamingStrategy when the two conventions are the same.
We could instead have a PassThroughNamingStrategy that contains null conventions and overrides toCassandra (and toJava if we keep it). It could even be a unique instance of an anonymous class exposed as a singleton, such as NamingStrategy.PASS_THROUGH.
This way we get rid of equals in all the implementations.
| * <li>{@code true} - myXMLParser</li> | ||
| * </ul> | ||
| */ | ||
| public static class LowerCamelCase extends CamelCase { |
There was a problem hiding this comment.
More documentation on these classes would be appreciated, and also on the constructors as some of them aren't clear to me (i.e. It isn't immediately clear to me what ignorablePrefixes means)
| public static class TestRecommendedSettings { | ||
|
|
||
| @PartitionKey | ||
| private int mMyXmlParser; |
There was a problem hiding this comment.
Sort of confused how this works. Since the Java Convention is LowerCamelCase (new CommonNamingConventions.LowerCamelCase("m", "_")) should this be mmyXmlParser, as the first m would be stripped and myXmlParser is LowerCamelCase (vs MyXmlParser which would be UpperCamelCase)?
There was a problem hiding this comment.
So the corresponding table snake_case_table contain the fields my_xml_parser and other. If I am a sworn android developer, i'd probably put m in the beginning of all my private fields, or i may have any other blind faith like putting _ as i personally like. But there many other examples. Anyways, in this test we want to tell the mapper - each time you spot m or _ in the beginning of a field name (or a getter/setter), strip it down. then convert to the output naming conversion.
So what the mapper does here is actually receive mMyXmlParser as input, strip it to myXmlParser, and then transform to snake case (the output strategy) which results in my_xml_parser. Same for _other field.
Practically, you should use this option only if you do have a constant prefix you use, and only for the one you use. I don't think anyone actually uses both "_" and "m" as prefixes but the test covers them both. although some extreme conventions have many different prefixes depending on the type of the variable, and we can support it too.
|
Summing up:
|
|
A new reflection I've had. Got some inputs that seemed like
The latter option seems to be less intuitive. I think that if someone is crazy enough to have cassandra convention set to something other than lower snake case and other than Regarding prefixes, since this is rather common - i think we should keep it. |
|
Note: these changes (with modifications for recent api changes) were applied to #814. |
|
Closing this since work has been moved to #814 |
Hi (:
So this is what i eventually thought to be the best design for a solution for Java-1316. First, two quick examples, then some key notes for feedback.
Example 1: camelCase to snake_case
Consider this class:
and this table:
CREATE TABLE foo (my_xml_parser int primary key, other int)Then in order to automatically match the two, we have to bootstrap a mapper configuration like this:
Or like this (whichever seems more intuitive):
Example 2: camelCase with ignorable prefixes
It is a common practice in Java to use a fixed prefix for all class member names. Let's now consider class
Foo2and the tablefoodefined aboveThen in order to automatically match the two, we have to bootstrap a mapper configuration with java convention of lower case with ignorable prefix of "m":
Or like this (whichever seems more intuitive):
Lastly, some important things to note:
CommonNamingConventionsclass - lowerCamelCase, UpperCamelCase, lower_snake_case, UPPER_SNAKE_CASE, lowercase, lower-kebab-case (currently cannot be used either as java properties or as cassandra columns) and UPPER-KEBAB-CASE (likewise). In a case where a user uses a different naming convention, he should implement theNamingConventioninterface and pass an instance of it to theMapperConfiguration.NamingStrategyobject. More info available in the javadoc ofNamingConventionandCommonNamingConventions.PropertyMapper::inferColumnNamethe last line was quating only case-sensetive values but not lower-cased values. Is there a down side to always quote column names? Currently it does, if that is a problem, i can add a regexp match to determine whether all chars are lower-cased in the resulted column name.myXMLParser), and java naming convention is not - then there's no way to detect abbreviations and user has to explicitly declare them [e.g.@Column(name = "myXMLParser")].mMyXmlParser,m_myXmlParser,_myXmlParser) who have to be expicitly declared.camelCase- to -lowercasestrategy which exists today (e.g. having a property namedmyXmlParserand a matching cassandra column namedmyxmlparser), the default cassandra naming convention is set toLowerCase, although the common strategy should probably be set tocamelCase- to -snake_case.@Columnor@Fieldannotation, and sets either column name or case sensitive values, naming strategy will be bypassed for this property and previous result should occur.CamelCaseparser, you may supply an array of member naming prefixes to ignore (e.g.mMyXmlParser,m_myXmlParser,_myXmlParser). In order to be 100% backwards compatible, if you do not supply them , the current default none. This means that by default,"m_myXmlParser"is mapped to"m_myxmlparser"instead of just"myxmlparser". If a user does want those he has to explicitly pass them to the constructor [e.g.new CamelCase("_", "m", "m_")]. This is more of a product question but you may decide to skip 100% backwards compatible (if we can assume no one has named his DB columns"m_my_xml_parser") and then have this kickass support by default (: