-
Notifications
You must be signed in to change notification settings - Fork 532
feat: add Character and Year converters (#1017) #1087
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
BigDataDZ
wants to merge
7
commits into
apache:main
Choose a base branch
from
BigDataDZ:feat/character-year-converters
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
ee23fc4
feat: add Character and Year converters (#1017)
BigDataDZ 17412c8
refactor: address review comments on Character and Year converters
BigDataDZ 254a11e
refactor: align default year format field naming
BigDataDZ faf2207
test: cover YearStringConverter and DateUtils year scenarios
BigDataDZ 0226848
Merge branch 'main' into feat/character-year-converters
bengbengbalabalabeng 7986ca1
fix: register the Year converter under the wildcard write key
BigDataDZ 6d65c0f
test: cover Character field round trip through FesodSheet
BigDataDZ 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
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
62 changes: 62 additions & 0 deletions
62
...c/main/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverter.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,62 @@ | ||
| /* | ||
| * 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.converters.charconverter; | ||
|
|
||
| import org.apache.fesod.sheet.converters.Converter; | ||
| import org.apache.fesod.sheet.enums.CellDataTypeEnum; | ||
| import org.apache.fesod.sheet.metadata.GlobalConfiguration; | ||
| import org.apache.fesod.sheet.metadata.data.ReadCellData; | ||
| import org.apache.fesod.sheet.metadata.data.WriteCellData; | ||
| import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; | ||
|
|
||
| /** | ||
| * Character and string converter | ||
| */ | ||
| public class CharacterStringConverter implements Converter<Character> { | ||
| @Override | ||
| public Class<?> supportJavaTypeKey() { | ||
| return Character.class; | ||
| } | ||
|
|
||
| @Override | ||
| public CellDataTypeEnum supportExcelTypeKey() { | ||
| return CellDataTypeEnum.STRING; | ||
| } | ||
|
|
||
| @Override | ||
| public Character convertToJavaData( | ||
| ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| String stringValue = cellData.getStringValue(); | ||
| if (stringValue == null || stringValue.isEmpty()) { | ||
| return null; | ||
| } | ||
| if (stringValue.length() > 1) { | ||
| throw new IllegalArgumentException( | ||
| "Can not convert '" + stringValue + "' to a character, the length must be 1"); | ||
| } | ||
| return stringValue.charAt(0); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteCellData<?> convertToExcelData( | ||
| Character value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| return new WriteCellData<>(value.toString()); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/year/YearStringConverter.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.converters.year; | ||
|
|
||
| import java.time.Year; | ||
| import org.apache.fesod.sheet.converters.Converter; | ||
| import org.apache.fesod.sheet.enums.CellDataTypeEnum; | ||
| import org.apache.fesod.sheet.metadata.GlobalConfiguration; | ||
| import org.apache.fesod.sheet.metadata.data.ReadCellData; | ||
| import org.apache.fesod.sheet.metadata.data.WriteCellData; | ||
| import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; | ||
| import org.apache.fesod.sheet.util.DateUtils; | ||
|
|
||
| /** | ||
| * Year and string converter | ||
| */ | ||
| public class YearStringConverter implements Converter<Year> { | ||
| @Override | ||
| public Class<?> supportJavaTypeKey() { | ||
| return Year.class; | ||
| } | ||
|
|
||
| @Override | ||
| public CellDataTypeEnum supportExcelTypeKey() { | ||
| return CellDataTypeEnum.STRING; | ||
| } | ||
|
|
||
| @Override | ||
| public Year convertToJavaData( | ||
| ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| return DateUtils.parseYear( | ||
| cellData.getStringValue(), getYearFormat(contentProperty), globalConfiguration.getLocale()); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteCellData<?> convertToExcelData( | ||
| Year value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| return new WriteCellData<>( | ||
| DateUtils.format(value, getYearFormat(contentProperty), globalConfiguration.getLocale())); | ||
| } | ||
|
|
||
| private static String getYearFormat(ExcelContentProperty contentProperty) { | ||
| if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { | ||
| return null; | ||
| } | ||
| return contentProperty.getDateTimeFormatProperty().getFormat(); | ||
| } | ||
| } |
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
122 changes: 122 additions & 0 deletions
122
...st/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverterTest.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,122 @@ | ||
| /* | ||
| * 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.converters.charconverter; | ||
|
|
||
| import java.io.File; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.apache.fesod.sheet.FesodSheet; | ||
| import org.apache.fesod.sheet.annotation.ExcelProperty; | ||
| import org.apache.fesod.sheet.context.AnalysisContext; | ||
| import org.apache.fesod.sheet.enums.CellDataTypeEnum; | ||
| import org.apache.fesod.sheet.event.AnalysisEventListener; | ||
| import org.apache.fesod.sheet.metadata.GlobalConfiguration; | ||
| import org.apache.fesod.sheet.metadata.data.ReadCellData; | ||
| import org.apache.fesod.sheet.metadata.data.WriteCellData; | ||
| import org.apache.fesod.sheet.testkit.Tags; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @Tag(Tags.UNIT) | ||
| class CharacterStringConverterTest { | ||
|
|
||
| private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration(); | ||
| private final CharacterStringConverter converter = new CharacterStringConverter(); | ||
|
|
||
| @Test | ||
| void supportJavaTypeKey() { | ||
| Assertions.assertEquals(Character.class, converter.supportJavaTypeKey()); | ||
| } | ||
|
|
||
| @Test | ||
| void supportExcelTypeKey() { | ||
| Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey()); | ||
| } | ||
|
|
||
| @Test | ||
| void convertToJavaDataReturnsSingleCharacter() { | ||
| Assertions.assertEquals( | ||
| Character.valueOf('A'), | ||
| converter.convertToJavaData(new ReadCellData<>("A"), null, GLOBAL_CONFIGURATION)); | ||
| } | ||
|
|
||
| @Test | ||
| void convertToJavaDataReturnsNullForEmptyString() { | ||
| Assertions.assertNull(converter.convertToJavaData(new ReadCellData<>(""), null, GLOBAL_CONFIGURATION)); | ||
| } | ||
|
|
||
| @Test | ||
| void convertToJavaDataThrowsOnLongerStrings() { | ||
| Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> converter.convertToJavaData(new ReadCellData<>("AB"), null, GLOBAL_CONFIGURATION)); | ||
| } | ||
|
|
||
| @Test | ||
| void convertToExcelDataWritesStringCell() { | ||
| WriteCellData<?> cellData = converter.convertToExcelData('A', null, GLOBAL_CONFIGURATION); | ||
| Assertions.assertEquals(CellDataTypeEnum.STRING, cellData.getType()); | ||
| Assertions.assertEquals("A", cellData.getStringValue()); | ||
| } | ||
|
|
||
| @Test | ||
| void characterFieldRoundTripsThroughFesodSheet() throws Exception { | ||
| File file = File.createTempFile("fesod-character", ".xlsx"); | ||
| file.deleteOnExit(); | ||
| CharacterWriteData row = new CharacterWriteData(); | ||
| row.setFlag('A'); | ||
|
|
||
| FesodSheet.write(file, CharacterWriteData.class).sheet().doWrite(Collections.singletonList(row)); | ||
|
|
||
| List<CharacterReadData> rows = FesodSheet.read(file, CharacterReadData.class, new CharacterReadListener()) | ||
| .sheet() | ||
| .doReadSync(); | ||
| Assertions.assertEquals(1, rows.size()); | ||
| Assertions.assertEquals(Character.valueOf('A'), rows.get(0).getFlag()); | ||
| } | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public static class CharacterWriteData { | ||
|
|
||
| @ExcelProperty("flag") | ||
| private Character flag; | ||
| } | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public static class CharacterReadData { | ||
|
|
||
| @ExcelProperty("flag") | ||
| private Character flag; | ||
| } | ||
|
|
||
| public static class CharacterReadListener extends AnalysisEventListener<CharacterReadData> { | ||
|
|
||
| @Override | ||
| public void invoke(CharacterReadData data, AnalysisContext context) {} | ||
|
|
||
| @Override | ||
| public void doAfterAllAnalysed(AnalysisContext context) {} | ||
| } | ||
| } | ||
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.
Suggest extending
AbstractExcelTestand using itscreateTempFile(...)helper to cover multiple formats (e.g., XLSX, XLS, CSV). And usingRoundTripHelper#writeAndRead(...)to simplify the boilerplate read/write code.Additionally, it would be better to move this test into a dedicated integration test class rather than mixing it with pure unit tests.
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.
Same advice:
YearStringConverterTest#yearFieldRoundTripsThroughFesodSheet()