Skip to content

Commit

Permalink
start adding extension data handling 🚧
Browse files Browse the repository at this point in the history
  • Loading branch information
caroott committed Jun 19, 2024
1 parent bbe7510 commit 93fb070
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### 0.0.1-alpha (Released 2024-6-19)
* First open alpha release of YAMLicious. Rough feature set:
* Tokenize YAML files
* Encode & Decode YAML tokens
* Write YAML tokens
25 changes: 25 additions & 0 deletions src/YAMLicious/Decode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,16 @@ module ObjectHelper =
//abstract At: List<string> -> Decoder<'a> -> 'a option
//abstract Raw: Decoder<'a> -> 'a option

type IOverflowGetter =
abstract FieldList: string list -> Dictionary<string, YAMLElement>


open ObjectHelper

type IGetters =
abstract Required: IRequiredGetter
abstract Optional: IOptionalGetter
abstract Overflow: IOverflowGetter

type Getter(ele: YAMLElement) =
let required =
Expand Down Expand Up @@ -337,9 +341,30 @@ type Getter(ele: YAMLElement) =
)
| anyElse -> Helper.raiseInvalidArg "value" "Expected an object" anyElse
}
let overflow =
{ new IOverflowGetter with
member this.FieldList (fieldNames: string list) =
match ele with
| YAMLElement.Object v ->
let overflow =
v
|> List.filter (function
| YAMLElement.Mapping (k, _) -> fieldNames |> List.exists (fun x -> x = k.Value)
| _ -> false
)
|> List.map (function
| YAMLElement.Mapping (k, v) -> (k.Value, v)
| _ -> Helper.raiseInvalidArg "value" "Expected a mapping" ele
)
let dict = new Dictionary<string, YAMLElement>()
overflow |> List.iter (fun (k, v) -> dict.Add(k, v))
dict
| anyElse -> Helper.raiseInvalidArg "value" "Expected an object" anyElse
}
interface IGetters with
member __.Required = required
member __.Optional = optional
member __.Overflow = overflow

let object (getter: IGetters -> 'a) (value: YAMLElement) : 'a =
let getterObj = Getter(value) :> IGetters
Expand Down
28 changes: 28 additions & 0 deletions tests/YAMLicious.Tests/Tests.EncoderDecoder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ module ValidationPackageTypes =
Packages: Package list
}

type ArcValidationExtensionData = {
ArcSpecificationVersion: string
Packages: Package list
ExtensionData: System.Collections.Generic.Dictionary<string, YAMLicious.YAMLiciousTypes.YAMLElement>
}

open ValidationPackageTypes

Expand All @@ -26,6 +31,17 @@ validation_packages:
- name: package2
version: 2.0.0
- name: package3"
let stringOverflow = "arc_specification: 2.0.0-draft
validation_packages:
- name: package1
version: 1.0.0
- name: package2
version: 2.0.0
- name: package3
author: TestAuthor
author_emails:
- [email protected]
- [email protected]"

let type_ = {
ArcSpecificationVersion = "2.0.0-draft"
Expand All @@ -35,6 +51,18 @@ validation_packages:
{ Name = "package3"; Version = None }
]
}
let typeOverflow_ = {
ArcSpecificationVersion = "2.0.0-draft"
Packages = [
{ Name = "package1"; Version = Some "1.0.0" }
{ Name = "package2"; Version = Some "2.0.0" }
{ Name = "package3"; Version = None }
]
ExtensionData =
let dict = System.Collections.Generic.Dictionary<string, YAMLicious.YAMLiciousTypes.YAMLElement>()
dict.Add("author", YAMLicious.YAMLiciousTypes.YAMLElement.Value (YAMLicious.YAMLiciousTypes.YAMLContent.create("TestAuthor")))
dict
}

open Fable.Pyxpecto
open YAMLicious
Expand Down

0 comments on commit 93fb070

Please sign in to comment.