Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for json codec backend #137

Merged
merged 6 commits into from
Feb 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
documentation for json codec backend
  • Loading branch information
klahnunya committed Feb 1, 2023
commit bfa223829dd1d030fe8f7c9c7c5aa5b580543943
67 changes: 67 additions & 0 deletions docs/json-codec-backend/codec-docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## Json Codec Backend
AttilaMihaly marked this conversation as resolved.
Show resolved Hide resolved
The purpose of this documentation is to give a explanation of how the Json Codec Backend works and how to use it.
The Json Codec backend is a tool that generates encoders and decoders from a Morphir IR using the Circe JSON library.
AttilaMihaly marked this conversation as resolved.
Show resolved Hide resolved

### How to use the JSON Codec Backend

The first step to using the JSON Codec backend is to add the Circe JSON library to your project. This can be done in two ways:
1. Add the Circe JSON dependency through your build tool eg. Sbt, Mill etc.
2. Add ```morphir-jvm``` as a dependency to your project.
AttilaMihaly marked this conversation as resolved.
Show resolved Hide resolved

#### Generating Codecs from the IR
The backend is built to generate codecs for only types so when generating an IR the ```-t``` which specifies ```--types-only``` flag should be used. Also, we want to add the ```-f``` flag to use the old CLI make function.
AttilaMihaly marked this conversation as resolved.
Show resolved Hide resolved

To generate Codecs from the IR:
1. Run ```morphir-elm make -f -t``` to generate the IR.
2. Run ```morphir-elm gen -s``` to generate the codecs
3. In the situation where you do not have ```morphir-jvm``` as a dependency in your project, you have to add the ```-c``` flag to copy dependencies from the ```morphir-jvm``` project. Complete command for this is ```morphir-elm gen -s -c```
AttilaMihaly marked this conversation as resolved.
Show resolved Hide resolved

### How to use Generated Codecs
Using an example , lets describe a model structure and look at how the generated codec for this particular model will be structured

The model:

- reference-model
- src
- Morphir
- Reference
- Model
- BooksAndRecords.elm
- morphir.json
- morphir-ir.json


Running the command ```morphir-elm gen -s -c -o codecs/src``` to generate codecs means your codecs would be outputted into the ``` codecs/src``` folder in the structure:

- reference-model
- codecs
- src
- morphir
- booksandrecords
- Codec.scala <----- This is your genereated codec
- morphir.json
- morphir-ir.json

The generated codecs ```Codec.scala``` has both encoders and decoders. Encoders at the top and decoders following. This is a truncated example of how it looks:
```
package morphir.reference.model.booksandrecords


object Codec{
encoders ...

decoders ...
}
```

In the example above ```myproject``` is the project in which the codecs would be used.
The codecs can now be imported into ```myproject``` and used.
AttilaMihaly marked this conversation as resolved.
Show resolved Hide resolved



#### Unsupported Features
Codecs are not generated for Function types in the morphir ir.