Giter Site home page Giter Site logo

Comments (6)

koxudaxi avatar koxudaxi commented on August 25, 2024 2

@gk-patel
Thank you for creating this issue.
I think it's a good idea.
The behavior may have to be an option of the command line 🤔

I will implement this feature after remove bugs.

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on August 25, 2024

@gk-patel
I have released a new version as 0.6.15.
The version has the --reuse-model option that you want.

I put an example.

Input

{
"Arm Right": {
"Joint 1": 5,
"Joint 2": 3,
"Joint 3": 66
},
"Arm Left": {
"Joint 1": 55,
"Joint 2": 13,
"Joint 3": 6
},
"Head": {
"Joint 1": 10
}
}

Output

class ArmRight(BaseModel):
Joint_1: int = Field(..., alias='Joint 1')
Joint_2: int = Field(..., alias='Joint 2')
Joint_3: int = Field(..., alias='Joint 3')
class ArmLeft(BaseModel):
Joint_1: int = Field(..., alias='Joint 1')
Joint_2: int = Field(..., alias='Joint 2')
Joint_3: int = Field(..., alias='Joint 3')
class Head(BaseModel):
Joint_1: int = Field(..., alias='Joint 1')
class Model(BaseModel):
Arm_Right: ArmRight = Field(..., alias='Arm Right')
Arm_Left: ArmRight = Field(..., alias='Arm Left')
Head: Head

I'm sorry, My work is too late.

from datamodel-code-generator.

gk-patel avatar gk-patel commented on August 25, 2024

@koxudaxi No worries man, looks nice.

Although, the following would have been much more cooler,

...
class ArmLeft(ArmRight): 
   pass 
...

If you can do that, it would look much nicer. But if you cannot, then you can close the issue. I leave it up to you.

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on August 25, 2024

@gk-patel
No problem, It's a good idea.
I can implement the changes easily.

But, I found a problem with a case.
The code generator generates the Enum class from JSONSchema/OpenAPI.
Enum class can't be extended.

>> class B(A): pass
TypeError: Cannot extend enumerations

We should assign to another value. or we don't touch duplicated enum 🤔

from datamodel-code-generator.

gk-patel avatar gk-patel commented on August 25, 2024

Well, IMHO the easiest way would be to check if the content of enums is exactly the same,

  • if it is exactly the same, just use the same name everywhere (so in that case, dont create class B)
  • if it not exactly the same, the create a seperate enum without inheritance, (so in that case, create class A and B)

Do you have any arguments against this idea ?

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on August 25, 2024

@gk-patel
OK. I released a new version as 0.6.17
This version has changed your idea.

class ArmLeft(ArmRight):
pass
class Head(BaseModel):
Joint_1: int = Field(..., alias='Joint 1')
class Model(BaseModel):
Arm_Right: ArmRight = Field(..., alias='Arm Right')
Arm_Left: ArmLeft = Field(..., alias='Arm Left')

The enum will reuse the same model. A duplicate model will be created too. Because some models may need the enum from other modules.

class Animal(Enum):
dog = 'dog'
cat = 'cat'
snake = 'snake'
class Pet(Enum):
dog = 'dog'
cat = 'cat'
snake = 'snake'
class User(BaseModel):
name: Optional[str] = None
animal: Optional[Animal] = None
pet: Optional[Animal] = None

from datamodel-code-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.