Giter Club home page Giter Club logo

dhall-eta's Introduction

CircleCI

dhall-eta is a eta library that wraps the haskell implementation of dhall configuration language with a friendly java api.

Its main goal is to create a language binding to make possible configure java libraries and applications with dhall files.

We already can use eta to compile the dhall haskell implementation and etlas (a specific build tool for eta) to install dhall and its tools (dhall-to-json or dhall-text) as java cli applications. However, the classes generated by eta can't be used directly in java code in a easy way. Hence the need for this library.

Table of contents

API Overview

  • The quick way to explore the java api is use jshell the java repl tool present since java 9.
    • To start a session with the library in the classpath you can do:
> jshell --class-path /path/to/dhall-eta-all-${version}.jar

NOTE: The version of java used is openjdk-9.0.4.

High level api

The main class to compile dhall expressions and get java objects is the class org.dhall.eta.Input.

  • The class has methods to compile simple types and convert them to java:
jshell> import org.dhall.eta.Input

jshell> Input.bool("True")
$2 ==> true

jshell> Input.str("\"This is a dhall Text\"")
$3 ==> "This is a dhall Text"

jshell> Input.bigInt("-1234567890")
$4 ==> -1234567890

jshell> Input.natural("123456789 * 123456789")
$5 ==> Natural [15241578750190521]
  • Those are shorthands for the main method Input.type. To use it we need to build a org.dhall.eta.Type<R> used by the api to extract a java value (of type R) and typecheck the dhall expression. Methods for basic types are already defined in org.dhall.eta.Types:
jshell> import org.dhall.eta.Type

jshell> import org.dhall.eta.Types

jshell> Type<Boolean> boolTy = Types.bool()
boolTy ==> eta.org.dhall.eta.Type$Eta@67bf0d91

jshell> Input.type(boolTy, "True && False")
$6 ==> false
  • The class org.dhall.eta.Types has methods to build standard "container" types as List, Optional or Map.
    • For extract a java Map from a dhall record, we need to build a Map with the keys and types of the fields expected and then use Types.objMap to get a Type<Map<String, Object>> suitable to use with Input.type:
jshell> Map<String,Type<? extends Object>> fieldTypes=new HashMap<>();
fieldTypes ==> {}

jshell> fieldTypes.put("name",Types.str());
$9 ==> null

jshell> fieldTypes.put("nats",Types.list(Types.natural()))
$10 ==> null

jshell> Type<Map<String,Object>> mapTy=Types.objMap(FieldTypes.upcast(fieldTypes))
mapTy ==> eta.org.dhall.eta.Type$Eta@35884aff

jshell> Input.type(mapTy, "{ name = \"name\", nats=[1, 2, 3] }")
$11 ==> {name=name, nats=[Natural[1], Natural[2], Natural[3]]}
  • Of course you can use the api to create an arbitrary java object from dhall code. We should use the generic Input.type method and create the appropiate type:

Low level api

Building

Etlas

  • Download etlas from https://eta-lang.org/docs/user-guides/eta-user-guide/installation/etlas

    • WARNING: Actually dhall-eta can only be built using eta and etlas built themselves from master. We are in the cutting edge!
  • Run etlas build --enable-uberjar-mode to compile and generate an uberjar with all dependecies included in ./dist/build/eta-${version}/dhall-eta-${version}/x/dhall-eta-all/build/dhall-eta-all/dhall-eta-all.jar

    • The output jar is not minimized and include an example executable with etlas run dhall-eta-all
  • Run etlas test to execute the test suite against dhall-lang acceptance tests

    • As a previous step you'll need to checkout dhall-lang project in the same parent directory as dhall-eta
    • Current dhall standard version supported is 4.0.0 and we have to use a fixed version of tests for 4.0.0 in https://github.com/jneira/dhall-lang
    • So the command to get them could be (being in the dhall-eta project dir):
> git clone --branch v4.0.0 --depth=1 https://github.com/jneira/dhall-lang.git ../dhall-lang
  • The test suite does not check the validity of dhall-haskell (and it should not do it!), only that the dhall-haskell implementation compiled by eta and dhall-eta itself are consistent.

Gradle

  • WARNING: For now, the gradle build is configured to use the system wide versions of eta and etlas so you will must have them built from source and available in PATH. We are in the cutting edge here too!
  • This is the preferred method to build the project and the used one to generate the artifacts needed for a release of the library.
  • It uses the gradle plugin for eta and the proguard and shadow ones to generate a minimized jar with all the dependencies included.
  • It is recommended to use the gradle wrapper cause the eta gradle plugin will likely work with it.
  • So ./gradlew build will generate the following artifacts:
    • A minimized uberjar ready to be used standalone in ./lib/dhall-eta-${version}-all.jar
    • An artefact with the objects representing the core business model of dhall. In this one will not be any code for actually parse, normalize o compile dhall code.
      • The package root name has not reference to eta: it is simply org.dhall.
      • The code is in a gradle subproject named dhall-model in the folder ./java
      • The artifact generated is ./java/build/libs/dhall-model-${version}.jar
    • An artefact with the java executable example org.dhall.eta.examples.Client.
      • The code is in a gradle subproject named dhall-eta-examples in the folder examples
      • You can run it with ./gradlew run.

dhall-eta's People

Contributors

jneira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dhall-eta's Issues

Publish dhall-eta-all.jar in maven central

  • Mentioned in twitter by @talios
  • It would make dramatically easier to use in existing java/jvm projects
  • It has the downside that it includes classes from eta, its core libraries, other haskell libraries and the apache bouncycastle library and use it with other eta projects or versions of bouncycastle could drive to incompabilities between different version of those classes.
    • So we should make clear that it should be used in isolation with other eta projects
  • Afaiu maven central requires demonstrate the owner of the url that represents the group id (so if we want to use as group id org.dhall-lang f.e., we'll need the intervention of the https://dhall-lang.org owners)

Make the api fully polymorphic in the java side

  • The haskell implementation of Normalize if polymorphic on both type parameters s and a
  • Unfortunately the java wrapper cant "thread" the polymorphism from java to haskell using the foreign exports, So we have to write a method for each possible value of s and a and cant use the Normalizer or the TypeChecker (see typelead/eta#886)
  • We should:

Update README with a more detailed usage section

Now it only suggests download the uberjar. It should mention:

  • Include the library in a eta application via etlas, adding it int .cabalor etlas.dhall config file
  • Include the library in a mixed project java/eta via eta gradle plugin, adding the dependency in build.gradle
  • Comment about the caveats of the uberjar:
    • It includes classes from the bouncycastle java lib, so it can produce Class loading issues if bouncycastleis already in the classpath woth another version
    • Although not probable for now, it can clause clashes in eta internal classes if another version of eta is in the classpath

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.