简体   繁体   English

micronaut petstore从Java到groovy的代码段

[英]micronaut petstore a code segment from java to groovy

The actual code is here 实际的代码在这里

private Function<String, Mono<? extends Offer>> keyToOffer(RedisReactiveCommands<String, String> commands) {
        return key -> {
            Flux<KeyValue<String, String>> values = commands.hmget(key, "price", "description");
            Map<String, String> map = new HashMap<>(3);
            return values.reduce(map, (all, keyValue) -> {
                all.put(keyValue.getKey(), keyValue.getValue());
                return all;
            })
                    .map(ConvertibleValues::of)
                    .flatMap(entries -> {
                        String description = entries.get("description", String.class).orElseThrow(() -> new IllegalStateException("No description"));
                        BigDecimal price = entries.get("price", BigDecimal.class).orElseThrow(() -> new IllegalStateException("No price"));
                        Flowable<Pet> findPetFlowable = petClient.find(key).toFlowable();
                        return Mono.from(findPetFlowable).map(pet -> new Offer(pet, description, price));
                    });
        };
    }

I have tried in a variety of different ways to convert above into groovy and all attempts so far not worked out too well. 我已经尝试了各种不同的方法将上述转换为常规,到目前为止,所有尝试都还不太顺利。 I wondered if anyone better with groovy could help 我想知道是否有更好的使用Groovy的人可以帮助您

My attempt wasn't posted since the code itself firstly returns Ambiguous code block in Intelij and secondly looks totally wrong. 我的尝试没有发布,因为代码本身首先在Intelij中返回Ambiguous代码块 ,其次看起来完全错误。

 private Function<String, Mono<? extends Orders>> keyToOrder(RedisReactiveCommands<String, String> commands) {
    return { key -> {
        Flux<KeyValue<String, String>> values = commands.hmget(key, "price", "description");
        Map map = [:]
        return values.reduce(map, (all, keyValue)= {all.put(keyValue.getKey(), keyValue.getValue());
            return all
        }).map({entries -> ConvertibleValues.of(entries)})
                .flatMap({entries -> {
            String description = entries.get("description", String.class).orElseThrow({ new IllegalStateException("No description")});
            BigDecimal price = entries.get("price", BigDecimal.class).orElseThrow({new IllegalStateException("No price")});
            Flowable<Item> findItemFlowable = itemClient.find(key).toFlowable();
            return Mono.from(findItemFlowable).map({item -> new Orders(item, description, price)});
        }});
    }}
}

When attempting to convert to groovy the biggest struggle appeared to be around: 当试图转换为常规时,最大的挣扎似乎在周围:

return values.reduce(map, (all, keyValue)= {all.put(keyValue.getKey(), keyValue.getValue());
                    return all

This is nothing like what the original java code looked like and really unsure if it would even behave as it should. 这与原始Java代码的外观完全不同,并且真的不确定它是否会按预期运行。 The issue I had was to find anything aorund RXJAVA Flux .reduce written in groovy out there. 我遇到的问题是找到任何以常规方式编写的RXJAVA Flux .reduce。

The Ambiguous code block is around this entire flatMap segment at the very bottom 模糊代码块位于底部的整个flatMap段中

 .flatMap({entries -> {

I haven't checked in this change nor did I post it since frankly it was embarrassing. 坦率地说,这很尴尬,我没有检查过此更改,也没有发布。

I have also come across: http://reactivex.io/documentation/operators/reduce.html#collapseRxGroovy 我也遇到过: http : //reactivex.io/documentation/operators/reduce.html#collapseRxGroovy

numbers.reduce({ a, b -> a+b }).

and ended up with: 并最终得到:

Map<String, String> map = new HashMap<>(3);
            return values.reduce({all, keyValue->
                all.put(keyValue.getKey(), keyValue.getValue());
                return all
        }).map({entries -> ConvertibleValues.of(entries)})

But this again looks wrong and doesn't really match what the java code was doing. 但是,这看起来又是错误的,并且与Java代码所做的并不完全匹配。

Final edit to suggest I have got Intelij to accept the code as groovy but not quite sure if it is what the java code was actually doing, since declared map is not even used: 最后的编辑建议我让Intelij接受该代码,但不能完全确定这是否是Java代码的实际功能,因为甚至没有使用声明的map:

private Function<String, Mono<? extends Orders>> keyToOrder(RedisReactiveCommands<String, String> commands) {
    Flux<KeyValue<String, String>> values = commands.hmget(key, "price", "description");
        Map<String, String> map = new HashMap<>(3);
        values.reduce({all, keyValue->
            all.put(keyValue.getKey(), keyValue.getValue());
            return all
    }).map({entries -> ConvertibleValues.of(entries)})
                .flatMap({entries ->  bindEntry(entries)});
    return values.key
}

private Mono<Orders> bindEntry(entries) {
    String description = entries.get("description", String.class).orElseThrow({ new IllegalStateException("No description")});
    BigDecimal price = entries.get("price", BigDecimal.class).orElseThrow({new IllegalStateException("No price")});
    Flowable<Item> findItemFlowable = itemClient.find(key).toFlowable();
    return Mono.from(findItemFlowable).map({item -> new Orders(item, description, price)});

}

The likely issue you're facing is because Groovy doesn't support Java method references or lambdas. 您面临的可能问题是因为Groovy不支持Java方法引用或lambda。

The first line is returning a lambda 第一行返回一个lambda

Java: return key -> { Java: return key -> {

Groovy: return { key - > Groovy: return { key - >

That is using a groovy closure that takes the key as the argument. 那是使用以键为参数的常规闭包。

Other places that use method references need to be converted 使用方法引用的其他地方需要转换

Java: .map(ConvertibleValues::of) Java: .map(ConvertibleValues::of)

Groovy: .map({ values -> ConvertibleValues.of(values) }) Groovy: .map({ values -> ConvertibleValues.of(values) })

It seems like you have most of that worked out, however you specifically asked about the map not being used. 似乎您已经完成了大部分工作,但是您特别询问了未使用地图的情况。 That is because you simply aren't passing it to the method. 那是因为您根本没有将其传递给方法。

values.reduce({all, keyValue-> vs values.reduce(map, {all, keyValue -> values.reduce({all, keyValue->values.reduce(map, {all, keyValue ->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM