-
Notifications
You must be signed in to change notification settings - Fork 532
feat: add UUID string converter (#1017) #1088
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
zhanghuang090
wants to merge
4
commits into
apache:main
Choose a base branch
from
zhanghuang090:codex/add-uuid-string-converter
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.
+246
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a375f3b
feat: add UUID string converter (#1017)
zhanghuang090 dbaada6
test: address UUID converter review feedback
zhanghuang090 13fe9b0
fix: align UUID converter naming and handle blank input
zhanghuang090 fabe330
Merge branch 'main' into codex/add-uuid-string-converter
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
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
59 changes: 59 additions & 0 deletions
59
fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/uuid/UUIDStringConverter.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,59 @@ | ||
| /* | ||
| * 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.uuid; | ||
|
|
||
| import java.util.UUID; | ||
| import org.apache.fesod.common.util.StringUtils; | ||
| 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; | ||
|
|
||
| /** | ||
| * Converts UUID values to canonical lowercase strings and reads strings using {@link UUID#fromString(String)}. | ||
| * Blank strings are treated as missing values, like blank cells. Leading and trailing whitespace is trimmed before parsing. | ||
| */ | ||
| public class UUIDStringConverter implements Converter<UUID> { | ||
|
|
||
| @Override | ||
| public Class<UUID> supportJavaTypeKey() { | ||
| return UUID.class; | ||
| } | ||
|
|
||
| @Override | ||
| public CellDataTypeEnum supportExcelTypeKey() { | ||
| return CellDataTypeEnum.STRING; | ||
| } | ||
|
|
||
| @Override | ||
| public UUID convertToJavaData( | ||
| ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| String value = cellData.getStringValue(); | ||
| return StringUtils.isBlank(value) ? null : UUID.fromString(value.trim()); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteCellData<?> convertToExcelData( | ||
| UUID value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| return new WriteCellData<>(value.toString()); | ||
| } | ||
| } |
183 changes: 183 additions & 0 deletions
183
fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/UuidConverterTest.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,183 @@ | ||
| /* | ||
| * 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.converter; | ||
|
|
||
| import java.io.File; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.UUID; | ||
| import lombok.Data; | ||
| import org.apache.fesod.sheet.converters.ConverterKeyBuild; | ||
| import org.apache.fesod.sheet.converters.DefaultConverterLoader; | ||
| import org.apache.fesod.sheet.converters.uuid.UUIDStringConverter; | ||
| import org.apache.fesod.sheet.enums.CellDataTypeEnum; | ||
| import org.apache.fesod.sheet.exception.ExcelDataConvertException; | ||
| 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.apache.fesod.sheet.testkit.base.AbstractExcelTest; | ||
| import org.apache.fesod.sheet.testkit.enums.ExcelFormat; | ||
| import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper; | ||
| import org.apache.fesod.sheet.testkit.params.ExcelFormatSource; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.NullSource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| @Tag(Tags.ROUND_TRIP) | ||
| class UuidConverterTest extends AbstractExcelTest { | ||
|
|
||
| private static final String TEXT = "123e4567-e89b-12d3-a456-426614174000"; | ||
| private final UUIDStringConverter converter = new UUIDStringConverter(); | ||
| private final GlobalConfiguration configuration = new GlobalConfiguration(); | ||
|
|
||
| @Test | ||
| void supportsUuidStringsAndRegistersAllLookupPaths() { | ||
| Assertions.assertEquals(UUID.class, converter.supportJavaTypeKey()); | ||
| Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey()); | ||
| Assertions.assertInstanceOf( | ||
| UUIDStringConverter.class, | ||
| DefaultConverterLoader.loadDefaultReadConverter() | ||
| .get(ConverterKeyBuild.buildKey(UUID.class, CellDataTypeEnum.STRING))); | ||
| Assertions.assertInstanceOf( | ||
| UUIDStringConverter.class, | ||
| DefaultConverterLoader.loadDefaultWriteConverter().get(ConverterKeyBuild.buildKey(UUID.class))); | ||
| Assertions.assertInstanceOf( | ||
| UUIDStringConverter.class, | ||
| DefaultConverterLoader.loadDefaultWriteConverter() | ||
| .get(ConverterKeyBuild.buildKey(UUID.class, CellDataTypeEnum.STRING))); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource( | ||
| strings = { | ||
| TEXT, | ||
| "123E4567-E89B-12D3-A456-426614174000", | ||
| "00000000-0000-0000-0000-000000000000", | ||
| "ffffffff-ffff-ffff-ffff-ffffffffffff" | ||
| }) | ||
| void readsAndWritesCanonicalStrings(String input) { | ||
| UUID value = converter.convertToJavaData(new ReadCellData<>(input), null, configuration); | ||
| WriteCellData<?> cell = converter.convertToExcelData(value, null, configuration); | ||
| Assertions.assertEquals(CellDataTypeEnum.STRING, cell.getType()); | ||
| Assertions.assertEquals(input.toLowerCase(Locale.ROOT), cell.getStringValue()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource( | ||
| strings = { | ||
| "not-a-uuid", | ||
| "123e4567-e89b-12d3-a456-42661417400g", | ||
| "123e4567e89b12d3a456426614174000", | ||
| " not-a-uuid\t", | ||
| "123e4567-e89b-12d3-a456-42661417 4000" | ||
| }) | ||
| void rejectsInvalidInput(String input) { | ||
| Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> converter.convertToJavaData(new ReadCellData<>(input), null, configuration)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @NullSource | ||
| @ValueSource(strings = {"", " ", "\t\r\n", "\u2003"}) | ||
| void readsBlankStringAsMissingValue(String input) { | ||
| ReadCellData<String> cell = new ReadCellData<>(); | ||
| cell.setStringValue(input); | ||
| Assertions.assertNull(converter.convertToJavaData(cell, null, configuration)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {" " + TEXT, TEXT + " ", "\t" + TEXT + "\r\n"}) | ||
| void trimsWhitespaceBeforeParsing(String input) { | ||
| Assertions.assertEquals( | ||
| UUID.fromString(TEXT), converter.convertToJavaData(new ReadCellData<>(input), null, configuration)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ExcelFormatSource | ||
| void roundTripsUuidAndNullFieldsWithoutCustomRegistration(ExcelFormat format) throws Exception { | ||
| File file = createTempFile(format); | ||
| UuidData first = new UuidData(); | ||
| first.setId(UUID.fromString(TEXT)); | ||
| first.setLabel("populated"); | ||
| UuidData second = new UuidData(); | ||
| second.setLabel("null UUID"); | ||
| List<UuidData> expected = Arrays.asList(first, second); | ||
| Assertions.assertEquals(expected, RoundTripHelper.writeAndRead(file, UuidData.class, expected)); | ||
| List<StringData> strings = RoundTripHelper.read(file, StringData.class); | ||
| Assertions.assertEquals(TEXT, strings.get(0).getId()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ExcelFormatSource | ||
| void readsUppercaseAndBlankCells(ExcelFormat format) throws Exception { | ||
| File file = createTempFile(format); | ||
| StringData first = new StringData(); | ||
| first.setId(TEXT.toUpperCase(Locale.ROOT)); | ||
| first.setLabel("uppercase"); | ||
| StringData second = new StringData(); | ||
| second.setId(""); | ||
| second.setLabel("blank UUID"); | ||
| StringData padded = new StringData(); | ||
| padded.setId(" \t" + TEXT + "\r\n"); | ||
| padded.setLabel("padded UUID"); | ||
| StringData whitespace = new StringData(); | ||
| whitespace.setId(" \t "); | ||
| whitespace.setLabel("whitespace UUID"); | ||
| RoundTripHelper.write(file, StringData.class, Arrays.asList(first, second, padded, whitespace)); | ||
| List<UuidData> rows = RoundTripHelper.read(file, UuidData.class); | ||
| Assertions.assertEquals(4, rows.size()); | ||
| Assertions.assertEquals(UUID.fromString(TEXT), rows.get(0).getId()); | ||
| Assertions.assertNull(rows.get(1).getId()); | ||
| Assertions.assertEquals(UUID.fromString(TEXT), rows.get(2).getId()); | ||
| Assertions.assertNull(rows.get(3).getId()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ExcelFormatSource | ||
| void reportsInvalidCellAsConversionFailure(ExcelFormat format) throws Exception { | ||
| File file = createTempFile(format); | ||
| StringData row = new StringData(); | ||
| row.setId("not-a-uuid"); | ||
| row.setLabel("invalid"); | ||
| RoundTripHelper.write(file, StringData.class, Collections.singletonList(row)); | ||
| ExcelDataConvertException error = Assertions.assertThrows( | ||
| ExcelDataConvertException.class, () -> RoundTripHelper.read(file, UuidData.class)); | ||
| Assertions.assertInstanceOf(IllegalArgumentException.class, error.getCause()); | ||
| } | ||
|
|
||
| @Data | ||
| public static class UuidData { | ||
| private UUID id; | ||
| private String label; | ||
| } | ||
|
|
||
| @Data | ||
| public static class StringData { | ||
| private String id; | ||
| private String label; | ||
| } | ||
| } | ||
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.
Please add a
@Tag(Tags.ROUND_TRIP)