Skip to content

Commit

Permalink
cargo fmt, update README, update publish.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ucarion committed Oct 10, 2020
1 parent 018857a commit 4df1346
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 37 deletions.
58 changes: 53 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
on:
push:
branches:
- master
tags:
- v*

jobs:
publish:
create_release:
runs-on: ubuntu-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}

release_binary:
runs-on: ${{ matrix.runs_on }}
needs: create_release
strategy:
matrix:
target:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
- x86_64-apple-darwin
- x86_64-pc-windows-gnu
include:
- target: x86_64-unknown-linux-gnu
runs_on: ubuntu-latest
artifact: jtd-infer
- target: x86_64-unknown-linux-musl
runs_on: ubuntu-latest
artifact: jtd-infer
- target: x86_64-apple-darwin
runs_on: macos-latest
artifact: jtd-infer
- target: x86_64-pc-windows-gnu
runs_on: ubuntu-latest
artifact: jtd-infer.exe
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: cargo publish
- run: cargo install cross
- run: cross build --release --target=$TARGET
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
TARGET: ${{ matrix.target }}
- run: zip --junk-paths $TARGET.zip target/$TARGET/release/$ARTIFACT
env:
TARGET: ${{ matrix.target }}
ARTIFACT: ${{ matrix.artifact }}
- uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ needs.create_release.outputs.upload_url }}
asset_path: ${{ matrix.target }}.zip
asset_name: ${{ matrix.target }}.zip
asset_content_type: application/zip
86 changes: 60 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
# jtd-infer

`jtd-infer` generates a JSON Typedef schema from example data.
`jtd-infer` generates ("infers") a JSON Typedef schema from example data.

For high-level guidance on how to use this package, see ["Inferring a JSON
Typedef Schema from Real Data"][jtd-jtd-infer] in the JSON Typedef docs.
```bash
echo '{ "name": "Joe", "age": 42 }' | jtd-infer | jq
```

```json
{
"properties": {
"age": {
"type": "uint8"
},
"name": {
"type": "string"
}
}
}
```

## Installation

To install `jtd-infer`, you have a few options:

### Install with Homebrew
### Install on macOS

This option is recommended if you're on macOS.
You can install `jtd-infer` via Homebrew:

```bash
brew install jsontypedef/jsontypedef/jtd-infer
```

Alternatively, you can download and extract the binary yourself from
`x86_64-apple-darwin.zip` in [the latest release][latest]. Because of Apple's
quarantine system, you will need to run:

```bash
xattr -d com.apple.quarantine path/to/jtd-infer
```

In order to be able to run the executable.

### Install on Linux

Download and extract the binary from `x86_64-unknown-linux-gnu.zip` in [the
latest release][latest].

### Install on Windows

Download and extract the binary from `x86_64-pc-windows-gnu.zip` in [the latest
release][latest]. Runs on 64-bit MinGW for Windows 7+.

### Install with Docker

This option is recommended on non-Mac platforms, or if you're running
`jtd-infer` in some sort of script and you want to make sure that everyone
running the script uses the same version of `jtd-infer`.
This option is recommended if you're running `jtd-infer` in some sort of script
and you want to make sure that everyone running the script uses the same version
of `jtd-infer`.

```bash
docker pull jsontypedef/jtd-tools
docker pull jsontypedef/jtd-infer
```

If you opt to use the Docker approach, you will need to change all invocations
Expand All @@ -37,31 +71,30 @@ jtd-infer [...]
To:

```bash
docker exec -it jsontypedef/jtd-tools /jtd-infer [...]
```

### Install with Cargo

This option is recommended if you already have `cargo` installed, or if you
would prefer to use a version of `jtd-infer` compiled on your machine:
# To have jtd-infer read from STDIN, run it like so:
docker exec -i jsontypedef/jtd-infer [...]

```bash
cargo install jtd-infer
# To have jtd-infer read from a file, run it as:
docker run -v /path/to/file.json:/file.json -i jsontypedef/jtd-infer [...] file.json
# or, if file.json is in your current directory:
docker run -v $(pwd)/file.json:/file.json -i jsontypedef/jtd-infer [...] file.json
```

## Usage

> See the top of this README for links to high-level guidance on how to use
> `jtd-infer`.
For high-level guidance on how to use `jtd-infer`, see ["Inferring a JSON
Typedef Schema from Real Data"][jtd-jtd-infer] in the JSON Typedef website docs.

### Basic Usage

To invoke `jtd-infer`, you can either:

1. Have it read from STDIN. This is the default behavior.
2. Have it read from a file. To do this, pass a file name as the last argument
to `jtd-infer`.

`jtd-infer` can read a _sequence_ of JSON messages. So for example, if you have
a file like this in `data.json`:
`jtd-infer` reads a _sequence_ of JSON messages. So for example, if you have a
file like this in `data.json`:

```json
{ "name": "john doe", "age": 42 }
Expand All @@ -82,7 +115,7 @@ In both cases, you'd get this output:
{"properties":{"name":{"type":"string"},"age":{"type":"uint8"}}}
```

## Advanced Usage: Providing Hints
### Advanced Usage: Providing Hints

By default, `jtd-infer` will never output `enum`, `values`, or `discriminator`
schemas. This is by design: by always being consistent with what it outputs,
Expand All @@ -102,7 +135,7 @@ As a corner-case, if you want to point to the *root* / top-level of your input,
then use the empty string as the path. See ["Using
`--values-hint`"](##using---values-hint) for an example of this.

### Using `--enum-hint`
#### Using `--enum-hint`

By default, strings are always inferred to be `{ "type": "string" }`:

Expand All @@ -126,7 +159,7 @@ echo '["foo", "bar", "baz"]' | jtd-infer --enum-hint=/-
{"elements":{"enum":["bar","baz","foo"]}}
```

### Using `--values-hint`
#### Using `--values-hint`

By default, objects are always assumed to be "structs", and `jtd-infer` will
generate `properties` / `optionalProperties`. For example:
Expand All @@ -151,7 +184,7 @@ echo '{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}' | jtd-infer --values-hin
{"values":{"elements":{"type":"uint8"}}}
```

### Using `--discriminator-hint`
#### Using `--discriminator-hint`

By default, objects are always assumed to be "structs", and `jtd-infer` will
generate `properties` / `optionalProperties`. For example:
Expand Down Expand Up @@ -197,3 +230,4 @@ echo '[{"type": "s", "value": "foo"},{"type": "n", "value": 3.14}]' | jtd-infer
```

[jtd-jtd-infer]: https://jsontypedef.com/docs/tools/jtd-infer
[latest]: https://github.com/jsontypedef/json-typedef-infer/releases/latest
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
mod hint_set;

use chrono::DateTime;
pub use hint_set::HintSet;
use jtd::form::{self, TypeValue};
use jtd::{Form, Schema};
use serde_json::Value;
use std::collections::HashMap;
use std::collections::HashSet;

#[derive(Debug)]
Expand Down Expand Up @@ -40,12 +45,6 @@ impl<'a> Hints<'a> {
}
}

use chrono::DateTime;
use jtd::form::{self, TypeValue};
use jtd::{Form, Schema};
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug)]
pub enum InferredSchema {
Unknown,
Expand Down

0 comments on commit 4df1346

Please sign in to comment.