Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

F# Record Serialization #38

Closed
jhugard opened this issue May 24, 2016 · 3 comments
Closed

F# Record Serialization #38

jhugard opened this issue May 24, 2016 · 3 comments
Assignees

Comments

@jhugard
Copy link
Collaborator

jhugard commented May 24, 2016

@ctaggart has requested the ability to serialize/deserialize F# Record types, rather than .NET Class Types.

@jhugard
Copy link
Collaborator Author

jhugard commented May 24, 2016

Protobuf serializable Record definition

module SampleNamespace =
    open System
    open Froto.Serialization
    open Froto.Serialization.Serializer
    open Froto.Serialization.Encoding

    type ETest =
        | Nada = 0
        | One = 1
        | Two = 2

    let ETestDefault = ETest.One  // NOTE: Non-zero default is only supported in Proto2

    type InnerSample = {
        id : int32
        name : string
        option : bool
        test : ETest
        packedFixed32 : uint32 list
        repeatedInt32 : int32 list
        _unknownFields : RawField list
        _foundFields : Set<FieldNum>
        }
        with
            static member Default = {
                id=0
                name=""
                option=false
                test=ETest.Nada
                packedFixed32=[]
                repeatedInt32=[]
                _unknownFields=List.empty
                _foundFields=Set.empty
            }

            static member Serializer (m, zcb) =
                (m.id             |> Encode.fromVarint 1) >>
                (m.name           |> Encode.fromString 2) >>
                (m.option         |> Encode.fromBool 3) >>
                (m.test           |> Encode.fromDefaultedVarint ETestDefault 4) >>
                (m.packedFixed32  |> Encode.fromPackedFixed32 5) >>
                (m.repeatedInt32  |> Encode.fromRepeated Encode.fromVarint 6) >>
                (m._unknownFields |> Encode.fromRawFields )
                <| zcb

            static member DecoderRing =
                [
                    0, fun m rawField -> { m with _unknownFields = rawField :: m._unknownFields }
                    1, fun m rawField -> { m with id = rawField |> Decode.toInt32 } : InnerSample
                    2, fun m rawField -> { m with name = rawField |> Decode.toString } : InnerSample
                    3, fun m rawField -> { m with option = rawField |> Decode.toBool } : InnerSample
                    4, fun m rawField -> { m with test = rawField |> Decode.toEnum } : InnerSample
                    5, fun m rawField -> { m with packedFixed32 = rawField |> Decode.toPackedFixed32 } : InnerSample
                    6, fun m rawField -> { m with repeatedInt32 = (rawField |> Decode.toInt32) :: m.repeatedInt32 } : InnerSample
                ]
                |> Map.ofList

            static member RememberFound (m,found) =
                { m with _foundFields = m._foundFields |> Set.add found }

            static member DecodeFixup m =
                { m with
                    packedFixed32 = List.rev m.packedFixed32
                    repeatedInt32 = List.rev m.repeatedInt32
                    _unknownFields = List.rev m._unknownFields }

            static member RequiredFields =
                [ 1; 2 ] |> Set.ofList

            static member FoundFields m =
                m._foundFields

            static member UnknownFields m =
                m._unknownFields

@jhugard jhugard self-assigned this May 25, 2016
@ctaggart
Copy link
Owner

This makes me really happy to see this!

jhugard added a commit that referenced this issue Jun 15, 2016
  * [#37](#38) Fix bug: O(n^2) Performance Problem on repeated fields
  * [#38](#38) Record Serialization
  * Reimplement Class serialization using the new Record Serialization structure
  * Project reorganization for accessibility, readability and consistency
    * Rename base namespace from Froto.Core to Froto.Serialization and assembly from Froto.Core.dll to Froto.Serialization.dll
    * At the WireFormat level, rename encodeXXX/decodeXXX functions to Pack.toXXX/Unpack.fromXXX
    * At the RawField level, and rename dehydrateXXX/hydrateXXX functions to Encode.fromXXX and Decode.toXXX
    * At the Serialization level, created functions for serializing to/from ZeroCopyBuffer and ArraySegment
    * Rename “Exe” solution to “Froto.Compiler”
    * Rename several folders; e.g., “ProtoParser” to “Parser” and “Exe” to “Compiler”
@jhugard
Copy link
Collaborator Author

jhugard commented Jun 15, 2016

Fixed in #39

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants