datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Add documentation for custom template

Open koxudaxi opened this issue 4 years ago • 6 comments

There is no document of the custom template. We should write a document on how to use the feature.

Related issues

https://github.com/koxudaxi/datamodel-code-generator/issues/144

koxudaxi avatar Jul 28 '20 16:07 koxudaxi

I was wanting to add https://github.com/snowplow/snowplow-python-tracker/blob/master/snowplow_tracker/self_describing_json.py to the template somehow, but did not really find any way to make that happen. so it would be nice to have some documentation around extending things

camerondavison avatar Sep 01 '20 22:09 camerondavison

@camerondavison Thank you for your comment.

I was wanting to add https://github.com/snowplow/snowplow-python-tracker/blob/master/snowplow_tracker/self_describing_json.py to the template somehow,

Sorry, What did you want? Did you want to inherit the class?

koxudaxi avatar Sep 02 '20 01:09 koxudaxi

I think maybe I want to wrap it in that class. Since I need to write the schema field and the model is the data.

camerondavison avatar Sep 02 '20 01:09 camerondavison

@camerondavison I don't know how to use snowplow-python-tracker. Do you want this output?

from pydantic import BaseModel

class Model(BaseModel):
    # inject schema path when generate this model
    _schema_path: str = "iglu:com.acme/viewed_product/jsonschema/2-0-2"

    product_id: int = "ASO01043"
    price: float = 49.95
    walrus: int = 1000

    def to_json(self):
        return {
            "schema": self._schema_path,
            "data": self.dict()
        }

    def to_string(self):
        import json
        return json.dumps(self.to_json())

print(Model().to_string())
# {"schema": "iglu:com.acme/viewed_product/jsonschema/2-0-2", "data": {"product_id": "ASO01043", "price": 49.95, "walrus": 1000}}

Otherwise, Would you please write an example of the output that you expect here?

koxudaxi avatar Sep 02 '20 02:09 koxudaxi

Yes that is what I want.

camerondavison avatar Sep 02 '20 11:09 camerondavison

@camerondavison Unfortunately, This code-generator can't output the schema path. But, This viewpoint looks good. I will implement it after we resolve other issues.

koxudaxi avatar Sep 02 '20 15:09 koxudaxi

I added https://koxudaxi.github.io/datamodel-code-generator/custom_template/

koxudaxi avatar Apr 30 '23 17:04 koxudaxi