From 436040e035dc2749e3f50c74ab23954365a94e0c Mon Sep 17 00:00:00 2001 From: 64J0 Date: Tue, 4 Jul 2023 18:50:33 -0300 Subject: [PATCH 1/2] feat: add first sample translation from c# to f#: DeserializeObjectGraph --- .../DeserializeObjectGraph.fs | 119 ++++++++++++++++++ .../YamlDotNet.Samples.Fsharp.fsproj | 20 +++ 2 files changed, 139 insertions(+) create mode 100644 YamlDotNet.Samples.Fsharp/DeserializeObjectGraph.fs create mode 100644 YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj diff --git a/YamlDotNet.Samples.Fsharp/DeserializeObjectGraph.fs b/YamlDotNet.Samples.Fsharp/DeserializeObjectGraph.fs new file mode 100644 index 000000000..6a2e0bb57 --- /dev/null +++ b/YamlDotNet.Samples.Fsharp/DeserializeObjectGraph.fs @@ -0,0 +1,119 @@ +module YamlDotNet.Samples.Fsharp.DeserializeObjectGraph + +open System +open System.Collections.Generic +open System.IO +open YamlDotNet.Serialization +open YamlDotNet.Serialization.NamingConventions + +[] +type Customer = { Given: string; Family: string } + +[] +type OrderItem = + { [] + PartNo: string + Descrip: string + Price: decimal + Quantity: int } + +[] +type Address = + { Street: string + City: string + State: string } + +[] +type Order = + { Receipt: string + Date: DateTime + Customer: Customer + Items: List + + [] + BillTo: Address + + [] + ShipTo: Address + SpecialDelivery: string } + +let Document = + @"--- + receipt: Oz-Ware Purchase Invoice + date: 2007-08-06 + customer: + given: Dorothy + family: Gale + + items: + - part_no: A4786 + descrip: Water Bucket (Filled) + price: 1.47 + quantity: 4 + + - part_no: E1628 + descrip: High Heeled ""Ruby"" Slippers + price: 100.27 + quantity: 1 + + bill-to: &id001 + street: |- + 123 Tornado Alley + Suite 16 + city: East Westville + state: KS + + ship-to: *id001 + + specialDelivery: > + Follow the Yellow Brick + Road to the Emerald City. + Pay no attention to the + man behind the curtain. +..." + +let main () = + let input = new StringReader(Document) + + let deserializer = + DeserializerBuilder() + .WithNamingConvention(CamelCaseNamingConvention.Instance) + .Build() + + let order = deserializer.Deserialize(input) + + printfn "Order" + printfn "-----" + printfn "" + + order.Items.ForEach(fun item -> printfn $"{item.PartNo}\t{item.Quantity}\t{item.Price}\t{item.Descrip}") + + printfn "" + + printfn "Shipping" + printfn "--------" + printfn "" + printfn "%A" order.ShipTo.Street + printfn "%A" order.ShipTo.City + printfn "%A" order.ShipTo.State + printfn "" + + printfn "Billing" + printfn "-------" + printfn "" + + if (order.BillTo = order.ShipTo) then + printfn "*same as shipping address*" + else + printfn "%A" order.ShipTo.Street + printfn "%A" order.ShipTo.City + printfn "%A" order.ShipTo.State + + printfn "" + + printfn "Delivery instructions" + printfn "---------------------" + printfn "" + printfn "%A" order.SpecialDelivery + +main () diff --git a/YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj b/YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj new file mode 100644 index 000000000..659afbbf6 --- /dev/null +++ b/YamlDotNet.Samples.Fsharp/YamlDotNet.Samples.Fsharp.fsproj @@ -0,0 +1,20 @@ + + + + Exe + net7.0 + false + + + + + {BF32DE1B-6276-4341-B212-F8862ADBBA7A} + YamlDotNet + + + + + + + + From 94b8aeb716e990d97e19bb14e6df6dcb5d5d14d2 Mon Sep 17 00:00:00 2001 From: 64J0 Date: Tue, 29 Aug 2023 19:00:04 -0300 Subject: [PATCH 2/2] chore: add YamlDotNet.Samples.Fsharp to the solution file --- YamlDotNet.sln | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/YamlDotNet.sln b/YamlDotNet.sln index f93c6b2f1..2951c8713 100644 --- a/YamlDotNet.sln +++ b/YamlDotNet.sln @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YamlDotNet.Analyzers.Static EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YamlDotNet.Core7AoTCompileTest", "YamlDotNet.Core7AoTCompileTest\YamlDotNet.Core7AoTCompileTest.csproj", "{DEB5099E-D216-438B-86A7-03674F9185EF}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "YamlDotNet.Samples.Fsharp", "YamlDotNet.Samples.Fsharp\YamlDotNet.Samples.Fsharp.fsproj", "{C047392D-6B20-47CD-9FE6-D0FA326FD262}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,6 +65,10 @@ Global {DEB5099E-D216-438B-86A7-03674F9185EF}.Debug|Any CPU.Build.0 = Debug|Any CPU {DEB5099E-D216-438B-86A7-03674F9185EF}.Release|Any CPU.ActiveCfg = Release|Any CPU {DEB5099E-D216-438B-86A7-03674F9185EF}.Release|Any CPU.Build.0 = Release|Any CPU + {C047392D-6B20-47CD-9FE6-D0FA326FD262}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C047392D-6B20-47CD-9FE6-D0FA326FD262}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C047392D-6B20-47CD-9FE6-D0FA326FD262}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C047392D-6B20-47CD-9FE6-D0FA326FD262}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE