ipld-schema
Advanced tools
Comparing version 1.1.6 to 2.0.0
@@ -0,1 +1,18 @@ | ||
## [2.0.0](https://github.com/rvagg/js-ipld-schema/compare/v1.1.6...v2.0.0) (2021-09-13) | ||
### ⚠ BREAKING CHANGES | ||
* add stringprefix, rename byteprefix->bytesprefix | ||
### Features | ||
* add stringprefix, rename byteprefix->bytesprefix ([81aa1dd](https://github.com/rvagg/js-ipld-schema/commit/81aa1ddfae67abfd90af8e0020b425296601e6b4)) | ||
### Trivial Changes | ||
* add stringprefix union test case ([3d9ad09](https://github.com/rvagg/js-ipld-schema/commit/3d9ad090c9441d004fecba74b30a9e351438388d)) | ||
* fix json formatting to match go fixtures (orderd, properly formatted) ([a77c409](https://github.com/rvagg/js-ipld-schema/commit/a77c40975e6bd2bea6633d80b59ccbe16563c2cc)) | ||
### [1.1.6](https://github.com/rvagg/js-ipld-schema/compare/v1.1.5...v1.1.6) (2021-08-06) | ||
@@ -2,0 +19,0 @@ |
{ | ||
"name": "ipld-schema", | ||
"version": "1.1.6", | ||
"version": "2.0.0", | ||
"description": "IPLD Schema parser and CLI utilities for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "./ipld-schema.js", |
62
print.js
@@ -103,7 +103,8 @@ const noop = (s) => s | ||
if (defn.representation) { | ||
if (typeof defn.representation.listpairs === 'object') { | ||
const repr = reprStrategy(defn) | ||
if (repr === 'listpairs') { | ||
str += ` ${highlighter.builtin('representation')} listpairs` | ||
} else if (typeof defn.representation.stringpairs === 'object') { | ||
} else if (repr === 'stringpairs') { | ||
str += stringpairs(indent, 'map', defn.representation.stringpairs, highlighter) | ||
} else if (typeof defn.representation.advanced === 'string') { | ||
} else if (repr === 'advanced') { | ||
str += ` ${highlighter.builtin('representation')} advanced ${defn.representation.advanced}` | ||
@@ -124,3 +125,3 @@ } | ||
if (defn.representation) { | ||
if (typeof defn.representation.advanced === 'string') { | ||
if (reprStrategy(defn) === 'advanced') { | ||
str += ` ${highlighter.builtin('representation')} advanced ${defn.representation.advanced}` | ||
@@ -175,5 +176,6 @@ } | ||
if (defn.representation) { | ||
if (typeof defn.representation.listpairs === 'object') { | ||
const repr = reprStrategy(defn) | ||
if (repr === 'listpairs') { | ||
str += ` ${highlighter.builtin('representation')} listpairs` | ||
} else if (typeof defn.representation.stringjoin === 'object') { | ||
} else if (repr === 'stringjoin') { | ||
if (typeof defn.representation.stringjoin.join !== 'string') { | ||
@@ -186,5 +188,5 @@ throw new Error('Invalid schema, struct stringjoin representations require an join string') | ||
str += highlighter.punctuation('}') | ||
} else if (typeof defn.representation.stringpairs === 'object') { | ||
} else if (repr === 'stringpairs') { | ||
str += stringpairs(indent, 'struct', defn.representation.stringpairs, highlighter) | ||
} else if (typeof defn.representation.tuple === 'object') { | ||
} else if (repr === 'tuple') { | ||
str += ` ${highlighter.builtin('representation')} tuple` | ||
@@ -196,3 +198,3 @@ if (Array.isArray(defn.representation.tuple.fieldOrder)) { | ||
} | ||
} else if (typeof defn.representation.advanced === 'string') { | ||
} else if (repr === 'advanced') { | ||
str += ` ${highlighter.builtin('representation')} advanced ${defn.representation.advanced}` | ||
@@ -229,2 +231,23 @@ } | ||
function reprStrategy (defn) { | ||
if (typeof defn.representation !== 'object') { | ||
throw new Error('Expected \'representation\' property of definition') | ||
} | ||
const keys = Object.keys(defn.representation) | ||
if (keys.length !== 1) { | ||
throw new Error('Expected exactly one \'representation\' field') | ||
} | ||
const repr = keys[0] | ||
if (repr === 'advanced') { | ||
if (typeof defn.representation[repr] !== 'string') { | ||
throw new Error('Expected representation \'advanced\' to be an string') | ||
} | ||
} else { | ||
if (typeof defn.representation[repr] !== 'object') { | ||
throw new Error(`Expected representation '${repr}' to be an object`) | ||
} | ||
} | ||
return repr | ||
} | ||
printTypeTerm.union = function union (defn, indent, highlighter) { | ||
@@ -236,4 +259,5 @@ if (typeof defn.representation !== 'object') { | ||
let str = highlighter.punctuation('{') | ||
const repr = reprStrategy(defn) | ||
if (typeof defn.representation.kinded === 'object') { | ||
if (repr === 'kinded') { | ||
for (const [kind, type] of Object.entries(defn.representation.kinded)) { | ||
@@ -243,13 +267,8 @@ str += `\n${indent}${highlighter.punctuation('|')} ${printTypeTerm(type, indent, highlighter)} ${kind}` | ||
str += `\n${highlighter.punctuation('}')} ${highlighter.builtin('representation')} kinded` | ||
} else if (typeof defn.representation.keyed === 'object') { | ||
for (const [key, type] of Object.entries(defn.representation.keyed)) { | ||
} else if (repr === 'keyed' || repr === 'stringprefix' || repr === 'bytesprefix') { | ||
for (const [key, type] of Object.entries(defn.representation[repr])) { | ||
str += `\n${indent}${highlighter.punctuation('|')} ${printTypeTerm(type, indent, highlighter)} ${highlighter.string(`"${key}"`)}` | ||
} | ||
str += `\n${highlighter.punctuation('}')} ${highlighter.builtin('representation')} keyed` | ||
} else if (typeof defn.representation.byteprefix === 'object') { | ||
for (const [type, key] of Object.entries(defn.representation.byteprefix)) { | ||
str += `\n${indent}${highlighter.punctuation('|')} ${printTypeTerm(type, indent, highlighter)} ${key}` | ||
} | ||
str += `\n${highlighter.punctuation('}')} ${highlighter.builtin('representation')} byteprefix` | ||
} else if (typeof defn.representation.inline === 'object') { | ||
str += `\n${highlighter.punctuation('}')} ${highlighter.builtin('representation')} ${repr}` | ||
} else if (repr === 'inline') { | ||
if (typeof defn.representation.inline.discriminantTable !== 'object') { | ||
@@ -265,3 +284,3 @@ throw new Error('Invalid schema, inline unions require a discriminantTable map') | ||
str += `\n${highlighter.punctuation('}')} ${highlighter.builtin('representation')} inline ${highlighter.punctuation('{')}\n${indent}discriminantKey ${highlighter.string(`"${defn.representation.inline.discriminantKey}"`)}\n${highlighter.punctuation('}')}` | ||
} else if (typeof defn.representation.envelope === 'object') { | ||
} else if (repr === 'envelope') { | ||
if (typeof defn.representation.envelope.discriminantTable !== 'object') { | ||
@@ -294,3 +313,4 @@ throw new Error('Invalid schema, envelope unions require a discriminantTable map') | ||
} | ||
if (typeof defn.representation.string !== 'object' && typeof defn.representation.int !== 'object') { | ||
const repr = reprStrategy(defn) | ||
if (repr !== 'string' && repr !== 'int') { | ||
throw new Error('Invalid schema, enum requires a "string" or "int" representation map') | ||
@@ -297,0 +317,0 @@ } |
@@ -67,3 +67,4 @@ export type KindBool = boolean | ||
| { inline: UnionRepresentation_Inline } | ||
| { byteprefix: UnionRepresentation_BytePrefix } | ||
| { stringprefix: UnionRepresentation_StringPrefix } | ||
| { bytesprefix: UnionRepresentation_BytesPrefix } | ||
export type UnionRepresentation_Kinded = { [k in RepresentationKind]?: KindedType } | ||
@@ -81,3 +82,4 @@ export type KindedType = TypeName | TypeLink | ||
} | ||
export type UnionRepresentation_BytePrefix = { [ k in TypeName]: KindInt } | ||
export type UnionRepresentation_StringPrefix = { [ k in KindString]: TypeName } | ||
export type UnionRepresentation_BytesPrefix = { [ k in KindString]: TypeName } | ||
export type TypeStruct = { | ||
@@ -84,0 +86,0 @@ kind: "struct" |
@@ -41,2 +41,4 @@ // A run-once script to split up schema-schema.ipldsch into a markdown file per type | ||
block.push('```ipldsch') | ||
const title = line.match(/^type ([A-Z]\w+)/) | ||
block.unshift(`# schema-schema: \`${title[1]}\``) | ||
} else if (inType && line.startsWith('#')) { | ||
@@ -47,4 +49,2 @@ await writeBlock() | ||
if (!block.length) { | ||
const title = line.match(/[A-Z]\w+/) | ||
block.push(`# schema-schema: \`${title[0]}\``) | ||
block.push('') | ||
@@ -51,0 +51,0 @@ } |
@@ -304,3 +304,3 @@ { | ||
"inline": "UnionRepresentation_Inline", | ||
"byteprefix": "UnionRepresentation_BytePrefix" | ||
"bytesprefix": "UnionRepresentation_BytesPrefix" | ||
} | ||
@@ -358,16 +358,6 @@ } | ||
}, | ||
"UnionRepresentation_BytePrefix": { | ||
"kind": "struct", | ||
"fields": { | ||
"discriminantTable": { | ||
"type": { | ||
"kind": "map", | ||
"keyType": "TypeName", | ||
"valueType": "Int" | ||
} | ||
} | ||
}, | ||
"representation": { | ||
"map": {} | ||
} | ||
"UnionRepresentation_BytesPrefix": { | ||
"kind": "map", | ||
"keyType": "String", | ||
"valueType": "TypeName" | ||
}, | ||
@@ -548,3 +538,3 @@ "TypeStruct": { | ||
"valueType": "Null" | ||
} | ||
} | ||
}, | ||
@@ -559,2 +549,5 @@ "representation": { | ||
}, | ||
"EnumValue": { | ||
"kind": "string" | ||
}, | ||
"EnumRepresentation": { | ||
@@ -569,5 +562,2 @@ "kind": "union", | ||
}, | ||
"EnumValue": { | ||
"kind": "string" | ||
}, | ||
"EnumRepresentation_String": { | ||
@@ -574,0 +564,0 @@ "kind": "map", |
@@ -37,3 +37,3 @@ # schema-schema: `StructField` | ||
nullable Bool (implicit "false") | ||
} representation map | ||
} | ||
``` |
@@ -1,2 +0,2 @@ | ||
# schema-schema: `The` | ||
# schema-schema: `Type` | ||
@@ -3,0 +3,0 @@ The types of Type are a union. |
@@ -16,5 +16,5 @@ # schema-schema: `TypeEnum` | ||
type TypeEnum struct { | ||
members {EnumValue:Null} | ||
members {EnumValue:Null} | ||
representation EnumRepresentation | ||
} | ||
``` |
@@ -12,3 +12,3 @@ # schema-schema: `TypeList` | ||
representation ListRepresentation | ||
} representation map | ||
} | ||
``` |
@@ -6,10 +6,14 @@ # schema-schema: `TypeMap` | ||
A constraint on keyType is that the referenced type must have a string | ||
representation kind. The IPLD Data Model only allows for string keys on maps, | ||
so this constraint is imposed here. | ||
```ipldsch | ||
type TypeMap struct { | ||
keyType TypeName # additionally, the referenced type must be reprkind==string. | ||
keyType TypeName | ||
valueType TypeTerm | ||
valueNullable Bool (implicit "false") | ||
representation MapRepresentation | ||
} representation map | ||
} | ||
``` |
@@ -1,10 +0,10 @@ | ||
# schema-schema: `Type` | ||
# schema-schema: `TypeName` | ||
Type names are a simple alias of string. | ||
There are some additional rules that should be applied: | ||
- Type names should by convention begin with a capital letter; | ||
- Type names must be all printable characters (no whitespace); | ||
- Type names must not contain punctuation other than underscores | ||
(dashes, dots, etc.). | ||
There are some additional rules that should be applied. Type names: | ||
- *Must* only contain alphanumeric ASCII characters and underscores | ||
- *Must* begin with a capital letter | ||
- *Should* avoid more than one connected underscore character, | ||
multiple-underscores may be used for codegen | ||
@@ -11,0 +11,0 @@ Type names are strings meant for human consumption at a local scope. |
@@ -1,2 +0,2 @@ | ||
# schema-schema: `Envelope` | ||
# schema-schema: `UnionRepresentation_Envelope` | ||
@@ -3,0 +3,0 @@ "Envelope" union representations will encode as a map, where the map has |
@@ -1,2 +0,2 @@ | ||
# schema-schema: `Inline` | ||
# schema-schema: `UnionRepresentation_Inline` | ||
@@ -15,3 +15,3 @@ "Inline" union representations require that all of their members encode | ||
When designing a new protocol, use inline unions sparringly; despite | ||
When designing a new protocol, use inline unions sparingly; despite | ||
appearing simple, they have the most edge cases of any kind of union | ||
@@ -18,0 +18,0 @@ representation, and their implementation is generally the most complex and |
@@ -1,2 +0,2 @@ | ||
# schema-schema: `Keyed` | ||
# schema-schema: `UnionRepresentation_Keyed` | ||
@@ -3,0 +3,0 @@ "Keyed" union representations will encode as a map, where the map has |
@@ -1,2 +0,2 @@ | ||
# schema-schema: `Kinded` | ||
# schema-schema: `UnionRepresentation_Kinded` | ||
@@ -3,0 +3,0 @@ "Kinded" union representations describe a bidirectional mapping between |
@@ -7,7 +7,7 @@ # schema-schema: `UnionRepresentation` | ||
There are five strategies that can be used to encode a union: | ||
"keyed", "envelope", "inline", "byteprefix", and "kinded". | ||
"keyed", "envelope", "inline", "bytesprefix", and "kinded". | ||
The "keyed", "envelope", and "inline" strategies are all ways to produce | ||
representations in a map format, using map keys as type discriminators | ||
(some literature may describe this as a "tagged" style of union). | ||
The "byteprefix" strategy, only available only for unions in which all | ||
The "bytesprefix" strategy, only available only for unions in which all | ||
member types themselves represent as bytes in the data model, uses another | ||
@@ -30,4 +30,4 @@ byte as the type discrimination hint (and like the map-oriented strategies, | ||
| UnionRepresentation_Inline "inline" | ||
| UnionRepresentation_BytePrefix "byteprefix" | ||
| UnionRepresentation_BytesPrefix "bytesprefix" | ||
} representation keyed | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
265373
5471