Skip to content

Commit 2e47979

Browse files
committed
start working on ro-crate-json parsing
1 parent 9af0c63 commit 2e47979

41 files changed

Lines changed: 340 additions & 168 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ARCtrl/ARCtrl.fsproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -47,4 +47,7 @@
4747
<Package Name="requests" Version="&gt;= 2.28.1 &lt; 3.0.0" ResolutionStrategy="Max" />
4848
</PythonDependencies>
4949
</PropertyGroup>
50+
<ItemGroup>
51+
<PackageReference Update="FSharp.Core" Version="8.0.400" />
52+
</ItemGroup>
5053
</Project>

src/Contract/ARCtrl.Contract.fsproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
@@ -35,4 +35,7 @@
3535
<RepositoryUrl>https://github.com/nfdi4plants/ARCtrl</RepositoryUrl>
3636
<RepositoryType>git</RepositoryType>
3737
</PropertyGroup>
38+
<ItemGroup>
39+
<PackageReference Update="FSharp.Core" Version="8.0.400" />
40+
</ItemGroup>
3841
</Project>

src/Json/ARCtrl.Json.fsproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
</PropertyGroup>
77
<ItemGroup>
8-
<Content Include="*.fsproj; **\*.fs; **\*.fsi" PackagePath="fable\" />
98
<None Include="../../build/logo.png" Pack="true" PackagePath="\" />
109
</ItemGroup>
1110
<ItemGroup>
@@ -78,6 +77,7 @@
7877
<Compile Include="Assay.fs" />
7978
<Compile Include="Study.fs" />
8079
<Compile Include="Investigation.fs" />
80+
<Compile Include="ROCrateObject.fs" />
8181
<Compile Include="ARC.fs" />
8282
</ItemGroup>
8383
<ItemGroup>
@@ -89,6 +89,10 @@
8989
</ItemGroup>
9090
<ItemGroup>
9191
<ProjectReference Include="..\Core\ARCtrl.Core.fsproj" />
92+
<ProjectReference Include="..\ROCrate\ARCtrl.ROCrate.fsproj" />
93+
</ItemGroup>
94+
<ItemGroup>
95+
<PackageReference Update="FSharp.Core" Version="8.0.400" />
9296
</ItemGroup>
9397
<PropertyGroup>
9498
<Authors>nfdi4plants, Lukas Weil, Florian Wetzels, Kevin Frey</Authors>

src/Json/ROCrateObject.fs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
namespace ARCtrl.Json
2+
3+
open ARCtrl
4+
open System
5+
open ARCtrl.ROCrate
6+
open Thoth.Json.Core
7+
open DynamicObj
8+
9+
module rec ROCrateObject =
10+
11+
#if !FABLE_COMPILER
12+
let (|SomeObj|_|) =
13+
// create generalized option type
14+
let ty = typedefof<option<_>>
15+
fun (a:obj) ->
16+
// Check for nulls otherwise 'a.GetType()' would fail
17+
if isNull a
18+
then
19+
None
20+
else
21+
let aty = a.GetType()
22+
// Get option'.Value
23+
let v = aty.GetProperty("Value")
24+
if aty.IsGenericType && aty.GetGenericTypeDefinition() = ty then
25+
// return value if existing
26+
Some(v.GetValue(a, [| |]))
27+
else
28+
None
29+
#endif
30+
31+
32+
let genericEncoder (obj : obj) : IEncodable =
33+
match obj with
34+
| :? string as s -> Encode.string s
35+
| :? int as i -> Encode.int i
36+
| :? bool as b -> Encode.bool b
37+
| :? float as f -> Encode.float f
38+
| :? DateTime as d -> Encode.dateTime d
39+
| :? ROCrateObject as o -> encoder o
40+
#if !FABLE_COMPILER
41+
| SomeObj o -> genericEncoder o
42+
#endif
43+
| null -> Encode.nil
44+
| :? System.Collections.IEnumerable as l -> [ for x in l -> genericEncoder x] |> Encode.list
45+
| _ -> failwith "Unknown type"
46+
47+
let rec encoder(obj: ROCrateObject) =
48+
obj.GetProperties true
49+
|> Seq.map (fun kv ->
50+
kv.Key,
51+
genericEncoder obj
52+
)
53+
|> Encode.object
54+
55+
56+
let rec decoder : Decoder<obj> =
57+
let rec decode() =
58+
let decodeObject : Decoder<ROCrateObject> =
59+
{ new Decoder<ROCrateObject> with
60+
member _.Decode(helpers, value) =
61+
if helpers.isObject value then
62+
let getters = Decode.Getters(helpers, value)
63+
let properties = helpers.getProperties value
64+
let builder =
65+
fun (get : Decode.IGetters) ->
66+
let o = ROCrateObject(
67+
id = get.Required.Field "@id" Decode.string,
68+
schemaType = get.Required.Field "@type" Decode.string
69+
)
70+
for property in properties do
71+
if property <> "@id" && property <> "@type" then
72+
o.SetProperty(property,get.Required.Field property (decode()))
73+
o
74+
let result = builder getters
75+
match getters.Errors with
76+
| [] -> Ok result
77+
| fst :: _ as errors ->
78+
if errors.Length > 1 then
79+
("", BadOneOf errors) |> Error
80+
else
81+
Error fst
82+
else
83+
("", BadPrimitive("an object", value)) |> Error
84+
}
85+
let resizeArray : Decoder<ResizeArray<obj>> =
86+
{ new Decoder<ResizeArray<obj>> with
87+
member _.Decode(helpers, value) =
88+
if helpers.isArray value then
89+
let mutable i = -1
90+
let tokens = helpers.asArray value
91+
let arr = ResizeArray()
92+
93+
(Ok arr, tokens)
94+
||> Array.fold (fun acc value ->
95+
i <- i + 1
96+
97+
match acc with
98+
| Error _ -> acc
99+
| Ok acc ->
100+
match decode().Decode(helpers, value) with
101+
| Error er ->
102+
Error(
103+
er
104+
|> Helpers.prependPath (
105+
".[" + (i.ToString()) + "]"
106+
)
107+
)
108+
| Ok value ->
109+
acc.Add value
110+
Ok acc
111+
)
112+
else
113+
("", BadPrimitive("an array", value)) |> Error
114+
}
115+
Decode.oneOf [
116+
Decode.map box (decodeObject)
117+
Decode.map box (resizeArray)
118+
Decode.map box (Decode.string)
119+
Decode.map box (Decode.int)
120+
Decode.map box (Decode.decimal)
121+
122+
]
123+
decode()

src/ROCrate/ARCtrl.ROCrate.fsproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
<None Include="../../build/logo.png" Pack="true" PackagePath="\" />
2222
</ItemGroup>
2323
<ItemGroup>
24-
<PackageReference Include="DynamicObj" Version="3.1.0" />
24+
<PackageReference Include="DynamicObj" Version="4.0.0" />
25+
<PackageReference Include="Thoth.Json.Core" Version="0.4.0" />
26+
</ItemGroup>
27+
<ItemGroup>
28+
<PackageReference Update="FSharp.Core" Version="8.0.400" />
2529
</ItemGroup>
2630
<PropertyGroup>
2731
<Authors>Kevin Schneider, nfdi4plants, DataPLANT OSS contributors</Authors>

src/ROCrate/ISAProfile/Assay.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ type Assay(
1919
) as this =
2020
inherit Dataset(id, "Assay")
2121
do
22-
DynObj.setValue this (nameof identifier) identifier
22+
DynObj.setProperty (nameof identifier) identifier this
2323

24-
DynObj.setValueOpt this (nameof measurementMethod) measurementMethod
25-
DynObj.setValueOpt this (nameof measurementTechnique) measurementTechnique
26-
DynObj.setValueOpt this (nameof variableMeasured) variableMeasured
27-
DynObj.setValueOpt this (nameof about) about
28-
DynObj.setValueOpt this (nameof comment) comment
29-
DynObj.setValueOpt this (nameof creator) creator
30-
DynObj.setValueOpt this (nameof hasPart) hasPart
31-
DynObj.setValueOpt this (nameof url) url
24+
DynObj.setOptionalProperty (nameof measurementMethod) measurementMethod this
25+
DynObj.setOptionalProperty (nameof measurementTechnique) measurementTechnique this
26+
DynObj.setOptionalProperty (nameof variableMeasured) variableMeasured this
27+
DynObj.setOptionalProperty (nameof about) about this
28+
DynObj.setOptionalProperty (nameof comment) comment this
29+
DynObj.setOptionalProperty (nameof creator) creator this
30+
DynObj.setOptionalProperty (nameof hasPart) hasPart this
31+
DynObj.setOptionalProperty (nameof url) url this

src/ROCrate/ISAProfile/Data.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type Data(
1515
) as this =
1616
inherit ROCrateObject(id = id, schemaType = "schema.org/MediaObject", ?additionalType = additionalType)
1717
do
18-
DynObj.setValue this (nameof name) name
18+
DynObj.setProperty (nameof name) name this
1919

20-
DynObj.setValueOpt this (nameof comment) comment
21-
DynObj.setValueOpt this (nameof encodingFormat) encodingFormat
22-
DynObj.setValueOpt this (nameof disambiguatingDescription) disambiguatingDescription
20+
DynObj.setOptionalProperty (nameof comment) comment this
21+
DynObj.setOptionalProperty (nameof encodingFormat) encodingFormat this
22+
DynObj.setOptionalProperty (nameof disambiguatingDescription) disambiguatingDescription this
2323

src/ROCrate/ISAProfile/Investigation.fs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ type Investigation(
2222
) as this =
2323
inherit Dataset(id, "Investigation")
2424
do
25-
DynObj.setValue this (nameof identifier) identifier
25+
DynObj.setProperty (nameof identifier) identifier this
2626

27-
DynObj.setValueOpt this (nameof citation) citation
28-
DynObj.setValueOpt this (nameof comment) comment
29-
DynObj.setValueOpt this (nameof creator) creator
30-
DynObj.setValueOpt this (nameof dateCreated) dateCreated
31-
DynObj.setValueOpt this (nameof dateModified) dateModified
32-
DynObj.setValueOpt this (nameof datePublished) datePublished
33-
DynObj.setValueOpt this (nameof hasPart) hasPart
34-
DynObj.setValueOpt this (nameof headline) headline
35-
DynObj.setValueOpt this (nameof mentions) mentions
36-
DynObj.setValueOpt this (nameof url) url
37-
DynObj.setValueOpt this (nameof description) description
27+
DynObj.setOptionalProperty (nameof citation) citation this
28+
DynObj.setOptionalProperty (nameof comment) comment this
29+
DynObj.setOptionalProperty (nameof creator) creator this
30+
DynObj.setOptionalProperty (nameof dateCreated) dateCreated this
31+
DynObj.setOptionalProperty (nameof dateModified) dateModified this
32+
DynObj.setOptionalProperty (nameof datePublished) datePublished this
33+
DynObj.setOptionalProperty (nameof hasPart) hasPart this
34+
DynObj.setOptionalProperty (nameof headline) headline this
35+
DynObj.setOptionalProperty (nameof mentions) mentions this
36+
DynObj.setOptionalProperty (nameof url) url this
37+
DynObj.setOptionalProperty (nameof description) description this

src/ROCrate/ISAProfile/LabProcess.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ type LabProcess(
1919
) as this =
2020
inherit ROCrateObject(id = id, schemaType = "bioschemas.org/LabProcess", ?additionalType = additionalType)
2121
do
22-
DynObj.setValue this (nameof name) name
23-
DynObj.setValue this (nameof agent) agent
24-
DynObj.setValue this (nameof object) object
25-
DynObj.setValue this (nameof result) result
22+
DynObj.setProperty (nameof name) name this
23+
DynObj.setProperty (nameof agent) agent this
24+
DynObj.setProperty (nameof object) object this
25+
DynObj.setProperty (nameof result) result this
2626

27-
DynObj.setValueOpt this (nameof executesLabProtocol) executesLabProtocol
28-
DynObj.setValueOpt this (nameof parameterValue) parameterValue
29-
DynObj.setValueOpt this (nameof endTime) endTime
30-
DynObj.setValueOpt this (nameof disambiguatingDescription) disambiguatingDescription
27+
DynObj.setOptionalProperty (nameof executesLabProtocol) executesLabProtocol this
28+
DynObj.setOptionalProperty (nameof parameterValue) parameterValue this
29+
DynObj.setOptionalProperty (nameof endTime) endTime this
30+
DynObj.setOptionalProperty (nameof disambiguatingDescription) disambiguatingDescription this

src/ROCrate/ISAProfile/LabProtocol.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ type LabProtocol(
2020
) as this =
2121
inherit ROCrateObject(id = id, schemaType = "bioschemas.org/LabProtocol", ?additionalType = additionalType)
2222
do
23-
DynObj.setValueOpt this (nameof name) name
24-
DynObj.setValueOpt this (nameof intendedUse) intendedUse
25-
DynObj.setValueOpt this (nameof description) description
26-
DynObj.setValueOpt this (nameof url) url
27-
DynObj.setValueOpt this (nameof comment) comment
28-
DynObj.setValueOpt this (nameof version) version
29-
DynObj.setValueOpt this (nameof labEquipment) labEquipment
30-
DynObj.setValueOpt this (nameof reagent) reagent
31-
DynObj.setValueOpt this (nameof computationalTool) computationalTool
23+
DynObj.setOptionalProperty (nameof name) name this
24+
DynObj.setOptionalProperty (nameof intendedUse) intendedUse this
25+
DynObj.setOptionalProperty (nameof description) description this
26+
DynObj.setOptionalProperty (nameof url) url this
27+
DynObj.setOptionalProperty (nameof comment) comment this
28+
DynObj.setOptionalProperty (nameof version) version this
29+
DynObj.setOptionalProperty (nameof labEquipment) labEquipment this
30+
DynObj.setOptionalProperty (nameof reagent) reagent this
31+
DynObj.setOptionalProperty (nameof computationalTool) computationalTool this

0 commit comments

Comments
 (0)