Skip to content

Commit

Permalink
Still trying to fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
woodfell committed Dec 14, 2023
1 parent 11b016b commit 64222e1
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 44 deletions.
4 changes: 2 additions & 2 deletions c/include/libsbp/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
/** Protocol minor version. */
#define SBP_MINOR_VERSION 0
/** Protocol patch version. */
#define SBP_PATCH_VERSION 3
#define SBP_PATCH_VERSION 4

/** Full SBP version string. */
#define SBP_VERSION "5.0.4-alpha"
#define SBP_VERSION "5.0.5-alpha"

/** Is this a staging branch? */
#define SBP_STAGING 0
Expand Down
2 changes: 1 addition & 1 deletion haskell/sbp.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sbp
version: 5.0.4-alpha
version: 5.0.5-alpha
synopsis: SwiftNav's SBP Library
homepage: https://github.com/swift-nav/libsbp
license: MIT
Expand Down
2 changes: 1 addition & 1 deletion javascript/sbp/RELEASE-VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.4-alpha
5.0.5-alpha
2 changes: 1 addition & 1 deletion kaitai/ksy/sbp.ksy
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
#
# Kaitai Struct definition file for Swift Binary Protocol 5.0.4-alpha
# Kaitai Struct definition file for Swift Binary Protocol 5.0.5-alpha
#
# Automatically generated with generate.py. Do not hand edit!

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sbp",
"version": "5.0.5-alpha",
"version": "5.0.4-alpha",
"description": "libsbp bindings for JavaScript. More information here: http://swift-nav.github.io/libsbp/",
"files": [
"javascript/*",
Expand Down
2 changes: 1 addition & 1 deletion python/sbp/RELEASE-VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.4-alpha
5.0.5-alpha
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
distro
sphinx
sphinxcontrib-spelling
autoflake
autoflake==1.7.8
2 changes: 1 addition & 1 deletion rust/sbp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[package]
name = "sbp"
version = "5.0.4-alpha"
version = "5.0.5-alpha"
description = "Rust native implementation of SBP (Swift Binary Protocol) for communicating with devices made by Swift Navigation"
authors = ["Swift Navigation <[email protected]>"]
repository = "https://github.com/swift-nav/libsbp"
Expand Down
2 changes: 1 addition & 1 deletion rust/sbp2json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[package]
name = "sbp2json"
version = "5.0.4-alpha"
version = "5.0.5-alpha"
repository = "https://github.com/swift-nav/libsbp"
description = "Rust native implementation of SBP (Swift Binary Protocol) to JSON conversion tools"
authors = ["Swift Navigation <[email protected]>"]
Expand Down
52 changes: 36 additions & 16 deletions sbpjson/javascript/SbpJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -1943,11 +1943,25 @@ function uTCTimeToJson(value) {
return JSON.stringify(uncast(value, r("UTCTime")), null, 2);
}

function invalidValue(typ, val, key = '') {
if (key) {
throw Error(`Invalid value for key "${key}". Expected type ${JSON.stringify(typ)} but got ${JSON.stringify(val)}`);
function invalidValue(typ, val, key, parent = '') {
const prettyTyp = prettyTypeName(typ);
const parentText = parent ? ` on ${parent}` : '';
const keyText = key ? ` for key "${key}"` : '';
throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);
}

function prettyTypeName(typ) {
if (Array.isArray(typ)) {
if (typ.length === 2 && typ[0] === undefined) {
return `an optional ${prettyTypeName(typ[1])}`;
} else {
return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`;
}
} else if (typeof typ === "object" && typ.literal !== undefined) {
return typ.literal;
} else {
return typeof typ;
}
throw Error(`Invalid value ${JSON.stringify(val)} for type ${JSON.stringify(typ)}`, );
}

function jsonToJSProps(typ) {
Expand All @@ -1968,10 +1982,10 @@ function jsToJSONProps(typ) {
return typ.jsToJSON;
}

function transform(val, typ, getProps, key = '') {
function transform(val, typ, getProps, key = '', parent = '') {
function transformPrimitive(typ, val) {
if (typeof typ === typeof val) return val;
return invalidValue(typ, val, key);
return invalidValue(typ, val, key, parent);
}

function transformUnion(typs, val) {
Expand All @@ -1983,17 +1997,17 @@ function transform(val, typ, getProps, key = '') {
return transform(val, typ, getProps);
} catch (_) {}
}
return invalidValue(typs, val);
return invalidValue(typs, val, key, parent);
}

function transformEnum(cases, val) {
if (cases.indexOf(val) !== -1) return val;
return invalidValue(cases, val);
return invalidValue(cases.map(a => { return l(a); }), val, key, parent);
}

function transformArray(typ, val) {
// val must be an array with no invalid elements
if (!Array.isArray(val)) return invalidValue("array", val);
if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent);
return val.map(el => transform(el, typ, getProps));
}

Expand All @@ -2003,24 +2017,24 @@ function transform(val, typ, getProps, key = '') {
}
const d = new Date(val);
if (isNaN(d.valueOf())) {
return invalidValue("Date", val);
return invalidValue(l("Date"), val, key, parent);
}
return d;
}

function transformObject(props, additional, val) {
if (val === null || typeof val !== "object" || Array.isArray(val)) {
return invalidValue("object", val);
return invalidValue(l(ref || "object"), val, key, parent);
}
const result = {};
Object.getOwnPropertyNames(props).forEach(key => {
const prop = props[key];
const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
result[prop.key] = transform(v, prop.typ, getProps, prop.key);
result[prop.key] = transform(v, prop.typ, getProps, key, ref);
});
Object.getOwnPropertyNames(val).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(props, key)) {
result[key] = transform(val[key], additional, getProps, key);
result[key] = transform(val[key], additional, getProps, key, ref);
}
});
return result;
Expand All @@ -2029,18 +2043,20 @@ function transform(val, typ, getProps, key = '') {
if (typ === "any") return val;
if (typ === null) {
if (val === null) return val;
return invalidValue(typ, val);
return invalidValue(typ, val, key, parent);
}
if (typ === false) return invalidValue(typ, val);
if (typ === false) return invalidValue(typ, val, key, parent);
let ref = undefined;
while (typeof typ === "object" && typ.ref !== undefined) {
ref = typ.ref;
typ = typeMap[typ.ref];
}
if (Array.isArray(typ)) return transformEnum(typ, val);
if (typeof typ === "object") {
return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
: typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
: typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
: invalidValue(typ, val);
: invalidValue(typ, val, key, parent);
}
// Numbers can be parsed by Date but shouldn't be.
if (typ === Date && typeof val !== "number") return transformDate(val);
Expand All @@ -2055,6 +2071,10 @@ function uncast(val, typ) {
return transform(val, typ, jsToJSONProps);
}

function l(typ) {
return { literal: typ };
}

function a(typ) {
return { arrayItems: typ };
}
Expand Down
Loading

0 comments on commit 64222e1

Please sign in to comment.