Skip to content

Commit

Permalink
Merge pull request #62 from mongodb-js/add-docs
Browse files Browse the repository at this point in the history
add an API section to readme
  • Loading branch information
durran committed May 15, 2018
1 parent c9037fa commit d6ebb51
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/bson-transpilers/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright MongoDB Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
87 changes: 72 additions & 15 deletions packages/bson-transpilers/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,80 @@
# BSON-Compilers
Transpilers for building BSON documents in any language.
## Authors
Anna Herlihy (herlihyap@gmail.com)
[![npm version][1]][2] [![build status][3]][4]
[![downloads][5]][6]

Alena Khineika (alena.khineika@mongodb.com)
Transpilers for building BSON documents in any language. Current support
provided for `shell` and `javascript` as inputs. `java`, `c#` and `python` as
outputs.

Irina Shestak (shestak.irina@gmail.com)
# Usage

## App Start

* `$ npm run compile`

## Usage
```
```js
const compiler = require('bson-compilers');

const input = 'javascript';
const output = 'java';
compiler[input][output]("some code in js to compile to java");
// or
compiler.javascript.java("some code in js to compile to java");
```

const string =`
{ item: "book", qty: Binary(Buffer.from("5")), tags: ["red", "blank"], dim_cm: [14, Int32("81")] }`

const compiledString = compiler[input][output](string);
console.log(compiledCode)
// new Document().append("item", "book").append("qty", new Binary("5".getBytes("UTF-8")))
// .append("tags", Arrays.asList("red", "blank"))
// .append("dim_cm", Arrays.asList(new java.lang.Long("14"), new java.lang.Integer("81")))
```

## API
### compiledString = compiler.inputLang.outputLang(codeString)
Output a compiled string given input and output languages.
- __inputLang:__ Input language of the code string. `shell` and `javascript` are currently supported.
- __outputLang:__ The language you would like the output to be. `java`, `python` and `C#` are currently supported.
- __codeString:__ The code string you would like to be compiled to your selected output language.

# Install
```shell
npm install -S bson-compilers
```

## Contributing
`bson-compilers` uses [antlr](https://github.com/antlr/antlr4/blob/master/doc/javascript-target.md) to help create an AST. As `antlr` is written in Java, you will need to set up a few tools before being able to compile this locally.

Make sure you have Java installed:
```shell
$ brew cask install java
```

Download `antlr`:
```shell
$ cd /usr/local/lib && curl -O http://www.antlr.org/download/antlr-4.7.1-complete.jar
```

You will then need to add it to your `$CLASSPATH`:
```shell
$ export CLASSPATH=".:/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH"
```

Alias `antlr4` and `grun`:
```shell
$ alias antlr4='java -Xmx500M -cp "/usr/local/lib/antlr-4.7.1-complete.jar:$CLASSPATH" org.antlr.v4.Tool' && alias grun='java org.antlr.v4.gui.TestRig'
```

Then compile and run tests locally with:
```shell
$ npm run compile && npm run test
```

# Authors
- [aherlihy](https://github.com/aherlihy) - Anna Herlihy <herlihyap@gmail.com>
- [alenakhineika](https://github.com/alenakhineika) - Alena Khineika <alena.khineika@mongodb.com>
- [lrlna](github.com/lrlna) - Irina Shestak <shestak.irina@gmail.com>

# License
[Apache-2.0](https://tldrlegal.com/license/apache-license-2.0-(apache-2.0))

[1]: https://img.shields.io/npm/v/bson-compilers.svg?style=flat-square
[2]: https://npmjs.org/package/bson-compilers
[3]: https://img.shields.io/travis/mongodb-js/bson-compilers/master.svg?style=flat-square
[4]: https://travis-ci.com/mongodb-js/bson-compilers
[5]: http://img.shields.io/npm/dm/bson-compilers.svg?style=flat-square
[6]: https://npmjs.org/package/bson-compilers

0 comments on commit d6ebb51

Please sign in to comment.