fjoppe / Legivel

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

Ability to provide naming conventions #36

Open kodfodrasz opened 3 years ago

kodfodrasz commented 3 years ago

Description

I'd need a way to provide naming conventions between yaml document field names and .net type field/property names, where I don't need to annotate the complete type hierarchy.

Repro steps

Please provide the steps required to reproduce the problem

  1. Have a yaml document with some naming convention, eg. snake case:
    ---
    foo:
      bar_baz: 1
      lorem_ipsum_dolor: et si amet
  2. Have a corresponding type hierarchy in the .Net PascalCase naming conventions, and no YamlFieldAttribute setup:
    type Foo = {
    BarBaz : int
    LoremIpsumDolor : string
    }
    type Document = {
      Foo : Foo
    }
  3. The magic happens here, set Legivel up so it uses the SnakeCase mapper (like in JSON.Net and other serializers, PascalCase is assumed on .Net side, when not literate field name mapping is used)
  4. Deserialize the document successfully with Deserialize<Document> theDocString

Expected behavior

Have a way to specify a mapper. If there is a way, have it documented and have a simple example.

Actual behavior

I could no find a way apart from annotating everything

Known workarounds

I can annotate every field, but over a certain size this is cumbersome and error prone.

Related information

fjoppe commented 3 years ago

Suppose the following:

let yml = "
    key_one_val_1 : foo
    key_one_val_2 : baz
" 

Deserialize<Dictionary<string,string>> yml 

Wich results in:

[Success { Data = dict [("key_one_val_1", "foo"); ("key_one_val_2", "baz")] Warn = [] }]

I guess these need to be untouched?

Also, what kind of function did you have in mind to inject:

Plus, besides this feature, one can still apply an annotation, this should overrule (omit) the injected function.

Thanks for this suggestion anyway. I'll have a look into this.