use of io.micronaut.nats.bind.NatsHeaderConvertibleValues in project micronaut-nats by micronaut-projects.
the class ProductInfoTypeBinder method bind.
@Override
public BindingResult<ProductInfo> bind(ArgumentConversionContext<ProductInfo> context, Message source) {
// <4>
Headers rawHeaders = source.getHeaders();
if (rawHeaders == null) {
return BindingResult.EMPTY;
}
NatsHeaderConvertibleValues headers = new NatsHeaderConvertibleValues(rawHeaders, conversionService);
// <5>
String size = headers.get("productSize", String.class).orElse(null);
// <6>
Optional<Long> count = headers.get("x-product-count", Long.class);
// <7>
Optional<Boolean> sealed = headers.get("x-product-sealed", Boolean.class);
if (headers.getConversionErrors().isEmpty() && count.isPresent() && sealed.isPresent()) {
// <8>
return () -> Optional.of(new ProductInfo(size, count.get(), sealed.get()));
} else {
return new BindingResult<ProductInfo>() {
@Override
public Optional<ProductInfo> getValue() {
return Optional.empty();
}
@Override
public List<ConversionError> getConversionErrors() {
// <9>
return headers.getConversionErrors();
}
};
}
}
Aggregations