Skip to content

Support for Attributes? #48

Description

@thautwarm

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?

Activity

  1. dbrattli commented on Jan 16, 2022

    @dbrattli
    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

  2. alfonsogarciacaro commented on Jan 17, 2022

    @alfonsogarciacaro
    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

  3. thautwarm commented on Jan 18, 2022

    @thautwarm
    Author

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

  4. dbrattli commented on Jan 18, 2022

    @dbrattli
    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

  5. alfonsogarciacaro commented on Jan 18, 2022

    @alfonsogarciacaro
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions