WoofWare.Myriad.Plugins 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package WoofWare.Myriad.Plugins --version 1.0.6
NuGet\Install-Package WoofWare.Myriad.Plugins -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WoofWare.Myriad.Plugins" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WoofWare.Myriad.Plugins --version 1.0.6
#r "nuget: WoofWare.Myriad.Plugins, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WoofWare.Myriad.Plugins as a Cake Addin
#addin nuget:?package=WoofWare.Myriad.Plugins&version=1.0.6

// Install WoofWare.Myriad.Plugins as a Cake Tool
#tool nuget:?package=WoofWare.Myriad.Plugins&version=1.0.6

WoofWare.Myriad.Plugins

Project logo: the face of a cartoon Shiba Inu, staring with powerful cyborg eyes directly at the viewer, with a background of stylised plugs.

Some helpers in Myriad which might be useful.

These are currently somewhat experimental, and I personally am their primary customer. The RemoveOptions generator in particular is extremely half-baked.

Currently implemented:

  • JsonParse (to stamp out jsonParse : JsonNode -> 'T methods);
  • RemoveOptions (to strip option modifiers from a type).

JsonParse

Takes records like this:

[<MyriadPlugin.JsonParse>]
type InnerType =
    {
        [<JsonPropertyName "something">]
        Thing : string
    }

/// My whatnot
[<MyriadPlugin.JsonParse>]
type JsonRecordType =
    {
        /// A thing!
        A : int
        /// Another thing!
        B : string
        [<System.Text.Json.Serialization.JsonPropertyName "hi">]
        C : int list
        D : InnerType
    }

and stamps out parsing methods like this:


/// Module containing JSON parsing methods for the InnerType type
[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module InnerType =
    /// Parse from a JSON node.
    let jsonParse (node: System.Text.Json.Nodes.JsonNode) : InnerType =
        let Thing = node.["something"].AsValue().GetValue<string>()
        { Thing = Thing }
namespace UsePlugin

/// Module containing JSON parsing methods for the JsonRecordType type
[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module JsonRecordType =
    /// Parse from a JSON node.
    let jsonParse (node: System.Text.Json.Nodes.JsonNode) : JsonRecordType =
        let D = InnerType.jsonParse node.["d"]

        let C =
            node.["hi"].AsArray() |> Seq.map (fun elt -> elt.GetValue<int>()) |> List.ofSeq

        let B = node.["b"].AsValue().GetValue<string>()
        let A = node.["a"].AsValue().GetValue<int>()
        { A = A; B = B; C = C; D = D }

What's the point?

System.Text.Json, in a PublishAot context, relies on C# source generators. The default reflection-heavy implementations have the necessary code trimmed away, and result in a runtime exception. But C# source generators are entirely unsupported in F#.

This Myriad generator expects you to use System.Text.Json to construct a JsonNode, and then the generator takes over to construct a strongly-typed object.

Limitations

This source generator is enough for what I first wanted to use it for. However, there is far more that could be done.

  • Make it possible to give an exact format and cultural info in date and time parsing.
  • Make it possible to reject parsing if extra fields are present.
  • Rather than just throwing NullReferenceException, print out the field name that failed.
  • Generally support all the System.Text.Json attributes.

RemoveOptions

Takes a record like this:

type Foo =
    {
        A : int option
        B : string
        C : float list
    }

and stamps out a record like this:

[<RequireQualifiedAccess>]
module Foo =
    type Short =
        {
            A : int
            B : string
            C : float list
        }

What's the point?

The motivating example is argument parsing. An argument parser naturally wants to express "the user did not supply this, so I will provide a default". But it's not a very ergonomic experience for the programmer to deal with all these options, so this Myriad generator stamps out a type without any options, and also stamps out an appropriate constructor function.

Limitations

This generator is far from where I want it, because I haven't really spent any time on it.

  • It really wants to be able to recurse into the types within the record, to strip options from them.
  • It needs some sort of attribute to mark a field as not receiving this treatment.
  • What do we do about discriminated unions?

Detailed examples

See the tests. For example, PureGymDto.fs is a real-world set of DTOs.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.47 60 6/24/2024
2.1.46 62 6/24/2024
2.1.45 444 6/17/2024
2.1.44 377 6/15/2024
2.1.43 71 6/10/2024
2.1.42 223 6/10/2024
2.1.41 65 6/9/2024
2.1.40 433 6/4/2024
2.1.39 68 6/3/2024
2.1.38 77 6/1/2024
2.1.37 70 5/31/2024
2.1.36 67 5/31/2024
2.1.35 70 5/31/2024
2.1.34 78 5/30/2024
2.1.33 77 5/30/2024
2.1.32 73 5/30/2024
2.1.31 82 5/30/2024
2.1.30 74 5/30/2024
2.1.29 76 5/28/2024
2.1.28 68 5/27/2024
2.1.27 72 5/24/2024
2.1.26 71 5/24/2024
2.1.25 71 5/24/2024
2.1.24 85 5/20/2024
2.1.23 72 5/20/2024
2.1.22 104 5/6/2024
2.1.21 79 4/30/2024
2.1.20 87 4/29/2024
2.1.19 77 4/29/2024
2.1.18 79 4/22/2024
2.1.17 81 4/17/2024
2.1.16 78 4/16/2024
2.1.15 85 4/16/2024
2.1.14 83 4/15/2024
2.1.13 99 3/19/2024
2.1.12 82 3/11/2024
2.1.11 94 3/4/2024
2.1.10 105 2/26/2024
2.1.9 94 2/26/2024
2.1.8 88 2/25/2024
2.1.7 96 2/25/2024
2.1.6 86 2/25/2024
2.1.5 81 2/19/2024
2.1.4 81 2/19/2024
2.1.3 74 2/18/2024
2.1.2 73 2/18/2024
2.1.1 75 2/17/2024
2.0.9 79 2/14/2024
2.0.8 91 2/13/2024
2.0.7 79 2/13/2024
2.0.6 77 2/13/2024
2.0.5 110 2/12/2024
2.0.4 65 2/7/2024
2.0.3 68 2/7/2024
2.0.2 74 2/7/2024
2.0.1 85 2/7/2024
1.4.15 87 2/6/2024
1.4.14 82 2/6/2024
1.4.13 80 2/6/2024
1.4.12 85 2/6/2024
1.4.11 86 2/6/2024
1.4.10 68 2/5/2024
1.4.9 81 1/30/2024
1.4.8 90 1/29/2024
1.4.7 74 1/29/2024
1.4.6 73 1/29/2024
1.4.5 76 1/28/2024
1.4.4 76 1/28/2024
1.4.3 77 1/26/2024
1.4.2 76 1/26/2024
1.4.1 74 1/26/2024
1.3.5 78 1/25/2024
1.3.4 94 1/15/2024
1.3.3 84 1/15/2024
1.3.2 98 1/8/2024
1.3.1 95 1/8/2024
1.2.3 93 1/3/2024
1.2.2 99 12/31/2023
1.2.1 102 12/30/2023
1.1.15 110 12/30/2023
1.1.14 111 12/30/2023
1.1.13 107 12/30/2023
1.1.12 105 12/30/2023
1.1.11 89 12/30/2023
1.1.10 111 12/29/2023
1.1.9 101 12/29/2023
1.1.8 105 12/29/2023
1.1.7 113 12/29/2023
1.1.6 114 12/29/2023
1.1.5 106 12/29/2023
1.1.4 102 12/29/2023
1.1.3 106 12/29/2023
1.1.2 93 12/28/2023
1.1.1 96 12/28/2023
1.0.6 99 12/28/2023
1.0.5 94 12/28/2023
1.0.4 98 12/27/2023