Skip to content

Commit 5aae159

Browse files
committed
hotfix parsing of old json-ld samples and sources
1 parent 17d6e64 commit 5aae159

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

src/Json/Process/Sample.fs

+6-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ module Sample =
5454
let decoder : Decoder<Sample> =
5555

5656
Decode.object (fun get ->
57+
match get.Optional.Field "additionalType" Decode.uri with
58+
| Some "Sample" | None -> ()
59+
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
60+
match get.Optional.Field "@type" (Decode.list Decode.string) with
61+
| Some ["Sample"] | None -> ()
62+
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
5763
let additionalProperties = get.Optional.Field "additionalProperties" (Decode.list additionalPropertyDecoder)
5864
let characteristics,factors =
5965
match additionalProperties with
@@ -65,9 +71,6 @@ module Sample =
6571
additionalProperties
6672
|> List.choose snd
6773
|> Helper.Option.fromValueWithDefault []
68-
match get.Optional.Field "additionalType" Decode.uri with
69-
| Some "Sample" | None -> ()
70-
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
7174
{
7275
ID = get.Optional.Field "@id" Decode.uri
7376
Name = get.Optional.Field "name" Decode.string

src/Json/Process/Source.fs

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ module Source =
3333
match get.Optional.Field "additionalType" Decode.uri with
3434
| Some "Source" | None -> ()
3535
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
36+
match get.Optional.Field "@type" (Decode.list Decode.string) with
37+
| Some ["Source"] | None -> ()
38+
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
39+
3640
{
3741
ID = get.Optional.Field "@id" Decode.uri
3842
Name = get.Optional.Field "name" Decode.string

tests/Json/Process/ProcessInput.Tests.fs

+89
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,70 @@ open ARCtrl.Json
66
open TestingUtils
77
open TestObjects.Json
88

9+
let deprecated_ROCrate_SourceObject = """{
10+
"@id": "#Source_HOR_3369",
11+
"@type": [
12+
"Source"
13+
],
14+
"name": "HOR 3369",
15+
"characteristics": [
16+
{
17+
"@id": "#MaterialAttributeValue/Organism=Hordeum vulgare",
18+
"@type": "PropertyValue",
19+
"additionalType": "MaterialAttributeValue",
20+
"category": "Organism",
21+
"categoryCode": "MIAPPE:0041",
22+
"value": "Hordeum vulgare",
23+
"valueCode": "http://purl.obolibrary.org/obo/NCBITaxon_4513",
24+
"@context": {
25+
"sdo": "http://schema.org/",
26+
"additionalType": "sdo:additionalType",
27+
"alternateName": "sdo:alternateName",
28+
"measurementMethod": "sdo:measurementMethod",
29+
"description": "sdo:description",
30+
"category": "sdo:name",
31+
"categoryCode": "sdo:propertyID",
32+
"value": "sdo:value",
33+
"valueCode": "sdo:valueReference",
34+
"unit": "sdo:unitText",
35+
"unitCode": "sdo:unitCode",
36+
"comments": "sdo:disambiguatingDescription"
37+
}
38+
}]}"""
39+
40+
let deprecated_ROCrate_SampleObject = """{
41+
"@id": "#Sample_HOR_3369",
42+
"@type": [
43+
"Sample"
44+
],
45+
"name": "HOR 3369",
46+
"additionalProperties": [
47+
{
48+
"@id": "#MaterialAttributeValue/Organism=Hordeum vulgare",
49+
"@type": "PropertyValue",
50+
"additionalType": "MaterialAttributeValue",
51+
"category": "Organism",
52+
"categoryCode": "MIAPPE:0041",
53+
"value": "Hordeum vulgare",
54+
"valueCode": "http://purl.obolibrary.org/obo/NCBITaxon_4513",
55+
"@context": {
56+
"sdo": "http://schema.org/",
57+
"additionalType": "sdo:additionalType",
58+
"alternateName": "sdo:alternateName",
59+
"measurementMethod": "sdo:measurementMethod",
60+
"description": "sdo:description",
61+
"category": "sdo:name",
62+
"categoryCode": "sdo:propertyID",
63+
"value": "sdo:value",
64+
"valueCode": "sdo:valueReference",
65+
"unit": "sdo:unitText",
66+
"unitCode": "sdo:unitCode",
67+
"comments": "sdo:disambiguatingDescription"
68+
}
69+
}]}"""
70+
71+
72+
973
let private tests_source =
1074
testList "Source" [
1175
testCase "ReaderSuccess" (fun () ->
@@ -37,6 +101,19 @@ let private tests_source =
37101
let inputChara = characteristics.[0]
38102
Expect.equal inputChara chara "Sample characteristic did not match"
39103
)
104+
testCase "LD_ReadDeprecatedWithCharacteristics" (fun () ->
105+
let inputPI = Decode.fromJsonString ProcessInput.ROCrate.decoder deprecated_ROCrate_SourceObject
106+
let inputSourceOpt = ProcessInput.trySource inputPI
107+
let inputSource = Expect.wantSome inputSourceOpt "Input is not a sample"
108+
Expect.equal inputSource.Name.Value "HOR 3369" "Sample name did not match"
109+
let characteristics = Expect.wantSome inputSource.Characteristics "No characteristics found"
110+
Expect.hasLength characteristics 1 "Sample characteristics length did not match"
111+
let inputChara = characteristics.[0]
112+
Expect.equal inputChara.NameText "Organism" "Sample characteristic name did not match"
113+
Expect.equal inputChara.ValueText "Hordeum vulgare" "Sample characteristic value did not match"
114+
)
115+
116+
40117
]
41118

42119
let private tests_material =
@@ -106,6 +183,18 @@ let private tests_sample =
106183
let inputChara = characteristics.[0]
107184
Expect.equal inputChara chara "Sample characteristic did not match"
108185
)
186+
testCase "LD_ReadDeprecatedWithCharacteristics" (fun () ->
187+
let inputPI = Decode.fromJsonString ProcessInput.ROCrate.decoder deprecated_ROCrate_SampleObject
188+
let inputSampleOpt = ProcessInput.trySample inputPI
189+
let inputSample = Expect.wantSome inputSampleOpt "Input is not a sample"
190+
Expect.equal inputSample.Name.Value "HOR 3369" "Sample name did not match"
191+
let characteristics = Expect.wantSome inputSample.Characteristics "No characteristics found"
192+
Expect.hasLength characteristics 1 "Sample characteristics length did not match"
193+
let inputChara = characteristics.[0]
194+
Expect.equal inputChara.NameText "Organism" "Sample characteristic name did not match"
195+
Expect.equal inputChara.ValueText "Hordeum vulgare" "Sample characteristic value did not match"
196+
197+
)
109198
]
110199

111200

0 commit comments

Comments
 (0)