首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebFlux条件FlatMap

WebFlux条件FlatMap
EN

Stack Overflow用户
提问于 2019-08-24 04:22:58
回答 1查看 5.9K关注 0票数 3

我对网络流量比较陌生,当有条件时,我想找出避免嵌套flatMap的解决方案:

我有三个简单的实体:

物品,品牌,类别。

项目主要包括: brandId和categoryId

代码语言:javascript
复制
public Mono<Item> patch(String itemId, PatchSpecs specs) {
return itemRepository.findById(itemId)
        .switchIfEmpty(Mono.error(..)))
        .flatMap(item -> {
            final String brandId = specs.getBrandId();
            if (brandId != null) {
                return brandService.getById(brandId)
                        .switchIfEmpty(Mono.error(..)))
                        .flatMap(brand -> {
                            final String categoryId = specs.getCategoryId();
                            if (categoryId != null) {
                               return categoryService.getById(categoryId)... -> return updateAndSave(..)
                            }
                            return updateAndSave(specs, item, brand, null);
                        });
            }
            else {
                final String categoryId = specs.getCategoryId();
                if (categoryId != null) {
                     return categoryService.getById(categoryId)... -> return updateAndSave(..)
                }
                return updateAndSave(specs, item, null, null);
            }

        });
}

如何防止这种分支条件flatMaps混乱?我无法想象如果我有另一个实体在里面的项目。还会有更多的嵌套flatMaps吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-24 10:44:23

如果我很好地理解你的代码,类别和品牌是项目的可选属性。在这种情况下,我建议如下:

代码语言:javascript
复制
public Mono<Item> patch(String itemId, PatchSpecs specs)
{
    return itemRepository.findById(itemId)
                         .switchIfEmpty(Mono.error(new RuntimeException("Can not find item.")))
                         .flatMap(item -> updateAndSave(specs, item));
}

private Mono<? extends Item> updateAndSave(PatchSpecs specs, Item item)
{
    Mono<Optional<Brand>> brand = getBrand(specs.getBrandId());
    Mono<Optional<Category>> category = getCategory(specs.getCategoryId());

    return Mono.zip(Mono.just(item), brand, category)
               .flatMap(tuple -> updateAndSave(specs, tuple));
}

private Mono<Optional<Brand>> getBrand(String inputBrandId)
{
    return Mono.justOrEmpty(inputBrandId)
               .flatMap(brandId -> brandService.getById(brandId)
                                               .switchIfEmpty(Mono.error(new RuntimeException("Can not find brand."))))
               .map(Optional::of)
               .defaultIfEmpty(Optional.empty());
}

private Mono<Optional<Category>> getCategory(String inputCategoryId)
{
    return Mono.justOrEmpty(inputCategoryId)
               .flatMap(brandId -> categoryService.getById(brandId)
                                                  .switchIfEmpty(Mono.error(new RuntimeException("Can not find brand."))))
               .map(Optional::of)
               .defaultIfEmpty(Optional.empty());
}

private Mono<Item> updateAndSave(PatchSpecs specs, Tuple3<Item, Optional<Brand>, Optional<Category>> tuple)
{
    Item item = tuple.getT1();
    Brand brand = tuple.getT2().orElse(null);
    Category category = tuple.getT3().orElse(null);

    // TODO do update and save here
    return null;
}

这样,它将更加可扩展,并且不需要重复和嵌套条件。请确认它是否正常工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57635069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档