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

Add method to Arguments for getting payloads #4144

Closed
1 task
onacit opened this issue Nov 21, 2024 · 5 comments
Closed
1 task

Add method to Arguments for getting payloads #4144

onacit opened this issue Nov 21, 2024 · 5 comments

Comments

@onacit
Copy link

onacit commented Nov 21, 2024

Say, I have the following methods.

    class Some {

        static Stream<Arguments> getNameAndAgeArgumentsStream() {
            return Stream.of(
                    Arguments.of(
                            Named.of("Whatever", "John"), // <<<<<<<<<<<<<<<<<
                            1
                    );
        }
    }

    class Other {

        static Stream<Arguments> getNameAndAgeArgumentsStream() {
            return Stream.of(
                    Arguments.of(
                            "Jane",  // <<<<<<<<<<<<<<<<<<<<<<<
                            1
                    );
        }
    }

Now I want to use those two methods in other class.

    class Another {

        static Stream<Arguments> getPrefixedNameAndAgeArguemntsStream() {
            return Stream.concat(
                            Some.getNameAndAgeArgumentsStream(),
                            Other.getNameAndAgeArgumentsStream()
                    )
                    .map(a -> {
                        var arguments = a.get();
                        var name = arguments[0] instanceof Named ? ((Named<String>) arguments[0]).getPayload() : arguments[0];
                        return Arguments.of(
                                "prefix" + name,
                                arguments[1]
                        );
                    });
        }
    }

I had to use a method looks like this.

    public static <T> T payload(final Object argument) {
        Objects.requireNonNull(argument, "argument is null");
        if (argument instanceof Named<?>) {
            return payload(((Named<?>) argument).getPayload());
        }
        return (T) argument;
    }

Deliverables

  • Add a method returns ultimate payload values from an Arguments, say, getPayloads()[Ljava.lang.Object
@marcphilipp
Copy link
Member

This is what we currently do to unwrap Named arguments:

private Object[] extractPayloads(Object[] arguments) {
return Arrays.stream(arguments) //
.map(argument -> {
if (argument instanceof Named) {
return ((Named<?>) argument).getPayload();
}
return argument;
}) //
.toArray();
}

I think we could expose that as a static Object[] Named.unwrap(Object[]) method on Named.

@onacit Would that be sufficient for you?

@onacit
Copy link
Author

onacit commented Nov 25, 2024

@marcphilipp That's great! Thank you.

  • Don't we have, you know, a Named<?> wraps another Named<?>?

If I may, let me share what I've done.

    // _Named_TestUtils.java
    public static <T> T payload(final Object argument) {
        Objects.requireNonNull(argument, "argument is null");
        if (argument instanceof Named<?>) {
            return payload( // <<<<<<<<<<< peel it off until it's not a `Named<?>` anymore
                    ((Named<?>) argument).getPayload()
            );
        }
        @SuppressWarnings({"unchecked"})
        final var cast = (T) argument;
        return cast;
    }
    // _Arguments_TestUtils.java
    public static Arguments ofPayloadsMapped(final Arguments arguments,
                                             final IntFunction<? extends Function<Object, Object>> mapper) {
        Objects.requireNonNull(arguments, "arguments is null");
        final var got = arguments.get();
        return Arguments.of(
                IntStream.range(0, got.length)
                        .mapToObj(i -> mapper.apply(i).apply(_Named_TestUtils.payload(got[i])))
                        .toArray()
        );
    }

    public static Arguments ofPayloads(final Arguments arguments) {
        return ofPayloadsMapped(arguments, i -> Function.identity());
    }
    // may gone to far, but worth it!
    static Stream<Arguments> getCipherAndParamsArgumentsStream() {
        return Stream.concat(
                JinahyaBlockCipherUtils_AES_Test.getCipherAndParamsArgumentsStream(),
                JinahyaBlockCipherUtils_ARIA_Test.getCipherAndParamsArgumentsStream()
        ).map(a -> _Arguments_TestUtils.ofPayloadsMapped( // peel off while remapping into, say, another `Named<?>`
                a,
                i -> p -> switch (i) {
                    case 0 -> _BufferedBlockCipher_TestUtils.named(new CTSBlockCipher((BlockCipher) p));
                    case 1 -> _KeyParameters_TestUtils.named((KeyParameter) p);
                    default -> p;
                }
        ));
    }

@marcphilipp
Copy link
Member

When would Named be wrapping an argument multiple times?

@onacit
Copy link
Author

onacit commented Nov 25, 2024

When would Named be wrapping an argument multiple times?

You're right. Thanks.

@onacit onacit closed this as completed Nov 25, 2024
@marcphilipp
Copy link
Member

Why did you close the issue? Don't you want the method I suggested in #4144 (comment) to be added?

@marcphilipp marcphilipp closed this as not planned Won't fix, can't repro, duplicate, stale Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants