Skip to content

Commit

Permalink
fix(Primitves): Refer to internal schema for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jul 14, 2021
1 parent 4b82b26 commit 54a8264
Show file tree
Hide file tree
Showing 14 changed files with 1,175 additions and 1,128 deletions.
1,737 changes: 872 additions & 865 deletions python/stencila/schema/types.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion r/R/types.R
Original file line number Diff line number Diff line change
Expand Up @@ -4512,7 +4512,7 @@ MediaObjectTypes <- Union(MediaObject, AudioObject, ImageObject, VideoObject)
#'
#' @return A `list` of class `Union` describing valid subtypes of this type
#' @export
Node <- Union(Entity, ArrayValidator, Article, AudioObject, BooleanValidator, Brand, CitationIntentEnumeration, Cite, CiteGroup, Claim, Code, CodeBlock, CodeChunk, CodeError, CodeExpression, CodeFragment, Collection, Comment, ConstantValidator, ContactPoint, CreativeWork, Datatable, DatatableColumn, Date, DefinedTerm, Delete, Emphasis, EnumValidator, Enumeration, Figure, Function, Grant, Heading, ImageObject, Include, IntegerValidator, Link, List, ListItem, Mark, Math, MathBlock, MathFragment, MediaObject, MonetaryGrant, NontextualAnnotation, Note, NumberValidator, Organization, Paragraph, Parameter, Periodical, Person, PostalAddress, Product, PropertyValue, PublicationIssue, PublicationVolume, Quote, QuoteBlock, Review, SoftwareApplication, SoftwareEnvironment, SoftwareSession, SoftwareSourceCode, StringValidator, Strong, Subscript, Superscript, Table, TableCell, TableRow, ThematicBreak, Thing, TupleValidator, Validator, Variable, VideoObject, VolumeMount, "NULL", "logical", "numeric", "character", "list", Array("Node"))
Node <- Union(Entity, ArrayValidator, Article, AudioObject, BooleanValidator, Brand, CitationIntentEnumeration, Cite, CiteGroup, Claim, Code, CodeBlock, CodeChunk, CodeError, CodeExpression, CodeFragment, Collection, Comment, ConstantValidator, ContactPoint, CreativeWork, Datatable, DatatableColumn, Date, DefinedTerm, Delete, Emphasis, EnumValidator, Enumeration, Figure, Function, Grant, Heading, ImageObject, Include, IntegerValidator, Link, List, ListItem, Mark, Math, MathBlock, MathFragment, MediaObject, MonetaryGrant, NontextualAnnotation, Note, NumberValidator, Organization, Paragraph, Parameter, Periodical, Person, PostalAddress, Product, PropertyValue, PublicationIssue, PublicationVolume, Quote, QuoteBlock, Review, SoftwareApplication, SoftwareEnvironment, SoftwareSession, SoftwareSourceCode, StringValidator, Strong, Subscript, Superscript, Table, TableCell, TableRow, ThematicBreak, Thing, TupleValidator, Validator, Variable, VideoObject, VolumeMount, "NULL", "logical", "numeric", "character", "list", Array(Any()))


#' All type schemas that are derived from Thing
Expand Down
2 changes: 1 addition & 1 deletion rust/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub const SCHEMAS: &[(&str, &str)] = &[
("Heading", r#"{"title":"Heading","required":["type","content"],"examples":[{"type":"Heading","depth":2,"content":["Secondary Heading"]}],"type":"object","additionalProperties":false,"propertyAliases":{},"properties":{"id":{"type":"string"},"depth":{"type":"integer","default":1,"minimum":1,"maximum":6},"content":{"type":"array","items":{"$ref":"InlineContent.schema.json"}}}}"#),
("ImageObject", r#"{"title":"ImageObject","examples":[{"type":"ImageObject","caption":"Kiwi","contentSize":"10.4","contentUrl":"http://www.example.com/kiwi.png","encodingFormat":"image/png","thumbnail":{"type":"ImageObject","contentUrl":"http://www.example.com/kiwi_mini.png"}}],"type":"object","additionalProperties":false,"required":["type","contentUrl"],"propertyAliases":{"alternateName":"alternateNames","identifier":"identifiers","image":"images","author":"authors","comment":"comments","date":"datePublished","editor":"editors","funder":"funders","keyword":"keywords","license":"licenses","maintainer":"maintainers","hasParts":"parts","part":"parts","citations":"references","reference":"references","headline":"title","encodingFormat":"mediaType"},"properties":{"id":{"type":"string"},"alternateNames":{"type":"array","items":{"type":"string"},"aliases":["alternateName"]},"description":{"anyOf":[{"type":"array","items":{"$ref":"BlockContent.schema.json"}},{"type":"array","items":{"$ref":"InlineContent.schema.json"},"minItems":2},{"type":"string"}]},"identifiers":{"type":"array","items":{"anyOf":[{"$ref":"PropertyValue.schema.json"},{"type":"string"}]},"aliases":["identifier"]},"images":{"type":"array","items":{"anyOf":[{"$ref":"ImageObject.schema.json"},{"type":"string","format":"uri"}]},"aliases":["image"]},"name":{"type":"string"},"url":{"type":"string","format":"uri"},"about":{"type":"array","items":{"$ref":"ThingTypes.schema.json"}},"authors":{"allOf":[{"parser":"scsi"},{"type":"array","items":{"anyOf":[{"$ref":"Person.schema.json"},{"$ref":"Organization.schema.json"}]}}],"aliases":["author"]},"comments":{"type":"array","items":{"$ref":"Comment.schema.json"},"aliases":["comment"]},"content":{"anyOf":[{"type":"array","items":{"$ref":"Node.schema.json"}},{"type":"string"}]},"dateCreated":{"allOf":[{"$ref":"Date.schema.json"}]},"dateReceived":{"allOf":[{"$ref":"Date.schema.json"}]},"dateAccepted":{"allOf":[{"$ref":"Date.schema.json"}]},"dateModified":{"allOf":[{"$ref":"Date.schema.json"}]},"datePublished":{"aliases":["date"],"allOf":[{"$ref":"Date.schema.json"}]},"editors":{"type":"array","items":{"$ref":"Person.schema.json"},"aliases":["editor"]},"funders":{"type":"array","items":{"anyOf":[{"$ref":"Person.schema.json"},{"$ref":"Organization.schema.json"}]},"aliases":["funder"]},"fundedBy":{"type":"array","items":{"anyOf":[{"$ref":"Grant.schema.json"},{"$ref":"MonetaryGrant.schema.json"}]}},"genre":{"allOf":[{"parser":"csi"},{"type":"array","items":{"type":"string"}}]},"keywords":{"allOf":[{"parser":"csi"},{"type":"array","items":{"type":"string"}}],"aliases":["keyword"]},"isPartOf":{"$ref":"CreativeWorkTypes.schema.json"},"licenses":{"type":"array","items":{"anyOf":[{"$ref":"CreativeWorkTypes.schema.json"},{"type":"string","format":"uri"}]},"aliases":["license"]},"maintainers":{"type":"array","items":{"anyOf":[{"$ref":"Person.schema.json"},{"$ref":"Organization.schema.json"}]},"aliases":["maintainer"]},"parts":{"aliases":["hasParts","part"],"type":"array","items":{"$ref":"CreativeWorkTypes.schema.json"}},"publisher":{"anyOf":[{"$ref":"Person.schema.json"},{"$ref":"Organization.schema.json"}]},"references":{"aliases":["citations","reference"],"type":"array","items":{"anyOf":[{"$ref":"CreativeWorkTypes.schema.json"},{"type":"string"}]}},"text":{"type":"string"},"title":{"aliases":["headline"],"anyOf":[{"type":"array","items":{"$ref":"InlineContent.schema.json"},"minItems":2},{"type":"string"}]},"version":{"anyOf":[{"type":"string"},{"type":"number"}]},"bitrate":{"type":"number"},"contentSize":{"type":"number"},"contentUrl":{"type":"string"},"embedUrl":{"type":"string"},"mediaType":{"aliases":["encodingFormat"],"type":"string","pattern":"^[a-z]+(\\/[a-z\\+\\-]+)$"},"caption":{"type":"string"},"thumbnail":{"allOf":[{"$ref":"ImageObject.schema.json"}]}}}"#),
("Include", r#"{"title":"Include","required":["type","source"],"type":"object","propertyAliases":{"encodingFormat":"mediaType"},"additionalProperties":false,"properties":{"id":{"type":"string"},"source":{"type":"string"},"mediaType":{"aliases":["encodingFormat"],"type":"string"},"content":{"type":"array","items":{"$ref":"BlockContent.schema.json"}}}}"#),
("InlineContent", r#"{"title":"InlineContent","anyOf":[{"$ref":"AudioObject.schema.json"},{"$ref":"Cite.schema.json"},{"$ref":"CiteGroup.schema.json"},{"$ref":"CodeExpression.schema.json"},{"$ref":"CodeFragment.schema.json"},{"$ref":"Delete.schema.json"},{"$ref":"Emphasis.schema.json"},{"$ref":"ImageObject.schema.json"},{"$ref":"Link.schema.json"},{"$ref":"MathFragment.schema.json"},{"$ref":"NontextualAnnotation.schema.json"},{"$ref":"Note.schema.json"},{"$ref":"Quote.schema.json"},{"$ref":"Strong.schema.json"},{"$ref":"Subscript.schema.json"},{"$ref":"Superscript.schema.json"},{"$ref":"VideoObject.schema.json"},{"type":"null"},{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"}]}"#),
("InlineContent", r#"{"title":"InlineContent","anyOf":[{"$ref":"AudioObject.schema.json"},{"$ref":"Cite.schema.json"},{"$ref":"CiteGroup.schema.json"},{"$ref":"CodeExpression.schema.json"},{"$ref":"CodeFragment.schema.json"},{"$ref":"Delete.schema.json"},{"$ref":"Emphasis.schema.json"},{"$ref":"ImageObject.schema.json"},{"$ref":"Link.schema.json"},{"$ref":"MathFragment.schema.json"},{"$ref":"NontextualAnnotation.schema.json"},{"$ref":"Note.schema.json"},{"$ref":"Quote.schema.json"},{"$ref":"Strong.schema.json"},{"$ref":"Subscript.schema.json"},{"$ref":"Superscript.schema.json"},{"$ref":"VideoObject.schema.json"},{"$ref":"Null.schema.json"},{"$ref":"Boolean.schema.json"},{"$ref":"Integer.schema.json"},{"$ref":"Number.schema.json"},{"$ref":"String.schema.json"}]}"#),
("Integer", r#"{"title":"Integer","type":"integer"}"#),
("IntegerValidator", r#"{"title":"IntegerValidator","type":"object","additionalProperties":false,"required":["type"],"propertyAliases":{},"properties":{"id":{"type":"string"}}}"#),
("Link", r#"{"title":"Link","required":["type","content","target"],"examples":[{"type":"Link","content":["Stencila’s website"],"target":"https://stenci.la"}],"type":"object","propertyAliases":{"export":"exportFrom","import":"importTo"},"additionalProperties":false,"properties":{"id":{"type":"string"},"content":{"type":"array","items":{"$ref":"InlineContent.schema.json"}},"exportFrom":{"aliases":["export"],"type":"string"},"importTo":{"aliases":["import"],"type":"string"},"target":{"type":"string","format":"uri-reference"},"title":{"type":"string"},"relation":{"type":"string"}}}"#),
Expand Down
2 changes: 1 addition & 1 deletion rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5484,7 +5484,7 @@ pub enum Node {
Number(Number),
String(String),
Object(Object),
Array(Vec<Node>),
Array(Array),
}

/// All type schemas that are derived from Thing
Expand Down
2 changes: 0 additions & 2 deletions schema/Array.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ category: data
status: stable
description: A value comprised of several other values.
type: array
items:
$ref: Node
10 changes: 5 additions & 5 deletions schema/InlineContent.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ anyOf:
- $ref: VideoObject
# Primitive types in the order that coercion from
# string should be attempted (for example by Ajv, or by serde_json).
- type: 'null'
- type: boolean
- type: integer
- type: number
- type: string
- $ref: 'Null'
- $ref: Boolean
- $ref: Integer
- $ref: Number
- $ref: String
6 changes: 6 additions & 0 deletions schema/Integer.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: Integer
'@id': schema:Integer
category: data
status: stable
description: A value that is a integer
type: integer
16 changes: 7 additions & 9 deletions schema/Node.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ $comment: |
Array should come last to avoid single items (e.g. a single string)
being coerced into an array.
anyOf:
- type: 'null'
- type: boolean
- type: integer
- type: number
- type: string
- type: object
- type: array
items:
$ref: Node
- $ref: 'Null'
- $ref: Boolean
- $ref: Integer
- $ref: Number
- $ref: String
- $ref: Object
- $ref: Array
48 changes: 24 additions & 24 deletions ts/bindings/__file_snapshots__/Person.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
class Person(Thing):
"""A person (alive, dead, undead, or fictional)."""

address: Optional[Union["PostalAddress", str]] = None
address: Optional[Union["PostalAddress", String]] = None
"""Postal address for the person."""

affiliations: Optional[Array["Organization"]] = None
"""Organizations that the person is affiliated with."""

emails: Optional[Array[str]] = None
emails: Optional[Array[String]] = None
"""Email addresses for the person."""

familyNames: Optional[Array[str]] = None
familyNames: Optional[Array[String]] = None
"""Family name. In the U.S., the last name of a person."""

funders: Optional[Array[Union["Organization", "Person"]]] = None
"""A person or organization that supports (sponsors) something through
some kind of financial contribution.
"""

givenNames: Optional[Array[str]] = None
givenNames: Optional[Array[String]] = None
"""Given name. In the U.S., the first name of a person."""

honorificPrefix: Optional[str] = None
honorificPrefix: Optional[String] = None
"""An honorific prefix preceding a person's name such as Dr/Mrs/Mr."""

honorificSuffix: Optional[str] = None
honorificSuffix: Optional[String] = None
"""An honorific suffix after a person's name such as MD/PhD/MSCSW."""

jobTitle: Optional[str] = None
jobTitle: Optional[String] = None
"""The job title of the person (for example, Financial Manager)."""

memberOf: Optional[Array["Organization"]] = None
"""An organization (or program membership) to which this person belongs."""

telephoneNumbers: Optional[Array[str]] = None
telephoneNumbers: Optional[Array[String]] = None
"""Telephone numbers for the person."""


def __init__(
self,
address: Optional[Union["PostalAddress", str]] = None,
address: Optional[Union["PostalAddress", String]] = None,
affiliations: Optional[Array["Organization"]] = None,
alternateNames: Optional[Array[str]] = None,
description: Optional[Union[Array["BlockContent"], Array["InlineContent"], str]] = None,
emails: Optional[Array[str]] = None,
familyNames: Optional[Array[str]] = None,
alternateNames: Optional[Array[String]] = None,
description: Optional[Union[Array["BlockContent"], Array["InlineContent"], String]] = None,
emails: Optional[Array[String]] = None,
familyNames: Optional[Array[String]] = None,
funders: Optional[Array[Union["Organization", "Person"]]] = None,
givenNames: Optional[Array[str]] = None,
honorificPrefix: Optional[str] = None,
honorificSuffix: Optional[str] = None,
id: Optional[str] = None,
identifiers: Optional[Array[Union["PropertyValue", str]]] = None,
images: Optional[Array[Union["ImageObject", str]]] = None,
jobTitle: Optional[str] = None,
givenNames: Optional[Array[String]] = None,
honorificPrefix: Optional[String] = None,
honorificSuffix: Optional[String] = None,
id: Optional[String] = None,
identifiers: Optional[Array[Union["PropertyValue", String]]] = None,
images: Optional[Array[Union["ImageObject", String]]] = None,
jobTitle: Optional[String] = None,
memberOf: Optional[Array["Organization"]] = None,
meta: Optional[Dict[str, Any]] = None,
name: Optional[str] = None,
telephoneNumbers: Optional[Array[str]] = None,
url: Optional[str] = None
meta: Optional[Object] = None,
name: Optional[String] = None,
telephoneNumbers: Optional[Array[String]] = None,
url: Optional[String] = None
) -> None:
super().__init__(
alternateNames=alternateNames,
Expand Down
23 changes: 16 additions & 7 deletions ts/bindings/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ async function build(): Promise<void> {
from typing import Any, Dict, List as Array, Optional, Union
from enum import Enum
Null = None
Boolean = bool
Number = float
Integer = int
String = str
Object = Dict[str, Any]
${globalsCode}
${classesCode}
Expand Down Expand Up @@ -223,20 +230,22 @@ function schemaToType(schema: JsonSchema): string {

if ($ref !== undefined) {
const title = $ref.replace('.schema.json', '')
return `"${title === 'Node' ? 'Any' : title}"`
if (title === 'Null') return 'None'
else if (title === 'Node') return 'Any'
else return `"${title}"`
}

if (anyOf !== undefined) return anyOfToType(anyOf)
if (allOf !== undefined) return allOfToType(allOf)
if (schema.enum !== undefined) return enumToType(schema['@id'], schema.enum)

if (type === 'null') return 'None'
if (type === 'boolean') return 'bool'
if (type === 'number') return 'float'
if (type === 'integer') return 'int'
if (type === 'string') return 'str'
if (type === 'null') return 'Null'
if (type === 'boolean') return 'Boolean'
if (type === 'number') return 'Number'
if (type === 'integer') return 'Integer'
if (type === 'string') return 'String'
if (type === 'array') return arrayToType(schema)
if (type === 'object') return 'Dict[str, Any]'
if (type === 'object') return 'Object'

throw new Error(`Unhandled schema: ${JSON.stringify(schema)}`)
}
Expand Down
22 changes: 20 additions & 2 deletions ts/bindings/r.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import fs from 'fs-extra'
import { JSONSchema7TypeName } from 'json-schema'
import path from 'path'
import { JsonSchema } from '../JsonSchema'
import {
Expand Down Expand Up @@ -159,9 +160,26 @@ function docComment(description: string, tags: string[] = []): string {
* Convert a schema definition to a R class
*/
function schemaToType(schema: JsonSchema): string {
const { type, anyOf, allOf, $ref } = schema
let { type, anyOf, allOf, $ref } = schema

if ($ref !== undefined) return `${$ref.replace('.schema.json', '')}`
if ($ref !== undefined) {
const name = $ref.replace('.schema.json', '')
if (
[
'Null',
'Boolean',
'Integer',
'Number',
'String',
'Array',
'Object',
].includes(name)
) {
type = name.toLowerCase() as JSONSchema7TypeName
} else {
return `${name}`
}
}
if (anyOf !== undefined) return anyOfToType(anyOf)
if (allOf !== undefined) return allOfToType(allOf)
if (schema.enum !== undefined) return enumToType(schema.enum)
Expand Down
4 changes: 2 additions & 2 deletions ts/bindings/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
if (require.main) build()

async function build() {
async function build(): Promise<void> {
await buildTypes()
await buildSchemas()
}
Expand Down Expand Up @@ -489,7 +489,7 @@ function arrayToType(schema: JsonSchema, context: Context): string {
* To reduce memory consumption, only keeps needed properties when
* serializing.
*/
async function buildSchemas() {
async function buildSchemas(): Promise<void> {
const schemas = Object.entries(await jsonSchemas())
.map(([name, schema]) => {
let {
Expand Down

0 comments on commit 54a8264

Please sign in to comment.