Skip to content

Commit

Permalink
fix to body choice
Browse files Browse the repository at this point in the history
  • Loading branch information
jptmoore committed Sep 3, 2024
1 parent 66b45b2 commit e1fc649
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 8 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.3",
"version": "1.1.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
3 changes: 3 additions & 0 deletions scripts/compilespec-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ sed -i 's/export function readMotivationT(/export function _readMotivationT(/g'
sed -i 's/export function writeAnnotationBodyT(/export function _writeAnnotationBodyT(/g' specification.ts
sed -i 's/export function readAnnotationBodyT(/export function _readAnnotationBodyT(/g' specification.ts

sed -i 's/export function writeAnnotationBodyItemsT(/export function _writeAnnotationBodyItemsT(/g' specification.ts
sed -i 's/export function readAnnotationBodyItemsT(/export function _readAnnotationBodyItemsT(/g' specification.ts

sed -i 's/export function writeAnnotationTargetT(/export function _writeAnnotationTargetT(/g' specification.ts
sed -i 's/export function readAnnotationTargetT(/export function _readAnnotationTargetT(/g' specification.ts

Expand Down
3 changes: 3 additions & 0 deletions scripts/compilespec-mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ sed -i '' 's/export function readMotivationT(/export function _readMotivationT(/
sed -i '' 's/export function writeAnnotationBodyT(/export function _writeAnnotationBodyT(/g' specification.ts
sed -i '' 's/export function readAnnotationBodyT(/export function _readAnnotationBodyT(/g' specification.ts

sed -i 's/export function writeAnnotationBodyItemsT(/export function _writeAnnotationBodyItemsT(/g' specification.ts
sed -i 's/export function readAnnotationBodyItemsT(/export function _readAnnotationBodyItemsT(/g' specification.ts

sed -i '' 's/export function writeAnnotationTargetT(/export function _writeAnnotationTargetT(/g' specification.ts
sed -i '' 's/export function readAnnotationTargetT(/export function _readAnnotationTargetT(/g' specification.ts

Expand Down
10 changes: 9 additions & 1 deletion scripts/use_adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///// appended to specification.ts

import { normalize_language, restore_language, normalize_geometry, restore_geometry, normalize_range_items, restore_range_items, normalize_resource_selector, restore_resource_selector, normalize_label, restore_label, normalize_first, restore_first, normalize_body, restore_body, normalize_target, restore_target, normalize_source, restore_source, normalize_selector, restore_selector, normalize_annotation_body, restore_annotation_body, normalize_annotation_target, restore_annotation_target, normalize_specification, restore_specification, normalize_service, restore_service, normalize_motivation, restore_motivation } from "./adapter";
import { normalize_annotation_body_items, restore_annotation_body_items, normalize_language, restore_language, normalize_geometry, restore_geometry, normalize_range_items, restore_range_items, normalize_resource_selector, restore_resource_selector, normalize_label, restore_label, normalize_first, restore_first, normalize_body, restore_body, normalize_target, restore_target, normalize_source, restore_source, normalize_selector, restore_selector, normalize_annotation_body, restore_annotation_body, normalize_annotation_target, restore_annotation_target, normalize_specification, restore_specification, normalize_service, restore_service, normalize_motivation, restore_motivation } from "./adapter";

export function writeSpecificationT(x: any, context: any = x): SpecificationT {
return restore_specification(x, context, _writeSpecificationT);
Expand Down Expand Up @@ -35,6 +35,14 @@ export function readAnnotationBodyT(x: any, context: any = x): AnnotationBodyT {
return normalize_annotation_body(x, context, _readAnnotationBodyT);
}

export function writeAnnotationBodyItemsT(x: any, context: any = x): AnnotationBodyItemsT {
return restore_annotation_body_items(x, context, _writeAnnotationBodyItemsT);
}

export function readAnnotationBodyItemsT(x: any, context: any = x): AnnotationBodyItemsT {
return normalize_annotation_body_items(x, context, _readAnnotationBodyItemsT);
}

export function writeAnnotationTargetT(x: any, context: any = x): AnnotationTargetT {
return restore_annotation_target(x, context, _writeAnnotationTargetT);
}
Expand Down
21 changes: 21 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ export function normalize_annotation_body<T extends { type: string }, R>(x: T, c
}
}

export function restore_annotation_body_items<T, R>(x: T, context: any = x, fn: (input: T, context: any) => R[]): R {
const resultList = fn(x, context);
if (resultList.length < 2) {
throw new Error(`${JSON.stringify(x)}: Result array must contain at least two items.`);
}
return resultList[1];
}

export function normalize_annotation_body_items<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(['T3', x], context);
} else {
throw new Error(`${JSON.stringify(x)}: Input type did not match expected types.`);
}
}


export function restore_annotation_target<T, R>(x: T, context: any = x, fn: (input: T, context: any) => R[]): R {
const resultList = fn(x, context);
if (resultList.length < 2) {
Expand Down
8 changes: 6 additions & 2 deletions src/specification.atd
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ type body_t = [
| T2 of annotation_body_t list
]

type annotation_body_t = [
type annotation_body_items_t = [
| T1 of annotation_body_t1
| T2 of annotation_body_t2
| T3 of annotation_body_t3
]

type annotation_body_t = [
inherit annotation_body_items_t
| T4 of annotation_body_t4
]

Expand All @@ -230,7 +234,7 @@ type annotation_body_t3 = {

type annotation_body_t4 = {
?_type <json name="type"> : type_t option;
items : resource_t list;
items : annotation_body_items_t list;
}

type target_t = [
Expand Down
47 changes: 43 additions & 4 deletions src/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ export type BodyT =
| { kind: 'T1'; value: AnnotationBodyT }
| { kind: 'T2'; value: AnnotationBodyT[] }

export type AnnotationBodyItemsT =
| { kind: 'T1'; value: AnnotationBodyT1 }
| { kind: 'T2'; value: AnnotationBodyT2 }
| { kind: 'T3'; value: AnnotationBodyT3 }

export type AnnotationBodyT =
| { kind: 'T1'; value: AnnotationBodyT1 }
| { kind: 'T2'; value: AnnotationBodyT2 }
Expand Down Expand Up @@ -327,7 +332,7 @@ export type AnnotationBodyT3 = {

export type AnnotationBodyT4 = {
type?: TypeT;
items: ResourceT[];
items: AnnotationBodyItemsT[];
}

export type TargetT =
Expand Down Expand Up @@ -1345,6 +1350,32 @@ export function _readBodyT(x: any, context: any = x): BodyT {
}
}

export function _writeAnnotationBodyItemsT(x: AnnotationBodyItemsT, context: any = x): any {
switch (x.kind) {
case 'T1':
return ['T1', writeAnnotationBodyT1(x.value, x)]
case 'T2':
return ['T2', writeAnnotationBodyT2(x.value, x)]
case 'T3':
return ['T3', writeAnnotationBodyT3(x.value, x)]
}
}

export function _readAnnotationBodyItemsT(x: any, context: any = x): AnnotationBodyItemsT {
_atd_check_json_tuple(2, x, context)
switch (x[0]) {
case 'T1':
return { kind: 'T1', value: readAnnotationBodyT1(x[1], x) }
case 'T2':
return { kind: 'T2', value: readAnnotationBodyT2(x[1], x) }
case 'T3':
return { kind: 'T3', value: readAnnotationBodyT3(x[1], x) }
default:
_atd_bad_json('AnnotationBodyItemsT', x, context)
throw new Error('impossible')
}
}

export function _writeAnnotationBodyT(x: AnnotationBodyT, context: any = x): any {
switch (x.kind) {
case 'T1':
Expand Down Expand Up @@ -1454,14 +1485,14 @@ export function readAnnotationBodyT3(x: any, context: any = x): AnnotationBodyT3
export function writeAnnotationBodyT4(x: AnnotationBodyT4, context: any = x): any {
return {
'type': _atd_write_optional_field(writeTypeT, x.type, x),
'items': _atd_write_required_field('AnnotationBodyT4', 'items', _atd_write_array(writeResourceT), x.items, x),
'items': _atd_write_required_field('AnnotationBodyT4', 'items', _atd_write_array(writeAnnotationBodyItemsT), x.items, x),
};
}

export function readAnnotationBodyT4(x: any, context: any = x): AnnotationBodyT4 {
return {
type: _atd_read_optional_field(readTypeT, x['type'], x),
items: _atd_read_required_field('AnnotationBodyT4', 'items', _atd_read_array(readResourceT), x['items'], x),
items: _atd_read_required_field('AnnotationBodyT4', 'items', _atd_read_array(readAnnotationBodyItemsT), x['items'], x),
};
}

Expand Down Expand Up @@ -2845,7 +2876,7 @@ function _atd_write_field_with_default<T>(

///// appended to specification.ts

import { normalize_language, restore_language, normalize_geometry, restore_geometry, normalize_range_items, restore_range_items, normalize_resource_selector, restore_resource_selector, normalize_label, restore_label, normalize_first, restore_first, normalize_body, restore_body, normalize_target, restore_target, normalize_source, restore_source, normalize_selector, restore_selector, normalize_annotation_body, restore_annotation_body, normalize_annotation_target, restore_annotation_target, normalize_specification, restore_specification, normalize_service, restore_service, normalize_motivation, restore_motivation } from "./adapter";
import { normalize_annotation_body_items, restore_annotation_body_items, normalize_language, restore_language, normalize_geometry, restore_geometry, normalize_range_items, restore_range_items, normalize_resource_selector, restore_resource_selector, normalize_label, restore_label, normalize_first, restore_first, normalize_body, restore_body, normalize_target, restore_target, normalize_source, restore_source, normalize_selector, restore_selector, normalize_annotation_body, restore_annotation_body, normalize_annotation_target, restore_annotation_target, normalize_specification, restore_specification, normalize_service, restore_service, normalize_motivation, restore_motivation } from "./adapter";

export function writeSpecificationT(x: any, context: any = x): SpecificationT {
return restore_specification(x, context, _writeSpecificationT);
Expand Down Expand Up @@ -2880,6 +2911,14 @@ export function readAnnotationBodyT(x: any, context: any = x): AnnotationBodyT {
return normalize_annotation_body(x, context, _readAnnotationBodyT);
}

export function writeAnnotationBodyItemsT(x: any, context: any = x): AnnotationBodyItemsT {
return restore_annotation_body_items(x, context, _writeAnnotationBodyItemsT);
}

export function readAnnotationBodyItemsT(x: any, context: any = x): AnnotationBodyItemsT {
return normalize_annotation_body_items(x, context, _readAnnotationBodyItemsT);
}

export function writeAnnotationTargetT(x: any, context: any = x): AnnotationTargetT {
return restore_annotation_target(x, context, _writeAnnotationTargetT);
}
Expand Down

0 comments on commit e1fc649

Please sign in to comment.