Giter VIP home page Giter VIP logo

Comments (3)

jhugard avatar jhugard commented on July 28, 2024

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

from froto.

ctaggart avatar ctaggart commented on July 28, 2024

This makes me really happy to see this!

from froto.

jhugard avatar jhugard commented on July 28, 2024

Fixed in #39

from froto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.