-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSource.fs
68 lines (56 loc) · 2.69 KB
/
Source.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace ARCtrl.Json
open Thoth.Json.Core
open ARCtrl
open ARCtrl.Process
module Source =
module ROCrate =
let genID (s:Source) : string =
match s.ID with
| Some id -> URI.toString id
| None -> match s.Name with
| Some n -> "#Source_" + n.Replace(" ","_")
| None -> "#EmptySource"
let rec encoder (oa : Source) =
[
"@id", Encode.string (oa |> genID) |> Some
"@type", (Encode.list [ Encode.string "Source"]) |> Some
"additionalType", Encode.string "Source" |> Some
Encode.tryInclude "name" Encode.string (oa.Name)
Encode.tryIncludeListOpt "characteristics" MaterialAttributeValue.ROCrate.encoder (oa.Characteristics)
"@context", ROCrateContext.Source.context_jsonvalue |> Some
]
|> Encode.choose
|> Encode.object
let rec decoder : Decoder<Source> =
Decode.object (fun get ->
match get.Optional.Field "additionalType" Decode.uri with
| Some "Source" | None -> ()
| Some _ -> get.Required.Field "FailBecauseNotSample" Decode.unit
{
ID = get.Optional.Field "@id" Decode.uri
Name = get.Optional.Field "name" Decode.string
Characteristics = get.Optional.Field "characteristics" (Decode.list MaterialAttributeValue.ROCrate.decoder)
}
)
module ISAJson =
let rec encoder (idMap : IDTable.IDTableWrite option) (oa : Source) =
let f (oa : Source) =
[
Encode.tryInclude "@id" Encode.string (oa |> ROCrate.genID |> Some)
Encode.tryInclude "name" Encode.string oa.Name
Encode.tryIncludeListOpt "characteristics" (MaterialAttributeValue.ISAJson.encoder idMap) oa.Characteristics
]
|> Encode.choose
|> Encode.object
match idMap with
| None -> f oa
| Some idMap -> IDTable.encode ROCrate.genID f oa idMap
let allowedFields = ["@id";"name";"characteristics";"@type"; "@context"]
let decoder: Decoder<Source> =
Decode.objectNoAdditionalProperties allowedFields (fun get ->
{
ID = get.Optional.Field "@id" Decode.uri
Name = get.Optional.Field "name" Decode.string
Characteristics = get.Optional.Field "characteristics" (Decode.list MaterialAttributeValue.ISAJson.decoder)
}
)