#!/bin/bash for ARG ; do case $ARG in -s|--silent) SILENT=true ;; *) if [ -z "$SCHEMA" ]; then SCHEMA=$ARG else echo "Error: '$ARG'. One file only, please." fi ;; esac shift done if [ ! -f "$SCHEMA" ]; then echo "Usage: $0 [-s|--silent] " >&2 exit -1 fi # if [ -z "$SILENT" ]; then echo "# $SCHEMA" # fi # Check json syntax if ! jq . "$SCHEMA" > /dev/null ; then exit 1 fi # Validate conformace do schema.cddl # to install cddl: # sudo gem install cddl # to install cbor: # sudo aptitude install ruby-dev # sudo gem install cbor if ! cddl schema.cddl validate "$SCHEMA" ; then exit 2 fi # Validate 'type' fields of the schema (generate instance if possible) if [ $(jq 'has("datamodel")' "$SCHEMA") == "true" ]; then readarray -t NAMES < <(jq -r '.datamodel | keys_unsorted | .[] | @text' "$SCHEMA") fi for (( i=0; i < ${#NAMES[*]}; i++ )) ; do if [ -z "$SILENT" ]; then echo "- ${NAMES[i]}" fi jq -r ".datamodel | .[\"${NAMES[i]}\"] | .type | @text" "$SCHEMA" > "/tmp/${NAMES[i]}.cddl" if [ -z "$SILENT" ]; then cddl "/tmp/${NAMES[i]}.cddl" generate else cddl "/tmp/${NAMES[i]}.cddl" generate >/dev/null fi rm -f "/tmp/${NAMES[i]}.cddl" done