Skip to content

Commit

Permalink
support SpecificResource in annotation body
Browse files Browse the repository at this point in the history
  • Loading branch information
jptmoore committed Aug 30, 2024
1 parent d36f4e7 commit 0dc6e7c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maniiifest",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
6 changes: 4 additions & 2 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ export function restore_annotation_body<T, R>(x: T, context: any = x, fn: (input
export function normalize_annotation_body<T extends { type: string }, R>(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.`);
}
Expand Down
7 changes: 6 additions & 1 deletion src/specification.atd
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,25 @@ 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 = {
inherit resource_t;
}

type annotation_body_t2 = {
inherit specific_resource_t;
}

type annotation_body_t3 = {
_type <json name="type"> : type_t;
?language : language_t option;
?format : format_t option;
?value : value_t option;
}

type annotation_body_t3 = {
type annotation_body_t4 = {
?_type <json name="type"> : type_t option;
items : resource_t list;
}
Expand Down
52 changes: 44 additions & 8 deletions src/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[];
}
Expand Down Expand Up @@ -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)]
}
}

Expand All @@ -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')
Expand Down Expand Up @@ -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),
};
}

Expand Down

0 comments on commit 0dc6e7c

Please sign in to comment.