-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support MultiValueMap mapping for columns using Regex
Feature request Refs: 304
- Loading branch information
Showing
9 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/poiji/annotation/ExcelCellsJoinedByName.java
This file contains 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,20 @@ | ||
package com.poiji.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Created by aerfus on 18/02/2024 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
@Documented | ||
public @interface ExcelCellsJoinedByName { | ||
|
||
/** | ||
* Specifies the column regular expression where the corresponding values are mapped from the | ||
* Excel data | ||
* | ||
* @return column regular expression | ||
*/ | ||
String expression(); | ||
} |
This file contains 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 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 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
55 changes: 55 additions & 0 deletions
55
src/test/java/com/poiji/deserialize/ReadExcelWithRegexAndJoinValuesTest.java
This file contains 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,55 @@ | ||
package com.poiji.deserialize; | ||
|
||
import com.poiji.bind.Poiji; | ||
import com.poiji.deserialize.model.Album; | ||
import com.poiji.option.PoijiOptions; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import static com.poiji.option.PoijiOptions.PoijiOptionsBuilder.settings; | ||
import static com.poiji.util.Data.unmarshallingAlbums; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Test for Reading Excel columns with Regular expression (Regex) | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class ReadExcelWithRegexAndJoinValuesTest { | ||
private final String path; | ||
private final List<Album> expectedData; | ||
private final PoijiOptions options; | ||
|
||
public ReadExcelWithRegexAndJoinValuesTest(String path, List<Album> expectedData, PoijiOptions options) { | ||
this.path = path; | ||
this.expectedData = expectedData; | ||
this.options = options; | ||
} | ||
|
||
@Parameterized.Parameters(name = "{index}: ({0})={1}") | ||
public static Iterable<Object[]> queries() { | ||
return List.of(new Object[][]{ | ||
{ | ||
"src/test/resources/regex/album.xlsx", | ||
unmarshallingAlbums(), | ||
settings().sheetName("Sheet 1").build() | ||
}, | ||
{ | ||
"src/test/resources/regex/album.xls", | ||
unmarshallingAlbums(), | ||
settings().sheetName("Sheet 1").build() | ||
}, | ||
}); | ||
} | ||
|
||
@Test | ||
public void shouldReadAlbumData() { | ||
List<Album> actualData = Poiji.fromExcel(new File(path), Album.class, options); | ||
|
||
assertEquals(expectedData, actualData); | ||
} | ||
|
||
} |
This file contains 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,47 @@ | ||
package com.poiji.deserialize.model; | ||
|
||
import com.poiji.annotation.ExcelCellsJoinedByName; | ||
import org.apache.commons.collections4.MultiValuedMap; | ||
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* An Album POJO. | ||
*/ | ||
public class Album { | ||
@ExcelCellsJoinedByName(expression = "Artist") | ||
private MultiValuedMap<String, String> artists = new ArrayListValuedHashMap<>(); | ||
|
||
@ExcelCellsJoinedByName(expression = "Track[0-9]+") | ||
private MultiValuedMap<String, String> tracks = new ArrayListValuedHashMap<>(); | ||
|
||
public void setArtists(MultiValuedMap<String, String> artists) { | ||
this.artists = artists; | ||
} | ||
|
||
public void setTracks(MultiValuedMap<String, String> tracks) { | ||
this.tracks = tracks; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Album{" + | ||
"artists=" + artists + | ||
", tracks=" + tracks + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Album album = (Album) o; | ||
return Objects.equals(artists, album.artists) && Objects.equals(tracks, album.tracks); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(artists, tracks); | ||
} | ||
} |
This file contains 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
Binary file not shown.
Binary file not shown.