question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add instance factory function

See original GitHub issue

I am using XSData to read dataclass instances which need additional checks.

The additional checks should be implemented on user supplied classes which derive from the XSData generated class:

XSData (raw) class:

@dataclass
class TModule(TTypeDefinition):
    import_: List[TImport] = field(
        default_factory=list,
        metadata={
            "name": "import",
            "type": "Element",
            "namespace": "http://xxx",
        }
    )

My class with additional checks:

class TModule(raw.TModule):
    def check(self):
         if condition ...

However, the problem is that my TModule is not instanciated during parsing. Currently, i use a “type hacking approach”

@functions_to_super
class TModule(raw.TModule):
    def check(self):
         if condition ...

where i copy the defined functions to the base class. This however is not ideal, since i want to present only my TModule to the user, not the raw classes.

Is there the option (or could this be added) to give a factory function to the parser

def createInstance(clazz);
     if clazz.__name__ = "TModule":
        return myTModule()

by which i can define for which dataclass which user supplied class is instanciated, so that the parsed objects dont contain any “raw” classes?

Kind regards

Hauke

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tefracommented, Jul 8, 2021

Thank you for the suggestion @hcw70

0reactions
tefracommented, Jul 9, 2021

No currently there are no validations, even though the generator adds restrictions as fields metadata, they are just hints for the developer. At some point I wish to work on something like this but it’s not really high on my priorities.

The binding process can only fail on unknown properties and errors during type conversions, both of them are configurable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript Factory Functions
A factory function is a function that returns a new object. Use Object.create() to create an object using an existing object as a...
Read more >
Factory Functions and the Module Pattern | The Odin Project
The factory function pattern is similar to constructors, but instead of using new to create an object, factory functions simply set up and...
Read more >
How to Create Objects Using Factory Functions in JavaScript
This article will cover building an object using factory functions, followed by an article covering constructor functions and using the new ...
Read more >
Factory Functions in JavaScript - Linux Hint
In JavaScript, a Factory function returns an object that can be reused for making multiple instances of the same object. It can additionally...
Read more >
What are factory functions in JavaScript ? - GeeksforGeeks
The Factory Function is similar to constructor functions/class functions, but instead of using new to create an object, factory functions ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found