Fable.Python icon indicating copy to clipboard operation
Fable.Python copied to clipboard

Support for Attributes?

Open thautwarm opened this issue 4 years ago • 5 comments

I'm now happy with Fable's reflection, which allows me to do what pydantic does in the python world.

However I want to step further. I want to allow syntax sugars in the configuration, but get the same syntax tree.

type github_url= { url: string;  [<Default("main")>] branch: string}

type myconfig = 
{
   [<AllowSingular>] author: string array
   url : github_url
    ...
}

By using attributes, I want to control JSON deserialization, to parse into the same results for

{
    "author" :  ["fable"],
    "url" : { "name": "xxx/xxx", "branch", "main" },
    ...
}

and

{ 
    "author":  "fable",
    "url": "xxx/xxx"
}

Is the use of attributes possible in Fable?

thautwarm avatar Jan 16 '22 03:01 thautwarm

@thautwarm I'm not sure about this so we need to ask the experts @alfonsogarciacaro and @Zaid-Ajaj. I don't know about custom attribute support, but know that there's some support for custom plugins that might handle custom attributes i.e https://github.com/Zaid-Ajaj/Feliz/tree/master/Feliz.CompilerPlugins but I have never used it myself

dbrattli avatar Jan 16 '22 08:01 dbrattli

Right now, attributes should be visible to plugins but are erased in generated code. This is because in JS we're always careful not to output too many things so the bundle size doesn't grow too much. Take note that F# already adds many attributes on its own. Maybe in the case of Python the bundle size doesn't matter too much and it's ok to output the attributes (probably filtering out the compiler generated ones). I think this should be possible to do in the Fable2Python step when generating the reflection info as the attribute info is kept in the Fable AST.

In the particular case of Json serialization, the community usually tends to customize things using custom coders instead of attributes in the type declaration though: https://thoth-org.github.io/Thoth.Json/documentation/auto/json-representation.html#extra-coders

alfonsogarciacaro avatar Jan 17 '22 03:01 alfonsogarciacaro

Thanks! @dbrattli @alfonsogarciacaro I'll do investigation and make PRs for this.

thautwarm avatar Jan 18 '22 08:01 thautwarm

@alfonsogarciacaro Yes, bundle size does not matter at all for Python (disk space is free), but loading times still matters. So we still need to be careful to not eagerly import everything for stuff that is rarely used. Lazy loading of modules is possible in Python so that is something we might have to consider down the line, e.g: https://github.com/ReactiveX/RxPY/blob/master/rx/operators/init.py#L60

dbrattli avatar Jan 18 '22 08:01 dbrattli

At the beginning I was considering to put all the reflection info in a separate file but discarded it because it messed up with Webpack integration (Fable 2 relied on the Webpack watcher), not sure if it would make sense to consider it again.

alfonsogarciacaro avatar Jan 18 '22 08:01 alfonsogarciacaro