Giter Club home page Giter Club logo

Comments (7)

vchekan avatar vchekan commented on May 30, 2024 1

"jfieldRepeated" repeated is protobuf terminology and will be confusing in fleece context.

from fleece.

gusty avatar gusty commented on May 30, 2024 1

I was analyzing this and came up to with an easy way to have this functionality, with the existing combinators.

Let's pretend we have a pair of functions to go from/to an option of list / plain list:

let flatten = function None -> [] | Some x -> x
let expand = function [] -> None | x  -> Some x

Now, we can use them this way:

type Repeat = { Name : string ; Ids : int list } with
    static member JsonObjCodec =
        fun n i -> { Name = n; Ids = flatten i }
        <!> jreq  "name" (fun x -> Some x.Name)
        <*> jopt  "ids"  (fun x -> expand x.Ids)

And we'll get the desired effect.

This has the additional advantage that we can apply this to any collection, not just lists and arrays.

from fleece.

7sharp9 avatar 7sharp9 commented on May 30, 2024

My vote would be for jfieldMany or jfieldRepeated, is there anyway to work around this with the current combinators?

from fleece.

7sharp9 avatar 7sharp9 commented on May 30, 2024

In Falanx this would be slightly different as it would be a ResizeArray instead

from fleece.

gusty avatar gusty commented on May 30, 2024

Yes, maybe something like this;

let flatten =  function None -> ResizeArray () | Some x -> x
let expand (x: ResizeArray<_>) = if Seq.isEmpty x then None else Some x

from fleece.

7sharp9 avatar 7sharp9 commented on May 30, 2024

Probably this as we would want 0 length intact.

let flattenResizeArray = function None -> ResizeArray() | Some x -> x
let expandResizeArray = function null -> None | x  -> Some x

from fleece.

gusty avatar gusty commented on May 30, 2024

Closing this as a mechanism was suggested in the comments and additionally:

Or maybe we can upgrade the existing jfieldOpt to handle multiple values.

jopt now as of 0.10.0 supports any type that has defined the zero value, including lists and arrays, which would do exactly what the title of the issue describes.

from fleece.

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.