From 0dc6e7c45eec5873e3ccab17a1332d16ef4bc467 Mon Sep 17 00:00:00 2001 From: John Moore Date: Fri, 30 Aug 2024 10:30:06 +0100 Subject: [PATCH] support SpecificResource in annotation body --- package.json | 2 +- src/adapter.ts | 6 +++-- src/specification.atd | 7 +++++- src/specification.ts | 52 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 1b2e5d2..a791091 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "maniiifest", - "version": "1.1.0", + "version": "1.1.1", "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ diff --git a/src/adapter.ts b/src/adapter.ts index c6ff72b..acc09db 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -68,10 +68,12 @@ export function restore_annotation_body(x: T, context: any = x, fn: (input export function normalize_annotation_body(x: T, context: any = x, fn: (input: [string, T], context: any) => R): R { if (x.type === 'Image' || x.type == 'Video' || x.type == 'Audio' || x.type == 'Sound' || x.type == 'Text') { return fn(['T1', x], context); + } else if (x.type === 'SpecificResource') { + return fn(['T2', x], context); } else if (x.type === 'TextualBody') { - return fn(['T2', x], context); + return fn(['T3', x], context); } else if (x.type === 'Choice') { - return fn(['T3', x], context) + return fn(['T4', x], context) } else { throw new Error(`${JSON.stringify(x)}: Input type did not match expected types.`); } diff --git a/src/specification.atd b/src/specification.atd index 5b4d20f..01cb734 100644 --- a/src/specification.atd +++ b/src/specification.atd @@ -210,6 +210,7 @@ type annotation_body_t = [ | T1 of annotation_body_t1 | T2 of annotation_body_t2 | T3 of annotation_body_t3 + | T4 of annotation_body_t4 ] type annotation_body_t1 = { @@ -217,13 +218,17 @@ type annotation_body_t1 = { } type annotation_body_t2 = { + inherit specific_resource_t; +} + +type annotation_body_t3 = { _type : type_t; ?language : language_t option; ?format : format_t option; ?value : value_t option; } -type annotation_body_t3 = { +type annotation_body_t4 = { ?_type : type_t option; items : resource_t list; } diff --git a/src/specification.ts b/src/specification.ts index 9642ce2..c53dce4 100644 --- a/src/specification.ts +++ b/src/specification.ts @@ -291,6 +291,7 @@ export type AnnotationBodyT = | { kind: 'T1'; value: AnnotationBodyT1 } | { kind: 'T2'; value: AnnotationBodyT2 } | { kind: 'T3'; value: AnnotationBodyT3 } +| { kind: 'T4'; value: AnnotationBodyT4 } export type AnnotationBodyT1 = { id: IdT; @@ -309,13 +310,22 @@ export type AnnotationBodyT1 = { } export type AnnotationBodyT2 = { + id?: IdT; + type?: TypeT; + format?: FormatT; + accessibility?: AccessibilityT; + source: SourceT; + selector: SelectorT; +} + +export type AnnotationBodyT3 = { type: TypeT; language?: LanguageT; format?: FormatT; value?: ValueT; } -export type AnnotationBodyT3 = { +export type AnnotationBodyT4 = { type?: TypeT; items: ResourceT[]; } @@ -1337,6 +1347,8 @@ export function _writeAnnotationBodyT(x: AnnotationBodyT, context: any = x): any return ['T2', writeAnnotationBodyT2(x.value, x)] case 'T3': return ['T3', writeAnnotationBodyT3(x.value, x)] + case 'T4': + return ['T4', writeAnnotationBodyT4(x.value, x)] } } @@ -1349,6 +1361,8 @@ export function _readAnnotationBodyT(x: any, context: any = x): AnnotationBodyT return { kind: 'T2', value: readAnnotationBodyT2(x[1], x) } case 'T3': return { kind: 'T3', value: readAnnotationBodyT3(x[1], x) } + case 'T4': + return { kind: 'T4', value: readAnnotationBodyT4(x[1], x) } default: _atd_bad_json('AnnotationBodyT', x, context) throw new Error('impossible') @@ -1393,33 +1407,55 @@ export function readAnnotationBodyT1(x: any, context: any = x): AnnotationBodyT1 export function writeAnnotationBodyT2(x: AnnotationBodyT2, context: any = x): any { return { - 'type': _atd_write_required_field('AnnotationBodyT2', 'type', writeTypeT, x.type, x), + 'id': _atd_write_optional_field(writeIdT, x.id, x), + 'type': _atd_write_optional_field(writeTypeT, x.type, x), + 'format': _atd_write_optional_field(writeFormatT, x.format, x), + 'accessibility': _atd_write_optional_field(writeAccessibilityT, x.accessibility, x), + 'source': _atd_write_required_field('AnnotationBodyT2', 'source', writeSourceT, x.source, x), + 'selector': _atd_write_required_field('AnnotationBodyT2', 'selector', writeSelectorT, x.selector, x), + }; +} + +export function readAnnotationBodyT2(x: any, context: any = x): AnnotationBodyT2 { + return { + id: _atd_read_optional_field(readIdT, x['id'], x), + type: _atd_read_optional_field(readTypeT, x['type'], x), + format: _atd_read_optional_field(readFormatT, x['format'], x), + accessibility: _atd_read_optional_field(readAccessibilityT, x['accessibility'], x), + source: _atd_read_required_field('AnnotationBodyT2', 'source', readSourceT, x['source'], x), + selector: _atd_read_required_field('AnnotationBodyT2', 'selector', readSelectorT, x['selector'], x), + }; +} + +export function writeAnnotationBodyT3(x: AnnotationBodyT3, context: any = x): any { + return { + 'type': _atd_write_required_field('AnnotationBodyT3', 'type', writeTypeT, x.type, x), 'language': _atd_write_optional_field(writeLanguageT, x.language, x), 'format': _atd_write_optional_field(writeFormatT, x.format, x), 'value': _atd_write_optional_field(writeValueT, x.value, x), }; } -export function readAnnotationBodyT2(x: any, context: any = x): AnnotationBodyT2 { +export function readAnnotationBodyT3(x: any, context: any = x): AnnotationBodyT3 { return { - type: _atd_read_required_field('AnnotationBodyT2', 'type', readTypeT, x['type'], x), + type: _atd_read_required_field('AnnotationBodyT3', 'type', readTypeT, x['type'], x), language: _atd_read_optional_field(readLanguageT, x['language'], x), format: _atd_read_optional_field(readFormatT, x['format'], x), value: _atd_read_optional_field(readValueT, x['value'], x), }; } -export function writeAnnotationBodyT3(x: AnnotationBodyT3, context: any = x): any { +export function writeAnnotationBodyT4(x: AnnotationBodyT4, context: any = x): any { return { 'type': _atd_write_optional_field(writeTypeT, x.type, x), - 'items': _atd_write_required_field('AnnotationBodyT3', 'items', _atd_write_array(writeResourceT), x.items, x), + 'items': _atd_write_required_field('AnnotationBodyT4', 'items', _atd_write_array(writeResourceT), x.items, x), }; } -export function readAnnotationBodyT3(x: any, context: any = x): AnnotationBodyT3 { +export function readAnnotationBodyT4(x: any, context: any = x): AnnotationBodyT4 { return { type: _atd_read_optional_field(readTypeT, x['type'], x), - items: _atd_read_required_field('AnnotationBodyT3', 'items', _atd_read_array(readResourceT), x['items'], x), + items: _atd_read_required_field('AnnotationBodyT4', 'items', _atd_read_array(readResourceT), x['items'], x), }; }