infer

JSON syntax of Infer

Synopsis

infer type syntax requires a parser, which adds complexity for applications. Since infer’s structure is an structured object, it would be simple to translate to JSON. Also could be more convenient to be processed and extended.

Example

{
  "kind": "object",
  "fields": {
    "a": "string",
    "b": "number",
    "c": ["boolean"],
    "d": {
      "kind": "union",
      "types": [
        "string", "number", "boolean"
      ]
    }
  }
}

Alternative JavaScript version:

var type = {
  kind: "object",
  fields: {
    a: String,
    b: Number,
    c: [Boolean],
    d: {
      kind: "union",
      types: [ String, Number, Boolean ]
    } 
  }
}

Details

Simple types

Simple Types can be represented by their name, or corresponded JavaScript Constructor.

infer type JSON Reps JSON Reps Expanded JS Alternative
number "number" { "kind": "simple", "type": "number" } Number
string "string" { "kind": "simple", "type": "string" } String
boolean "boolean" { "kind": "simple", "type": "boolean" } Boolean
null "null" { "kind": "simple", "type": "null" } "null"
bottom "bottom" { "kind": "simple", "type": "bottom" } "bottom"

Array

Union

Object

Nullable

Add an extra field "nullable": true on expanded json syntax or nullable: true on alternative JS syntax, denotes that type was nullable.

Simplified JSON Syntax

Objects and Unions have their specific fields in expanded json syntax, so it would be simple to just use these fields to determine which kind of type that this type are.

This means when you see a expanded json type includes field fields, then this type must be an object, when types, then must be union.

So the example above can be simplified to:

{
  "fields": {
    "a": "string",
    "b": "number",
    "c": ["boolean"],
    "d": {
      "types": [
        "string", "number", "boolean"
      ]
    }
  }
}

Also you can change fields to object or change types to union, which makes more sense.

{
  "object": {
    "a": "string",
    "b": "number",
    "c": ["boolean"],
    "d": {
      "union": [
        "string", "number", "boolean"
      ]
    }
  }
}

Type Definition for JSON Syntax

Following document include 2 infer syntax extensions(not implemented yet): recursion and mappable type.

{
  "kind": "union",
  "types": [
    "string",
    {
      "kind": "object",
      "fields": {
        "kind":{ "type":  "string", "nullable": true },
        "type": "string"
      }
    },
    {
      "kind": "object",
      "fields": {
        "kind": { 
          "type": "string", "nullable": true
        },
        "types": ["$"]
      }
    },
    {
      "kind": "object",
      "fields": {
        "kind": { 
          "type": "string", "nullable": true
        },
        "fields": "$[string]"
      }
    },
    {
      "kind": "object",
      "fields": {
        "object": "$[string]"
      }
    },
    {
      "kind": "object",
      "fields": {
        "union": ["$"]
      }
    }
  ]
}