@SuppressWarnings("unchecked")
  public static <T extends Comparable<? super T>> Range<Comparable<Object>> writable(
      Encoder<T> encoder,
      Range<T> range) {
    if (range.hasLowerBound() && range.hasUpperBound()) {
      return Range.range(
          (Comparable<Object>) writable(encoder, range.lowerEndpoint()),
          range.lowerBoundType(),
          (Comparable<Object>) writable(encoder, range.upperEndpoint()),
          range.upperBoundType());
    } else if (range.hasLowerBound()) {
      return Range.downTo(
          (Comparable<Object>) writable(encoder, range.lowerEndpoint()),
          range.lowerBoundType());
    } else if (range.hasUpperBound()) {
      return Range.upTo(
          (Comparable<Object>) writable(encoder, range.upperEndpoint()),
          range.upperBoundType());
    }
    throw new AssertionError();
  }
Builds bson with index definition. The difference with
{@link #convertToBson(Constraints.ConstraintHost)} is that this method fails on duplicate
fields. Currently only one index is allowed per field.
@see <a href="https://docs.mongodb.com/manual/indexes/">Mongo Indexes</a>
mongo/src/org/immutables/mongo/repository/internal/Support.java