Skip to content
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

[ClientCore] Introduce Union type #43778

Merged
merged 26 commits into from
Jan 31, 2025
Merged

[ClientCore] Introduce Union type #43778

merged 26 commits into from
Jan 31, 2025

Conversation

mssfang
Copy link
Member

@mssfang mssfang commented Jan 13, 2025

This PR brings the idea of Union type to the client core:

A Union type could be one of the mixed types. For example,

1, Create an instance of mixed types, Integer, Double, and String, you can use the following code

Union.ofTypes(String.class, Integer.class); 
// or primitive types
Union.ofTypes(int.class, double.class); 
// or Paramatered types (like collections)
Union.ofTypes(new GenericParameterizedType(List.class, String.class), new GenericParameterizedType(List.class, Integer.class))

2, Set the value

union.setValue("Hello");

3, How to consume the value:

3.1, In Java 17+, use the switch:

switch (union.getValue()) {
    case String s -> System.out.println("String value: " + s);
    case Integer i -> System.out.println("Integer value: " + i);
    default -> throw new IllegalArgumentException(
        "Unknown type: " + union.getType().getTypeName());
}

or use if-else:

if (value instanceof String) {
    String s = (String) value;
    System.out.println("String value: " + s);
} else if (value instanceof Integer) {
    Integer i = (Integer) value;
    System.out.println("Integer value: " + i);
} else {
    throw new IllegalArgumentException("Unknown type: " + union.getCurrentType().getTypeName());
}

3.2, use the tryConsume lambda method:

union.tryConsume(v -> System.out.println("String value: " + v), String.class);

@azure-sdk
Copy link
Collaborator

API change check

APIView has identified API level changes in this PR and created following API reviews.

io.clientcore:core

@mssfang mssfang requested a review from alzimmermsft January 23, 2025 21:06
@mssfang mssfang requested a review from srnagar January 27, 2025 20:23
@mssfang mssfang requested a review from alzimmermsft January 28, 2025 06:04
@mssfang mssfang merged commit e745564 into Azure:main Jan 31, 2025
18 checks passed
@mssfang mssfang deleted the ClientCore-Union branch January 31, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants