Skip to content

Commit 73f6bc6

Browse files
committed
feat: enhance FriendlyIds.toUuid to accept both UUID and FriendlyId formats
- toUuid() now auto-detects input format (UUID or FriendlyId) - Simplify FriendlyId.parse() to delegate to FriendlyIds.toUuid() - Update documentation
1 parent 184724b commit 73f6bc6

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ This converts a UUID in the form of a string to a FriendlyID, for example: `5wbw
133133

134134
```java
135135
FriendlyIds.toUuid("5wbwf6yUxVBcr48AMbz9cb");
136+
// or
137+
FriendlyIds.toUuid("c3587ec5-0976-497f-8374-61e0c2ea3da5");
136138
```
137139

138-
This converts a FriendlyID to its UUID, for example: `c3587ec5-0976-497f-8374-61e0c2ea3da5`
140+
This converts a FriendlyID or UUID string to UUID. Both formats are accepted.
139141

140142

141143
Notes

friendly-id-openfeign/src/main/java/com/devskiller/friendly_id/openfeign/FriendlyIdDecoder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.UUID;
66

77
import com.devskiller.friendly_id.FriendlyIds;
8+
import com.devskiller.friendly_id.type.FriendlyId;
89

910
import feign.FeignException;
1011
import feign.Response;
@@ -24,7 +25,7 @@
2425
* </p>
2526
* <ul>
2627
* <li>FriendlyId string → {@link UUID}</li>
27-
* <li>FriendlyId string → {@link com.devskiller.friendly_id.type.FriendlyId}</li>
28+
* <li>FriendlyId string → {@link FriendlyId}</li>
2829
* </ul>
2930
*
3031
* @see FriendlyIdEncoder
@@ -46,8 +47,8 @@ public Object decode(Response response, Type type) throws IOException, DecodeExc
4647
return FriendlyIds.toUuid(stringValue);
4748
}
4849

49-
if (type == com.devskiller.friendly_id.type.FriendlyId.class && decoded instanceof String stringValue) {
50-
return parse(stringValue);
50+
if (type == FriendlyId.class && decoded instanceof String stringValue) {
51+
return FriendlyId.parse(stringValue);
5152
}
5253

5354
return decoded;

friendly-id/src/main/java/com/devskiller/friendly_id/FriendlyIds.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,31 @@ public static String toFriendlyId(UUID uuid) {
5050
}
5151

5252
/**
53-
* Decodes a FriendlyId string to UUID.
53+
* Converts a string to UUID, accepting both UUID and FriendlyId formats.
54+
* <p>
55+
* This method auto-detects the format:
56+
* <ul>
57+
* <li>Standard UUID format (36 chars with hyphens): parsed directly</li>
58+
* <li>FriendlyId format (up to 22 chars): decoded from Base62</li>
59+
* </ul>
5460
*
55-
* @param friendlyId encoded UUID
56-
* @return decoded UUID
61+
* @param value UUID or FriendlyId string
62+
* @return parsed UUID
63+
* @throws IllegalArgumentException if value is not a valid UUID or FriendlyId
5764
*/
58-
public static UUID toUuid(String friendlyId) {
59-
return Url62.decode(friendlyId);
65+
public static UUID toUuid(String value) {
66+
if (isStandardUuidFormat(value)) {
67+
return UUID.fromString(value);
68+
}
69+
return Url62.decode(value);
70+
}
71+
72+
private static boolean isStandardUuidFormat(String value) {
73+
return value.length() == 36
74+
&& value.charAt(8) == '-'
75+
&& value.charAt(13) == '-'
76+
&& value.charAt(18) == '-'
77+
&& value.charAt(23) == '-';
6078
}
6179

6280
}

friendly-id/src/main/java/com/devskiller/friendly_id/type/FriendlyId.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Objects;
55
import java.util.UUID;
66

7+
import com.devskiller.friendly_id.FriendlyIds;
8+
79
/**
810
* Value object representing a FriendlyId that wraps a UUID.
911
* <p>
@@ -108,21 +110,7 @@ public static FriendlyId friendlyId(UUID uuid) {
108110
*/
109111
public static FriendlyId parse(String value) {
110112
Objects.requireNonNull(value, "Value cannot be null");
111-
if (isStandardUuidFormat(value)) {
112-
return new FriendlyId(UUID.fromString(value));
113-
}
114-
return new FriendlyId(com.devskiller.friendly_id.FriendlyIds.toUuid(value));
115-
}
116-
117-
/**
118-
* Checks if the string matches standard UUID format (36 chars with hyphens).
119-
*/
120-
private static boolean isStandardUuidFormat(String value) {
121-
return value.length() == 36
122-
&& value.charAt(8) == '-'
123-
&& value.charAt(13) == '-'
124-
&& value.charAt(18) == '-'
125-
&& value.charAt(23) == '-';
113+
return new FriendlyId(FriendlyIds.toUuid(value));
126114
}
127115

128116
/**
@@ -162,7 +150,7 @@ public UUID uuid() {
162150
* @return the FriendlyId string
163151
*/
164152
public String value() {
165-
return com.devskiller.friendly_id.FriendlyIds.toFriendlyId(uuid);
153+
return FriendlyIds.toFriendlyId(uuid);
166154
}
167155

168156
/**

0 commit comments

Comments
 (0)