Skip to content

How to contribute genericSeq implementation? #45

@njlr

Description

@njlr

Hello!

I think I have figured out how to implement an Auto decoder for seq<'t>:

    open System
    open System.Collections
    open System.Collections.Generic

    type private UnboxedSeq<'t> (inner : obj seq) =
        interface seq<'t> with
          member this.GetEnumerator() : IEnumerator<'t> =
            (seq {
              for x in inner do
                yield x :?> 't
            }).GetEnumerator()

          member this.GetEnumerator() =
            (this :> seq<'t>).GetEnumerator() :> IEnumerator

    let private genericSeq (elementT : Type) (decoder: BoxedDecoder) =
        let unboxedSeqT = typedefof<UnboxedSeq<_>>
        let seqT = unboxedSeqT.MakeGenericType(elementT)

        fun (path : string) (value: JsonValue) ->
            if not (Helpers.isArray value) then
                (path, BadPrimitive ("a seq", value)) |> Error
            else
                let values = value.Value<JArray>()
                (values, Ok []) ||> Seq.foldBack (fun value acc ->
                    match acc with
                    | Error _ -> acc
                    | Ok acc ->
                        match decoder.Decode(path, value) with
                        | Error er -> Error er
                        | Ok result -> result :: acc |> Ok)
                |> Result.map (fun xs -> Activator.CreateInstance(seqT, xs))

However, I'm not sure how to contribute it.

Should I add tests to this repo first?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions