Skip to content

Commit

Permalink
language can also be an array
Browse files Browse the repository at this point in the history
  • Loading branch information
jptmoore committed Sep 3, 2024
1 parent 05a2b72 commit 66b45b2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 14 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.2",
"version": "1.1.3",
"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 @@ -45,6 +45,9 @@ sed -i 's/export function readRangeItemsT(/export function _readRangeItemsT(/g'
sed -i 's/export function writeGeometryT(/export function _writeGeometryT(/g' specification.ts
sed -i 's/export function readGeometryT(/export function _readGeometryT(/g' specification.ts

sed -i 's/export function writeLanguageT(/export function _writeLanguageT(/g' specification.ts
sed -i 's/export function readLanguageT(/export function _readLanguageT(/g' specification.ts

# rename _type to use type
sed -i 's/_type/type/g' specification.ts
# add adapter code
Expand Down
3 changes: 3 additions & 0 deletions scripts/compilespec-mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ sed -i '' 's/export function readRangeItemsT(/export function _readRangeItemsT(/
sed -i '' 's/export function writeGeometryT(/export function _writeGeometryT(/g' specification.ts
sed -i '' 's/export function readGeometryT(/export function _readGeometryT(/g' specification.ts

sed -i '' 's/export function writeLanguageT(/export function _writeLanguageT(/g' specification.ts
sed -i '' 's/export function readLanguageT(/export function _readLanguageT(/g' specification.ts

# rename _type to use type
sed -i '' 's/_type/type/g' specification.ts
# add adapter code
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_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_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 @@ -113,4 +113,12 @@ export function writeGeometryT(x: any, context: any = x): GeometryT {

export function readGeometryT(x: any, context: any = x): GeometryT {
return normalize_geometry(x, context, _readGeometryT);
}

export function writeLanguageT(x: any, context: any = x): LanguageT {
return restore_language(x, context, _writeLanguageT);
}

export function readLanguageT(x: any, context: any = x): LanguageT {
return normalize_language(x, context, _readLanguageT);
}
16 changes: 16 additions & 0 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,20 @@ export function normalize_geometry<T extends { type: string }, R>(x: T, context:
} else {
throw new Error(`${JSON.stringify(x)}: Input type did not match expected types.`);
}
}

export function restore_language<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_language<T, R>(x: T, context: any = x, fn: (input: [string, T], context: any) => R): R {
if (Array.isArray(x)) {
return fn(['T2', x], context);
} else {
return fn(['T1', x], context);
}
}
9 changes: 8 additions & 1 deletion src/specification.atd
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ type summary_t = lng_string_t

type lng_string_t = (string * string list) list <json repr="object">

type language_t = [
| T1 of language_t1
| T2 of language_t2
]

type language_t1 = string

type language_t2 = string list

type external_t = {
inherit class_t;
Expand Down Expand Up @@ -474,7 +482,6 @@ type created_t = string
type modified_t = string
type format_t = string
type profile_t = string
type language_t = string
type viewing_direction_t = string
type behavior_t = string
type nav_date_t = string
Expand Down
66 changes: 55 additions & 11 deletions src/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ export type SummaryT = LngStringT

export type LngStringT = [string, string[]][]

export type LanguageT =
| { kind: 'T1'; value: LanguageT1 }
| { kind: 'T2'; value: LanguageT2 }

export type LanguageT1 = string

export type LanguageT2 = string[]

export type ExternalT = {
id: IdT;
type: TypeT;
Expand Down Expand Up @@ -583,8 +591,6 @@ export type FormatT = string

export type ProfileT = string

export type LanguageT = string

export type ViewingDirectionT = string

export type BehaviorT = string
Expand Down Expand Up @@ -1947,6 +1953,44 @@ export function readLngStringT(x: any, context: any = x): LngStringT {
return _atd_read_assoc_object_into_array(_atd_read_array(_atd_read_string))(x, context);
}

export function _writeLanguageT(x: LanguageT, context: any = x): any {
switch (x.kind) {
case 'T1':
return ['T1', writeLanguageT1(x.value, x)]
case 'T2':
return ['T2', writeLanguageT2(x.value, x)]
}
}

export function _readLanguageT(x: any, context: any = x): LanguageT {
_atd_check_json_tuple(2, x, context)
switch (x[0]) {
case 'T1':
return { kind: 'T1', value: readLanguageT1(x[1], x) }
case 'T2':
return { kind: 'T2', value: readLanguageT2(x[1], x) }
default:
_atd_bad_json('LanguageT', x, context)
throw new Error('impossible')
}
}

export function writeLanguageT1(x: LanguageT1, context: any = x): any {
return _atd_write_string(x, context);
}

export function readLanguageT1(x: any, context: any = x): LanguageT1 {
return _atd_read_string(x, context);
}

export function writeLanguageT2(x: LanguageT2, context: any = x): any {
return _atd_write_array(_atd_write_string)(x, context);
}

export function readLanguageT2(x: any, context: any = x): LanguageT2 {
return _atd_read_array(_atd_read_string)(x, context);
}

export function writeExternalT(x: ExternalT, context: any = x): any {
return {
'id': _atd_write_required_field('ExternalT', 'id', writeIdT, x.id, x),
Expand Down Expand Up @@ -2295,14 +2339,6 @@ export function readProfileT(x: any, context: any = x): ProfileT {
return _atd_read_string(x, context);
}

export function writeLanguageT(x: LanguageT, context: any = x): any {
return _atd_write_string(x, context);
}

export function readLanguageT(x: any, context: any = x): LanguageT {
return _atd_read_string(x, context);
}

export function writeViewingDirectionT(x: ViewingDirectionT, context: any = x): any {
return _atd_write_string(x, context);
}
Expand Down Expand Up @@ -2809,7 +2845,7 @@ function _atd_write_field_with_default<T>(

///// appended to specification.ts

import { 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_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 @@ -2922,4 +2958,12 @@ export function writeGeometryT(x: any, context: any = x): GeometryT {

export function readGeometryT(x: any, context: any = x): GeometryT {
return normalize_geometry(x, context, _readGeometryT);
}

export function writeLanguageT(x: any, context: any = x): LanguageT {
return restore_language(x, context, _writeLanguageT);
}

export function readLanguageT(x: any, context: any = x): LanguageT {
return normalize_language(x, context, _readLanguageT);
}

0 comments on commit 66b45b2

Please sign in to comment.