Skip to content

Commit

Permalink
Merge pull request #1 from podium/init-functionality
Browse files Browse the repository at this point in the history
Adding Rust Code with Elixir Bindings
  • Loading branch information
bradschwartz authored May 28, 2024
2 parents 51d7710 + 1b82edc commit b64b65d
Show file tree
Hide file tree
Showing 25 changed files with 2,074 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @podium/oss-engineers
23 changes: 23 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: epinault

---

**Describe the bug**
A clear and concise description of what the bug is.

** Provide the following details

- Elixir version (elixir -v):
- Erlang version (erl -v):
- Operating system:

**Expected behavior**
A clear and concise description of what you expected to happen.

**Actual behavior**
A clear and concise description of what actually happens.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

- package-ecosystem: "mix"
directory: "/"
schedule:
interval: "weekly"
55 changes: 55 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Elixir CI

on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
setup:
runs-on: ubuntu-latest
env:
MIX_ENV: test
RUSTLER_PRECOMPILATION_FORCE_BUILD: true

strategy:
fail-fast: false
matrix:
elixir_version: [1.15, 1.16]
otp_version: [25, 26]
steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp_version}}
elixir-version: ${{matrix.elixir_version}}

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- uses: actions/cache@v4
with:
path: |
deps
_build
key: deps-${{ runner.os }}-${{ matrix.otp_version }}-${{ matrix.elixir_version }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
deps-${{ runner.os }}-${{ matrix.otp_version }}-${{ matrix.elixir_version }}
- run: mix deps.get

- run: mix format --check-formatted

- run: mix deps.unlock --check-unused

- run: mix deps.compile

- run: mix compile --warnings-as-errors

- run: mix credo --strict --format=oneline

- run: mix test --warnings-as-errors --cover
92 changes: 92 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Build Precompiled NIFs
# lifted from: https://github.com/philss/rustler_precompilation_example/blob/main/.github/workflows/release.yml

on:
pull_request:
branches:
- master
push:
branches:
- master
tags:
- "*"

jobs:
build_release:
name: NIF ${{ matrix.nif }} - ${{ matrix.job.target }} (${{ matrix.job.os }})
runs-on: ${{ matrix.job.os }}
strategy:
fail-fast: false
matrix:
nif: ["2.16", "2.15"]
job:
- {
target: arm-unknown-linux-gnueabihf,
os: ubuntu-20.04,
use-cross: true,
}
- {
target: aarch64-unknown-linux-gnu,
os: ubuntu-20.04,
use-cross: true,
}
- {
target: aarch64-unknown-linux-musl,
os: ubuntu-20.04,
use-cross: true,
}
- { target: aarch64-apple-darwin, os: macos-11 }
- {
target: riscv64gc-unknown-linux-gnu,
os: ubuntu-20.04,
use-cross: true,
}
- { target: x86_64-apple-darwin, os: macos-11 }
- { target: x86_64-unknown-linux-gnu, os: ubuntu-20.04 }
- {
target: x86_64-unknown-linux-musl,
os: ubuntu-20.04,
use-cross: true,
}
- { target: x86_64-pc-windows-gnu, os: windows-2019 }
- { target: x86_64-pc-windows-msvc, os: windows-2019 }

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Extract project version
shell: bash
run: |
# Get the project version from mix.exs
echo "PROJECT_VERSION=$(sed -n 's/^ @version "\(.*\)"/\1/p' mix.exs | head -n1)" >> $GITHUB_ENV
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: ${{ matrix.job.target }}

- name: Build the project
id: build-crate
uses: philss/[email protected]
with:
project-name: json_schema_nif
project-version: ${{ env.PROJECT_VERSION }}
target: ${{ matrix.job.target }}
nif-version: ${{ matrix.nif }}
use-cross: ${{ matrix.job.use-cross }}
project-dir: "native/json_schema_nif"

- name: Artifact upload
uses: actions/upload-artifact@v3
with:
name: ${{ steps.build-crate.outputs.file-name }}
path: ${{ steps.build-crate.outputs.file-path }}

- name: Publish archives and packages
uses: softprops/action-gh-release@v1
with:
files: |
${{ steps.build-crate.outputs.file-path }}
if: startsWith(github.ref, 'refs/tags/')
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
json_schema_nif-*.tar

# Temporary files, for example, from tests.
/tmp/

# ---

# Rustler
*.so
35 changes: 35 additions & 0 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
IO.puts("Using .iex.exs file loaded from #{__DIR__}/.iex.exs")

defmodule :_util do
defdelegate exit(), to: System, as: :halt
defdelegate q(), to: System, as: :halt
defdelegate restart(), to: System, as: :restart

def atom_status do
limit = :erlang.system_info(:atom_limit)
count = :erlang.system_info(:atom_count)

IO.puts("Currently using #{count} / #{limit} atoms")
end

def cls, do: IO.puts("\ec")

def raw(any, label \\ "iex") do
IO.inspect(any,
label: label,
pretty: true,
limit: :infinity,
structs: false,
syntax_colors: [
number: :yellow,
atom: :cyan,
string: :green,
nil: :magenta,
boolean: :magenta
],
width: 0
)
end
end

import :_util
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elixir 1.15.7-otp-26
erlang 26.1.1
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rust-analyzer.linkedProjects": [
"./native/json_schema_nif/Cargo.toml"
]
}
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic
Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.1.0] - 2024-05-15

Initial release.

### Added

- `JsonSchemaNif.validate_json/2` to validate a JSON instance against a provided JSON schema.
40 changes: 40 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Contributing

This library is kept intentionally simple.

That said, contributions (especially bug fixes!) are welcome. To contribute:

- Propose the changes as a GitHub Issue
- If feedback is supportive, create a new branch for your feature or bug fix
- Implement your changes
- Write tests for your changes
- Document your changes
- Ensure all tests pass
- Submit a merge request

# Building the Project

Build dependencies:

1. Elixir toolchain (default to asdf installation)
1. Rust toolchain

To force a compilation of the Rust portions locally, instead of relying
on pre-built artifacts, set `RUSTLER_PRECOMPILATION_FORCE_BUILD=true`.

```bash
cd native/json_schema_nif/
# If you need extra toolchains
rustup target add x86_64-apple-darwin
rustup target add aarch64-apple-darwin
rustup target add aarch64-unknown-linux-gnu

cargo build --target aarch64-unknown-linux-gnu --release
cargo build --target aarch64-apple-darwin --release
cargo build --target x86_64-apple-darwin --release
```

To generate the checksum file **after** the NIF files have been uploaded to GitHub,
run: `mix rustler_precompiled.download JsonSchemaNif --only-local`.

These files are required as part of the Hex package, but don't need to be tracked in VCS.
8 changes: 8 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2024 Podium

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,35 @@ Library for validating JSON instances against JSON schemas.

Provides a straightforward way to ensure that JSON data adheres to a predefined schema, enhancing the reliability and
consistency of data. Especially useful in contexts like Kafka.

## Installation

The package can be installed by adding `json_schema_nif` to your list of dependencies in `mix.exs`:

```elixir
defp deps do
[
{:json_schema_nif, "~> 0.1.0"}
]
end
```

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md).

## Documentation

Documentation is available in [HexDocs](https://hexdocs.pm/json_schema_nif/readme.html).

## Releasing

Since we rely on RustlerPrecompiled actually building our Rust bindings in advance,
we need to follow their [recommended flow](https://hexdocs.pm/rustler_precompiled/precompilation_guide.html#recommended-flow):

1. Release a new tag
1. Push the code to your repository with the new tag: git push origin main --tags
1. Wait for all NIFs to be built
1. Run the `mix rustler_precompiled.download JsonSchemaNif --all`task (with the flag --all)
release the package to Hex.pm (make sure your release includes the correct files).
- Run `mix hex.build --unpack` to ensure checksum files exist
Loading

0 comments on commit b64b65d

Please sign in to comment.