micronaut petstore 从 java 到 groovy 的代码段

micronaut petstore a code segment from java to groovy

实际代码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));
                    });
        };
    }

我尝试了多种不同的方式将上面的内容转换为 groovy,但到目前为止所有的尝试都不太奏效。我想知道是否有更好的 groovy 可以帮助

我的尝试没有 posted,因为代码本身首先 returns Intelij 中的代码块不明确 ,其次看起来完全错误。

 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)});
        }});
    }}
}

在尝试转换为 groovy 时,最大的困难似乎在于:

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

这与原始 java 代码的样子完全不同,而且真的不确定它是否会按预期运行。我遇到的问题是在 groovy 中找到任何关于 RXJAVA Flux .reduce 的内容。

有歧义的代码块围绕着最底部的整个 flatMap 段

 .flatMap({entries -> {

我没有签入此更改,也没有 post 因为坦率地说它很尴尬。

我也遇到过: http://reactivex.io/documentation/operators/reduce.html#collapseRxGroovy

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

最后是:

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)})

但这又看起来是错误的,并且与 java 代码的作用不符。

最后编辑建议我让 Intelij 接受代码作为 groovy 但不太确定它是否是 java 代码实际在做什么,因为声明的地图甚至没有使用:

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)});

}

您遇到的问题可能是因为 Groovy 不支持 Java 方法引用或 lambda。

第一行返回一个 lambda

Java: return key -> {

Groovy: return { key - >

那是使用一个 groovy 以键为参数的闭包。

其他使用方法引用的地方需要转换

Java: .map(ConvertibleValues::of)

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

看起来你已经解决了大部分问题,但是你特别询问了未使用的地图。那是因为您根本没有将它传递给方法。

values.reduce({all, keyValue-> 对比 values.reduce(map, {all, keyValue ->