-
Notifications
You must be signed in to change notification settings - Fork 506
feat: add view-based filtering support for sheet writing #925
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
Open
bengbengbalabalabeng
wants to merge
7
commits into
apache:main
Choose a base branch
from
bengbengbalabalabeng:feature-issues-854
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd81f3d
feat: add view-based filtering support for sheet writing
bengbengbalabalabeng 1ab1e5a
Merge branch 'main' into feature-issues-854
bengbengbalabalabeng dcd29ee
Merge branch 'main' into feature-issues-854
bengbengbalabalabeng 2fe05e4
refactor: address review feedback for ExcelView and WriteViewMatcher
bengbengbalabalabeng d5f2c77
Merge branch 'main' into feature-issues-854
bengbengbalabalabeng b97d584
Merge branch 'main' into feature-issues-854
bengbengbalabalabeng d134127
docs: update the usage documentation for the @ExcelView annotation
bengbengbalabalabeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
fesod-sheet/src/main/java/org/apache/fesod/sheet/annotation/write/ExcelView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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.fesod.sheet.annotation.write; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import org.apache.fesod.sheet.write.builder.AbstractExcelWriterParameterBuilder; | ||
|
|
||
| /** | ||
| * Annotation used for indicating view(s) that the property | ||
| * that is defined by field annotated is part of. | ||
| * <p> | ||
| * An example annotation would be: | ||
| * <pre> | ||
| * @ExcelView(asTypes = BasicView.class) | ||
| * // Or | ||
| * @ExcelView(asNames = "BasicView") | ||
| * </pre> | ||
| * which would specify that field annotated would be included | ||
| * when processing (writing) Sheet identified by <code>BasicView.class</code> (or its subclass) or | ||
| * <code>"BasicView"</code>. | ||
| * If multiple View class or string identifiers are included, the field will be part of all of them. | ||
| * </p> | ||
| * | ||
| * @see AbstractExcelWriterParameterBuilder#groups(Class[]) | ||
| * @see AbstractExcelWriterParameterBuilder#groups(String[]) | ||
| */ | ||
| @Target(ElementType.FIELD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface ExcelView { | ||
|
Comment on lines
+47
to
+49
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| /** | ||
| * View or views that annotated element is part of. Views are identified | ||
| * by classes, when a view type is selected, fields annotated with that type | ||
| * or any of subtypes are included. | ||
| */ | ||
|
Comment on lines
+51
to
+55
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| Class<?>[] asTypes() default {}; | ||
|
|
||
| /** | ||
| * View or views that annotated element is part of. Views are identified | ||
| * by strings. | ||
| */ | ||
| String[] asNames() default {}; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
fesod-sheet/src/main/java/org/apache/fesod/sheet/write/view/ClassBasedViewMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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.fesod.sheet.write.view; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
| import lombok.EqualsAndHashCode; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.lang3.ArrayUtils; | ||
| import org.apache.fesod.sheet.annotation.write.ExcelView; | ||
|
|
||
| /** | ||
| * View matcher that resolves view-based on class | ||
| * identifiers declared in {@code @ExcelView#asTypes()}. | ||
| */ | ||
| @EqualsAndHashCode | ||
| public class ClassBasedViewMatcher implements WriteViewMatcher { | ||
|
|
||
| private final Collection<Class<?>> expectedGroups; | ||
|
|
||
| public ClassBasedViewMatcher(Class<?>... expectedGroups) { | ||
| this(ArrayUtils.isEmpty(expectedGroups) ? Collections.emptyList() : Arrays.asList(expectedGroups)); | ||
| } | ||
|
|
||
| public ClassBasedViewMatcher(Collection<Class<?>> expectedGroups) { | ||
| if (CollectionUtils.isEmpty(expectedGroups)) { | ||
| throw new IllegalArgumentException("Type-based view groups must not be empty"); | ||
| } | ||
| this.expectedGroups = Collections.unmodifiableCollection(expectedGroups); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean matches(Field field) { | ||
| Class<?>[] fieldGroups = Optional.ofNullable(field.getAnnotation(ExcelView.class)) | ||
| .map(ExcelView::asTypes) | ||
| .orElse(new Class<?>[0]); | ||
|
|
||
| if (ArrayUtils.isEmpty(fieldGroups)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Arrays.stream(fieldGroups).anyMatch(fieldGroup -> expectedGroups.stream() | ||
| .anyMatch(expectedGroup -> expectedGroup.isAssignableFrom(fieldGroup))); | ||
| } | ||
|
Comment on lines
+62
to
+64
|
||
| } | ||
65 changes: 65 additions & 0 deletions
65
fesod-sheet/src/main/java/org/apache/fesod/sheet/write/view/NameBasedViewMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * 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.fesod.sheet.write.view; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
| import lombok.EqualsAndHashCode; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.apache.commons.lang3.ArrayUtils; | ||
| import org.apache.fesod.sheet.annotation.write.ExcelView; | ||
|
|
||
| /** | ||
| * View matcher that resolves view-based on string | ||
| * identifiers declared in {@code @ExcelView#asNames()}. | ||
| */ | ||
| @EqualsAndHashCode | ||
| public class NameBasedViewMatcher implements WriteViewMatcher { | ||
|
|
||
| private final Collection<String> expectedGroups; | ||
|
|
||
| public NameBasedViewMatcher(String... expectedGroups) { | ||
| this(ArrayUtils.isEmpty(expectedGroups) ? Collections.emptyList() : Arrays.asList(expectedGroups)); | ||
| } | ||
|
|
||
| public NameBasedViewMatcher(Collection<String> expectedGroups) { | ||
| if (CollectionUtils.isEmpty(expectedGroups)) { | ||
| throw new IllegalArgumentException("Name-based view groups must not be empty"); | ||
| } | ||
| this.expectedGroups = Collections.unmodifiableCollection(expectedGroups); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean matches(Field field) { | ||
| String[] fieldGroups = Optional.ofNullable(field.getAnnotation(ExcelView.class)) | ||
| .map(ExcelView::asNames) | ||
| .orElse(new String[0]); | ||
|
|
||
| if (ArrayUtils.isEmpty(fieldGroups)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Arrays.stream(fieldGroups).anyMatch(fieldGroup -> expectedGroups.stream() | ||
| .anyMatch(expectedGroup -> expectedGroup.equals(fieldGroup))); | ||
| } | ||
|
Comment on lines
+62
to
+64
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.