You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/12/29 13:18:29 UTC
[GitHub] [shardingsphere] gxxiong opened a new pull request, #23171: Implement TELEPHONE_RANDOM_REPLACE Algorithm
gxxiong opened a new pull request, #23171:
URL: https://github.com/apache/shardingsphere/pull/23171
Ref #22759 .
Changes proposed in this pull request:
-Implement TELEPHONE_RANDOM_REPLACE Algorithm
Before committing this PR, I'm sure that I have checked the following options:
- [X] My code follows the [code of conduct](https://shardingsphere.apache.org/community/en/involved/conduct/code/) of this project.
- [X] I have self-reviewed the commit code.
- [ ] I have (or in comment I request) added corresponding labels for the pull request.
- [X] I have passed maven check locally : `./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e`.
- [ ] I have made corresponding changes to the documentation.
- [X] I have added corresponding unit tests for my changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #23171: Implement TELEPHONE_RANDOM_REPLACE Algorithm
Posted by GitBox <gi...@apache.org>.
strongduanmu commented on code in PR #23171:
URL: https://github.com/apache/shardingsphere/pull/23171#discussion_r1059205583
##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String NETWORK_NUMBER = "network-number";
+
+ private Set<String> networkNumbers;
+
+ private List<Integer> networkNumberLength;
+
+ @Getter
+ private Properties props;
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ createNetworkNumbers(props);
+ }
+
+ private void createNetworkNumbers(final Properties props) {
+ MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+ networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+ networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
+ }
+
+ private String isNumeric(final String networkNumber) {
+ try {
+ Integer.parseInt(networkNumber);
+ return networkNumber;
+ } catch (NumberFormatException e) {
+ throw new MaskAlgorithmInitializationException(getType(), "network-number can only be numbers and ,");
+ }
+ }
+
+ @Override
+ public String mask(final Object plainValue) {
+ String result = null == plainValue ? null : String.valueOf(plainValue);
+ if (Strings.isNullOrEmpty(result)) {
+ return result;
+ }
+ Random random = new Random();
+ char[] chars = result.toCharArray();
+ for (Integer length : networkNumberLength) {
Review Comment:
Please rename length to each.
##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
Review Comment:
Please move this algorithm to replace package.
##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String NETWORK_NUMBER = "network-number";
+
+ private Set<String> networkNumbers;
+
+ private List<Integer> networkNumberLength;
+
+ @Getter
+ private Properties props;
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ createNetworkNumbers(props);
+ }
+
+ private void createNetworkNumbers(final Properties props) {
+ MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+ networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+ networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
+ }
+
+ private String isNumeric(final String networkNumber) {
+ try {
+ Integer.parseInt(networkNumber);
+ return networkNumber;
+ } catch (NumberFormatException e) {
+ throw new MaskAlgorithmInitializationException(getType(), "network-number can only be numbers and ,");
+ }
+ }
+
+ @Override
+ public String mask(final Object plainValue) {
+ String result = null == plainValue ? null : String.valueOf(plainValue);
+ if (Strings.isNullOrEmpty(result)) {
+ return result;
+ }
+ Random random = new Random();
+ char[] chars = result.toCharArray();
+ for (Integer length : networkNumberLength) {
+ if (networkNumbers.contains(result.substring(0, length))) {
+ for (int i = length; i != -1 && i < chars.length; i++) {
+ chars[i] = Character.forDigit(random.nextInt(10), 10);
+ }
+ continue;
+ }
+ }
+ return new String(chars);
+ }
+
+ @Override
+ public String getType() {
+ return "TELEPHONE_RANDOM_REPLACE";
Review Comment:
Hi @gxxiong, can we add this new algorithm to mask built-in algorithm document and dev-manual document?
- https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/mask/
- https://shardingsphere.apache.org/document/current/cn/dev-manual/mask/
##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String NETWORK_NUMBER = "network-number";
+
+ private Set<String> networkNumbers;
+
+ private List<Integer> networkNumberLength;
+
+ @Getter
+ private Properties props;
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ createNetworkNumbers(props);
+ }
+
+ private void createNetworkNumbers(final Properties props) {
+ MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+ networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+ networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
Review Comment:
Why we need networkNumberLength param?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] codecov-commenter commented on pull request #23171: Implement TELEPHONE_RANDOM_REPLACE Algorithm
Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #23171:
URL: https://github.com/apache/shardingsphere/pull/23171#issuecomment-1367337641
# [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/23171?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#23171](https://codecov.io/gh/apache/shardingsphere/pull/23171?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (159742c) into [master](https://codecov.io/gh/apache/shardingsphere/commit/aeec3fea5aebcb9f94269dd629eabc763d29c61a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (aeec3fe) will **increase** coverage by `0.01%`.
> The diff coverage is `96.00%`.
```diff
@@ Coverage Diff @@
## master #23171 +/- ##
============================================
+ Coverage 49.96% 49.98% +0.01%
- Complexity 2437 2450 +13
============================================
Files 4116 4117 +1
Lines 57650 57675 +25
Branches 9107 9111 +4
============================================
+ Hits 28806 28830 +24
Misses 26311 26311
- Partials 2533 2534 +1
```
| [Impacted Files](https://codecov.io/gh/apache/shardingsphere/pull/23171?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [...gorithm/cover/TelephoneRandomReplaceAlgorithm.java](https://codecov.io/gh/apache/shardingsphere/pull/23171/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-ZmVhdHVyZXMvbWFzay9jb3JlL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zaGFyZGluZ3NwaGVyZS9tYXNrL2FsZ29yaXRobS9jb3Zlci9UZWxlcGhvbmVSYW5kb21SZXBsYWNlQWxnb3JpdGhtLmphdmE=) | `96.00% <96.00%> (ø)` | |
:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] strongduanmu merged pull request #23171: Implement TELEPHONE_RANDOM_REPLACE Algorithm
Posted by GitBox <gi...@apache.org>.
strongduanmu merged PR #23171:
URL: https://github.com/apache/shardingsphere/pull/23171
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #23171: Implement TELEPHONE_RANDOM_REPLACE Algorithm
Posted by GitBox <gi...@apache.org>.
strongduanmu commented on code in PR #23171:
URL: https://github.com/apache/shardingsphere/pull/23171#discussion_r1059207174
##########
features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/TelephoneRandomReplaceAlgorithm.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.algorithm.cover;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String NETWORK_NUMBER = "network-number";
+
+ private Set<String> networkNumbers;
Review Comment:
Please use Collection if we don't need special Set method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org