brettviren / moo

ruminants on module oriented programming
GNU General Public License v3.0
4 stars 4 forks source link

Add support for a "hex" type in moo oschema #34

Open brettviren opened 2 years ago

brettviren commented 2 years ago

JSON numbers, and thus Jsonnet numbers, are only IEEE 754 double-precision floating-point format. So integers, per se, are not supported in JSON. However, DP FP's exactly represent integers in the range of +/- 2^53.

With no integers it is not surprising that there is also no hex format supported in JSON.

But, moo schema could help here.

A moo "hex" type could be added to moo.oschema. And for that matter an explicit "integer" or a "bigint" type could be supported for integer values larger than 2^53.

The values might be specified as string representations, eg:

{ "myhex": "0xff" }

Then, moo templates can be extended to generate code which parses the hex string into a proper language-native (eg, C++) type.

Until such support, if the hex values to be represented are within IEEE 754 integer guarantees then schema for a "hex" type can be left as a "number" and the value can be given as an integer-literal.

When producing values as JSON one is left to your own devices to convert hex to int.

For values provided as Jsonnet one can make use of:

  std.parseHex(str)

  Parses an unsigned hexadecimal integer, from the input string. Case
  insensitive.

  Example: std.parseHex("ff") yields 255.

Refs: