anz-bank / sysl

Sysl (pronounced "sizzle") is a system specification language
https://sysl.io
Apache License 2.0
122 stars 42 forks source link

Add support for float32 and float64 types #1011

Closed andrewemeryanz closed 4 years ago

andrewemeryanz commented 4 years ago

Description

Sysl currently includes support for int, int32, int64 and float primitive types.

Support for float32 and float64 types should be added.

Steps to Reproduce

The current behaviour is to regard float32 and float64 as custom types:

Foo:
  !type Bar:
    f <: float
    f32 <: float32
    f64 <: float64

Expected behavior

"f32": {
  "primitive": "FLOAT",
  "constraint": { "length": { "max": 32 } }
},
"f64": {
  "primitive": "FLOAT",
  "constraint": { "length": { "max": 64 } }
},

Actual behavior

"f32": {
  "typeRef": {
    "context": { "appname": { "part": [ "Foo" ] }, "path": [ "Bar" ] },
    "ref": { "path": [ "float32" ] } }
},
"f64": {
  "typeRef": {
    "context": { "appname": { "part": [ "Foo" ] }, "path": [ "Bar" ] },
    "ref": { "path": [ "float64" ] } }
}

Your Environment

$ sysl info
Build:
  Version      : v0.155.0
  Git Commit   : 1dc740cb8c1b629d587d374640f89eb12fddc3da
  Date         : 2020-07-27T02:38:58Z
  Go Version   : go1.13.14 linux/amd64
  OS           : darwin/amd64
springwiz commented 4 years ago

float32 is not needed for cloud spanner. Spanner supports only float64. https://cloud.google.com/spanner/docs/data-types

andrewemeryanz commented 4 years ago

A description of the current support of numeric, string and byte types:

d <: decimal            # primitive: DECIMAL
dx <: decimal(4)        # primitive: DECIMAL, constraint: { length: { max: 4 } }
dxy <: decimal(4.8)     # primitive: DECIMAL, constraint: { length: { max: 4 }, precision: 4, scale: 8 }
dxxy <: decimal(4..8)   # primitive: DECIMAL, constraint: { length: { min: 4, max: 8 } }

i <: int                # primitive: INT
ix <: int(4)            # primitive: INT, constraint: { length: { max: 4 } }
ixy <: int(4.8)         # primitive: INT, constraint: { length: { max: 4 } }
ixxy <: int(4..8)       # primitive: INT, constraint: { length: { min: 4, max: 8 } }

i32 <: int32            # primitive: INT, constraint: { range: { min: { i: -2147483648 }, max: { i: 2147483647 } } }
i32x <: int32(4)        # primitive: INT, constraint: { length: { max: 4 } }
i32xy <: int32(4.8)     # primitive: INT, constraint: { length: { max: 4 } }
i32xxy <: int32(4..8)   # primitive: INT, constraint: { length: { min: 4, max: 8 } }

i64 <: int64            # primitive: INT, constraint: { range: { min: { i: -9223372036854775808 }, max: { i: 9223372036854775807 } } }
i64x <: int64(4)        # primitive: INT, constraint: { length: { max: 4 } }
i64xy <: int64(4.8)     # primitive: INT, constraint: { length: { max: 4 } }
i64xxy <: int64(4..8)   # primitive: INT, constraint: { length: { min: 4, max: 8 } }

f <: float              # primitive: FLOAT
fx <: float(4)          # parse error
fxy <: float(4.8)       # parse error
fxxy <: float(4..8)     # parse error

s <: string             # primitive: STRING
sx <: string(4)         # primitive: STRING, constraint: { length: { max: 4 } }
sxy <: string(4.8)      # primitive: STRING, constraint: { length: { max: 4 } }
sxxy <: string(4..8)    # primitive: STRING, constraint: { length: { max: 8 } }

b <: bytes              # primitive: BYTES
bx <: bytes(4)          # parse error
bxy <: bytes(4.8)       # parse error
bxxy <: bytes(4..8)     # parse error
andrewemeryanz commented 4 years ago

The following changes should be made:

  1. Introduce an int32 bitWidth value within the Constraint type.
  2. Explicitly set the bitWidth of int32 and int64 types to be 32 and 64 respectively.
  3. Introduce parser support for float32 and float64 types setting the bitWidth to be 32 and 64 respectively.

This should result in the following values:

i32 <: int32            # primitive: INT, constraint: { bitWidth: 32, range: { min: { i: -2147483648 }, max: { i: 2147483647 } } }
i32x <: int32(4)        # primitive: INT, constraint: { bitWidth: 32, length: { max: 4 } }
i32xy <: int32(4.8)     # primitive: INT, constraint: { bitWidth: 32, length: { max: 4 } }
i32xxy <: int32(4..8)   # primitive: INT, constraint: { bitWidth: 32, length: { min: 4, max: 8 } }

i64 <: int64            # primitive: INT, constraint: { bitWidth: 64, range: { min: { i: -9223372036854775808 }, max: { i: 9223372036854775807 } } }
i64x <: int64(4)        # primitive: INT, constraint: { bitWidth: 64, length: { max: 4 } }
i64xy <: int64(4.8)     # primitive: INT, constraint: { bitWidth: 64, length: { max: 4 } }
i64xxy <: int64(4..8)   # primitive: INT, constraint: { bitWidth: 64, length: { min: 4, max: 8 } }

f32 <: float32          # primitive: FLOAT, constraint: { bitWidth: 32 }
f64 <: float64          # primitive: FLOAT, constraint: { bitWidth: 64 }