Skip to content

Commit

Permalink
Updates Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
solomon-b committed Sep 28, 2021
1 parent b90708d commit 5c2715c
Showing 1 changed file with 65 additions and 21 deletions.
86 changes: 65 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,53 @@

[![kriti-lang::CI](https://github.com/hasura/kriti-lang/actions/workflows/haskell.yml/badge.svg)](https://github.com/hasura/kriti-lang/actions/workflows/haskell.yml)

A minimal json templating language based on Go' template language.

## TODO
- Improve and extend lexer errors
- Pretty printer
- Improve span generation
- Array membership parsing
- CLI interface
- List flatten operation

## Uses
This library exposes the function `runKriti`, the type definition of the function is:
A minimal json templating language inspired by Go's template language.

Kriti templates are a superset if JSON with path lookups, if/the/else expressions, loops, and some basic predicate and conditional operators.

Kriti expressions are wrapped in double curly brackets such as `"http://wwww.google.com/{{$body.path}}"`. The `Kriti` evaluator takes `Kriti` template and a set of source json expressions paired with binders then uses them to construct a new json expression.

### Kriti Expressions

#### Path Accessors

Values can be looked up in bound json expressions using the standard path lookup syntax:
```
{{ $.foo.bar[0] }}
```
`.` is used to look up object fields and `[x]` is used to lookup array indices.

#### Loops

The `range` identifier is used to declare for loops:
```
{{ range i, x := $.event.author.articles }}
{{ x.title }}
{{ end }}
```
`i` and `x` above are binders for the index and value of the array element from `$.event.author.articles`. The index can be omitted by using an underscore in its place.

#### If Statements
Kriti supports if statements and `>` `<` `==` `||` and `&&` operators.
```
{{ if x.published && (x.post_id > 100) }}
{
"id": {{x.id}},
"title": {{x.title}}
}
{{ else }}
null
{{ end }}
```

#### String Interpolation
String Interpolation is currently limited to path lookups:
```
"http://www.{{$.domain}}.com/{{$.path}}"
```

### Library
The library exposes the function `runKriti`, the type definition of the function is:
``` haskell
runKriti :: Text -> [(Text, Value)] -> Either KritiErr Value
```
Expand All @@ -22,17 +57,13 @@ The first argument of the function is the template JSON, for example, we can use
myTemplate :: Text
myTemplate =
"{\
\ 'name': {{x.name.english}},\
\ 'id': {{x.id}},\
\ 'hp': {{x.base.HP}}\
\ 'name': {{$.name.english}},\
\ 'id': {{$.id}},\
\ 'hp': {{$.base.HP}}\
\}"
```

The second argument is a `list` of `tuple` of `(Text, Value)`. The first element of the tuple is the binding to be used for the JSON object, i.e. for the above template we are using `x` as the JSON binding, so, `x` will bind to the JSON object. The second element of the tuple is of type `Data.Aeson.Value` (can be obtained by `Data.Aeson.decode` method).

The function `runKriti` will return `Either KritiErr Value`. If the parser is successful, then it will return `Right Value`, else it will return `Left KritiErr` which can be used for debugging.

## Run example
#### Library Usage Sample Program
To run the example, first clone this repository using the following command:
``` sh
git clone git@github.com:hasura/kriti-lang.git
Expand All @@ -42,7 +73,20 @@ Now, run the following command:
cd kriti-lang
cabal new-run example
```
## Examples

The second argument is a `list` of `tuple` of `(Text, Value)`. The first element of the tuple is the binding to be used for the JSON object, i.e. for the above template we are using `x` as the JSON binding, so, `x` will bind to the JSON object. The second element of the tuple is of type `Data.Aeson.Value` (can be obtained by `Data.Aeson.decode` method).

The function `runKriti` will return `Either KritiErr Value`. If the parser is successful, then it will return `Right Value`, else it will return `Left KritiErr` which can be used for debugging.

### CLI Tool
The executable is a CLI tool which applices a transformation to a single json file:
``` bash
➜ cabal run kriti -- --json test/data/eval/success/source.json --template test/data/eval/success/examples/example1.kriti
{"guid":"43a922da-9665-4099-8dfc-f9af369695a4"}
```
The binder for the source file can be changed wit the `--bind` flag.

## Transformation Examples

JSON Input:
```
Expand Down

0 comments on commit 5c2715c

Please sign in to comment.