Search in sources :

Example 1 with ConversionError

use of io.micronaut.core.convert.ConversionError 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();
            }
        };
    }
}
Also used : NatsHeaderConvertibleValues(io.micronaut.nats.bind.NatsHeaderConvertibleValues) Headers(io.nats.client.impl.Headers) ConversionError(io.micronaut.core.convert.ConversionError)

Aggregations

ConversionError (io.micronaut.core.convert.ConversionError)1 NatsHeaderConvertibleValues (io.micronaut.nats.bind.NatsHeaderConvertibleValues)1 Headers (io.nats.client.impl.Headers)1