Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Attributes? #48

Open
thautwarm opened this issue Jan 16, 2022 · 5 comments
Open

Support for Attributes? #48

thautwarm opened this issue Jan 16, 2022 · 5 comments

Comments

@thautwarm
Copy link

thautwarm commented Jan 16, 2022

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?

@dbrattli
Copy link
Collaborator

@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

@alfonsogarciacaro
Copy link
Member

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

@thautwarm
Copy link
Author

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

@dbrattli
Copy link
Collaborator

@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

@alfonsogarciacaro
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants