Skip to content

Remove repeat hyphen #474

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

Closed

Conversation

bodom91
Copy link
Contributor

@bodom91 bodom91 commented Aug 28, 2016

This change is Reviewable

@@ -65,6 +65,8 @@ protected void initBinder(WebDataBinder binder) {
StringTrimmerEditor editor = new StringTrimmerEditor(false);
binder.registerCustomEditor(String.class, "name", editor);
binder.registerCustomEditor(String.class, "nameRu", editor);
binder.registerCustomEditor(String.class, "name", new CustomCategoryNameEditor());
binder.registerCustomEditor(String.class, "nameRu", new CustomCategoryNameEditor());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use the same instance of the editor for both fields? In other words, is the class thread-safe?

@php-coder
Copy link
Owner

php-coder commented Aug 28, 2016

плюс не хватает интеграционных тестов

@php-coder
Copy link
Owner

Трэвис, помимо всего прочего, также жалуется на отсутствие хедера: https://github.com/php-coder/mystamps/wiki/check-license-header

@bodom91 bodom91 force-pushed the gh465_Prohibit_spec_char_collection branch from 4e16819 to 613fe42 Compare August 28, 2016 15:39
@@ -62,9 +62,9 @@

@InitBinder("addCategoryForm")
protected void initBinder(WebDataBinder binder) {
StringTrimmerEditor editor = new StringTrimmerEditor(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот почему у тебя тесты падать начали -- ты зря удалил этот эдитор -- он для другого и нужен здесь.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я добавил тримы непосредственно в реализацию кастомэдитора и все чеки прошли.
Возможно, я неверно трактовал эту проверку, но исходя из того что мы везде тримим пробелы, я решил это вынести.
И еще один момент, если мы оставим этот эдитор, то у нас не будет это все корректно работать , я пока не капнул почему, но моя догадка, что ЛУЧШЕ использовать только один эдитор.
Как ты считаешь ??

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ни разу не имел дел с более чем одним валидатором, поэтому придется тебе экспериментировать и проверять что/как можно, а что нельзя. Если смотреть с точки зрения принципа единой ответственности (SRP), то лучше держать проверки отдельно -- и реюзать проще и логика не "слипается".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну вот я и эксперементировал , и если делать два подряд - выполняется только последний......
Еще попробую покрутить, но пока такие результаты.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Only one single registered custom editor per property path is supported. (c) http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/DataBinder.html#registerCustomEditor-java.lang.Class-java.lang.String-java.beans.PropertyEditor-

Тогда надо либо попробовать какие-либо конвертеры использовать, либо использовать паттерн декоратор (и вкладывать один в другой).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to re-implement StringTrimmerEditor and don't re-use this class then we don't need this flags because we're not using them, right?

Then, implement our own editor that do a trimming and also replace hyphens. Hm. Not ideal, but we have tests after all, so we can refactor this any time.

And also add the comment why we're not using StringTrimmerEditor.

Copy link
Contributor Author

@bodom91 bodom91 Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes , i will not use StringTrimEditor class , but i need use boolean .
I need it because we want separate functionality CustomEditor.
If we have true - trim String , if we have false or empty value - not trim String.
Do you agree ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're going to add flag that always will have value true, right? As far I understand we'll use this converter for category and country names. For them we always need to trim the string, right?

And now I'm still don't understand why we need this flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly ))
Maybe i do , and you will see ... )

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@bodom91 bodom91 force-pushed the gh465_Prohibit_spec_char_collection branch 5 times, most recently from 22760b2 to 8e3be2d Compare August 29, 2016 13:45
@@ -24,6 +24,8 @@ value.too-short = Value is less than allowable minimum of {min} characters
value.too-long = Value is greater than allowable maximum of {max} characters
value.invalid-length = Value length must be equals to {max} characters
value.hyphen = Value must not start or end with hyphen
value.hyphen_repeat_en = Value must not contain repetition of hyphen
value.hyphen_repeat_ru = Value must not contain repetition of hyphen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need such duplication? Please, join them and remove language postfix.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And let's rename it to repeating_hyphen.

@bodom91 bodom91 force-pushed the gh465_Prohibit_spec_char_collection branch 2 times, most recently from 23a9bb8 to 5a546bf Compare August 29, 2016 19:39
@php-coder
Copy link
Owner

66 message from 1 PR ..

It's a normal working process. Especially when we're discussed about how to achieve our goal.

}

if (text.contains(" ")) {
text = text.replaceAll(REPEATING_SPACES_REGEXP, " ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR replaceAll will compile the regexp on every method's invocation. Let's use Pattern and compile the regexp only once.

@bodom91 bodom91 force-pushed the gh465_Prohibit_spec_char_collection branch from 5a546bf to af6a087 Compare September 14, 2016 22:45
Copy link
Owner

@php-coder php-coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're closed to finishing this issue! :)

@@ -42,6 +42,9 @@
public static final String CATEGORY_NAME_EN_REGEXP = "[- a-zA-Z]+";
public static final String CATEGORY_NAME_RU_REGEXP = "[- а-яёА-ЯЁ]+";
public static final String CATEGORY_NAME_NO_HYPHEN_REGEXP = "[ \\p{L}]([- \\p{L}]+[ \\p{L}])*";
// CheckStyle: ignore LineLength for next 1 line
@SuppressWarnings("PMD")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which exactly warning from PMD you're disabling? (Hint: git grep @SuppressWarnings to find examples)

Also, if we're using @SuppressWarnings we can suppress CheckStyle's warning too: @SuppressWarnings({"PMD.LongVariable", "checkstyle:linelength"})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where can i read about SuppressWarnings ??

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know... I didn't write docs for that yet (tickets already exist).

}

if (text.contains(" ")) {
text = text.replaceAll(String.valueOf(REP_SPACES), " ");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missed the point: we don't want to use String.replaceAll at all. Instead we should use Pattern and Matcher to do the same.

See also: http://stackoverflow.com/questions/1466959/string-replaceall-vs-matcher-replaceall-performance-differences

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did it .

@RequiredArgsConstructor
public class ReplaceRepeatingSpacesEditor extends PropertyEditorSupport {
private static final String REPEATING_SPACES_REGEXP = "[ ]{2,}";
private static final Pattern REP_SPACES = Pattern.compile(REPEATING_SPACES_REGEXP);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this member to REPEATING_SPACES

*/
@RequiredArgsConstructor
public class ReplaceRepeatingSpacesEditor extends PropertyEditorSupport {
private static final String REPEATING_SPACES_REGEXP = "[ ]{2,}";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why we need this as a separate constant. Inline this string.

Copy link
Owner

@php-coder php-coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know (by sending e-mail or leaving comment here) once you update this branch (because I don't receive notifications and have to check for any updated manually).

@bodom91
Copy link
Contributor Author

bodom91 commented Sep 19, 2016

Ok , sorry for delay , i m on holiday )

@php-coder
Copy link
Owner

@bodom91 No problem, I just wanted to say that I stopped polling this PR every day and now I'm waiting when you ping me.

@php-coder
Copy link
Owner

@bodom91 Have a great time there!

@bodom91 bodom91 force-pushed the gh465_Prohibit_spec_char_collection branch 3 times, most recently from 070aa92 to 1edb4ed Compare September 22, 2016 11:12
@bodom91
Copy link
Contributor Author

bodom91 commented Sep 23, 2016

You can check it. =)

@php-coder
Copy link
Owner

@bodom91 Ok, thank you! It's in my todo list for today.

@php-coder
Copy link
Owner

LGTM. Please, squash commits.

// We can't use StringTrimmerEditor here because "only one single registered custom editor per property path is supported".
ReplaceRepeatingSpacesEditor nameEditor = new ReplaceRepeatingSpacesEditor(true);
binder.registerCustomEditor(String.class, "name", nameEditor);
binder.registerCustomEditor(String.class, "nameRu", nameEditor);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, it would be good if you also will change the name from nameEditor to editor. I don't see a reason for such renaming.

@bodom91 bodom91 force-pushed the gh465_Prohibit_spec_char_collection branch from 1edb4ed to ae8a699 Compare September 26, 2016 10:43
@php-coder
Copy link
Owner

Merged in c8f2dca commit.

Thank you for your patience and contribution!

@php-coder php-coder closed this Sep 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants