question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Combinator to interpret absence of a field as an empty array

See original GitHub issue

The same way we have jfieldOpt which interpret the absence of a field as a None, we could add a combinator that interpret the absence as an empty array.

How can we call that combinator?

  • jfieldMany
  • jfieldRepeated
  • jfieldArray

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gustycommented, Dec 7, 2018

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.

1reaction
vchekancommented, Nov 30, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How iterating through an empty array works?
Creates an empty array, so the lottery numbers can be stored here, after they're checked for duplicates. There is a for loop and...
Read more >
The Verse Programming Language [pdf]
Empty array in this pattern does not represent an error. It represents the fact that no values you asked for can be found....
Read more >
How to check if an array is empty using Javascript?
How to check if an array is empty using Javascript? We continue with Flexiple's tutorial series to explain the code and concept behind...
Read more >
APLcart - Find your way in APL
Interpret internal representation as array of type 'type' ... Validate numeric input: returns Boolean validity mask and numeric vector of converted input.
Read more >
query on array=null not working
I have the following situation: i have a db table called notification; it has a field called "target_users", it's text field but marked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found