Search in sources :

Example 1 with ReqlLambda

use of com.rethinkdb.model.ReqlLambda in project MantaroBot by Mantaro.

the class Util method toReqlAst.

@SuppressWarnings("unchecked")
private static ReqlAst toReqlAst(Object val, int remainingDepth) {
    if (remainingDepth <= 0) {
        throw new ReqlDriverCompileError("Recursion limit reached converting to ReqlAst");
    }
    if (val instanceof ReqlAst) {
        return (ReqlAst) val;
    }
    if (val instanceof Object[]) {
        Arguments innerValues = new Arguments();
        for (Object innerValue : Arrays.asList((Object[]) val)) {
            innerValues.add(toReqlAst(innerValue, remainingDepth - 1));
        }
        return new MakeArray(innerValues, null);
    }
    if (val instanceof List) {
        Arguments innerValues = new Arguments();
        for (Object innerValue : (List) val) {
            innerValues.add(toReqlAst(innerValue, remainingDepth - 1));
        }
        return new MakeArray(innerValues, null);
    }
    if (val instanceof Map) {
        Map<String, ReqlAst> obj = new MapObject<>();
        for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) val).entrySet()) {
            if (!(entry.getKey() instanceof String)) {
                throw new ReqlDriverCompileError("Object keys can only be strings");
            }
            obj.put((String) entry.getKey(), toReqlAst(entry.getValue()));
        }
        return MakeObj.fromMap(obj);
    }
    if (val instanceof ReqlLambda) {
        return Func.fromLambda((ReqlLambda) val);
    }
    final DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
    if (val instanceof LocalDateTime) {
        ZoneId zid = ZoneId.systemDefault();
        DateTimeFormatter fmt2 = fmt.withZone(zid);
        return Iso8601.fromString(((LocalDateTime) val).format(fmt2));
    }
    if (val instanceof ZonedDateTime) {
        return Iso8601.fromString(((ZonedDateTime) val).format(fmt));
    }
    if (val instanceof OffsetDateTime) {
        return Iso8601.fromString(((OffsetDateTime) val).format(fmt));
    }
    if (val instanceof Number || val instanceof Boolean || val instanceof String) {
        return new Datum(val);
    }
    if (val == null) {
        return new Datum(null);
    }
    // val is a non-null POJO, let's introspect its public properties
    return toReqlAst(toMap(val));
}
Also used : LocalDateTime(java.time.LocalDateTime) ReqlDriverCompileError(com.rethinkdb.gen.exc.ReqlDriverCompileError) ZoneId(java.time.ZoneId) Arguments(com.rethinkdb.model.Arguments) ReqlLambda(com.rethinkdb.model.ReqlLambda) ZonedDateTime(java.time.ZonedDateTime) OffsetDateTime(java.time.OffsetDateTime) MapObject(com.rethinkdb.model.MapObject) List(java.util.List) Map(java.util.Map) MapObject(com.rethinkdb.model.MapObject) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

ReqlDriverCompileError (com.rethinkdb.gen.exc.ReqlDriverCompileError)1 Arguments (com.rethinkdb.model.Arguments)1 MapObject (com.rethinkdb.model.MapObject)1 ReqlLambda (com.rethinkdb.model.ReqlLambda)1 LocalDateTime (java.time.LocalDateTime)1 OffsetDateTime (java.time.OffsetDateTime)1 ZoneId (java.time.ZoneId)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 List (java.util.List)1 Map (java.util.Map)1