Skip to content
Ravi Chodavarapu edited this page Jun 9, 2016 · 3 revisions

datamill offers a fluent API for dealing with values of various types. A primary feature offered by this API is the ability to easily cast between different types. Consider for example HTTP request URI parameters:

    rb.ifMethodAndUriMatch(Method.GET, "/users/{id}", request ->
        request.uriParameter("id").asLong() == 0 ? 
            request.respond().ok() : 
            request.respond().notFound()));

Here, the request URI parameter "id", retrieved using request.uriParameter("id") returns a foundation.stack.datamill.values.Value, the main interface for the values API. This URI parameter, as with all URI parameters, is originally a string value. But notice that it is easily converted into a long value using the asLong() method. There is a method to quickly cast to all the primitive types. Another example of where values are returned is when you access database row data:

    dbClient.select(outline.propertyNames()).from(outline.pluralName()).where()
        .eq(outline.name(outline.members().getId()), request.uriParameter("id").asLong())
    .map(row -> row.column(outline.name(outline.members().isActive())).asBoolean())
    .flatMap(active -> active ? 
         r.respond.ok("Active") :
         r.respond.ok("Not Active"))));

Note that when we retrieve the value of the "active" column using row.column(outline.name(outline.members().isActive())), we get back a value, which we cast to a boolean with a call to asBoolean(). In this case, the database type may have originally been a boolean, or it may have been an integer type - by using asBoolean(), we can perform the cast safely and easily. In any location where the ability to perform this easy casting is appropriate, Values are used within the framework.

Clone this wiki locally