fjoppe / Legivel

F# Yaml 1.2 parser
https://fjoppe.github.io/Legivel
The Unlicense
60 stars 5 forks source link

A less strict mode of MapYaml.AndRequireFullProjection #17

Open pkese opened 5 years ago

pkese commented 5 years ago

In MapYaml.AndRequireFullProjection with Schema:

type Schema = {
    dict: Map<string,string>
    number: int
    items: int list
}

and an empty document:

# empty document

There is an expected deserialization error due to full projection not being satisfied. They way to remedy that is to redefine the schema as:

type Schema2 = {
    dict: Map<string,string> option
    number: int option
    items: int list option
}

The question here is:

It is unambiguous that a missing int should always be reported. Yet for dictionaries, lists and arrays, there is an interpretation, that a missing item might mean an empty dictionary, list or array (maybe someone had their yaml serializer configured to omit serialization of empty dictionaries to save some space).

My issue is that the only way to parse such document is to define lists and dictionaries as in Schema2. But then each time I'm accessing items in such a dictionary, I have to go through an option first and there is a lot of unnecessary and hard to read code.

Maybe there could be an option to have another less strict version of AndRequireFullProjection that would allow to instantiate empty complex structures that have default initializers (lists, dictionaries, arrays, but not strings or typed structures).

As a user, if I'd be interested whether the array was there or not, I could still define my schema as this and still get full control over deserialization results:

type Schema = {
    dict: Map<string,string> // I don't care, just create an empty map if the key doesn't exist
    number: int // must be present or deserialization will fail
    items: int list option // here I'm, interested if the key was present in yaml document so give me the option
}

This was intended for discussion. I can live with what we have right now, but it gets a bit annoying and verbose.

fjoppe commented 5 years ago

Did you try to parse Schema option type? An empty document suggests that the whole thing is optional.

pkese commented 5 years ago

Yeah, yeah, except it is not just the top level...
It's turtles all the way down. :)

pkese commented 5 years ago

I'll just close this - it's probably too much effort for a somewhat obscure use-case.

fjoppe commented 4 years ago

I'm reopening this one. Because of all the other stuff going on, I've had not taken the time to read your question well enough and to understand it. But I wanted to return to this one when I had some more time.