11"""patch helpers."""
22
3- from typing import Optional
3+ from typing import Any , Optional
44
5- from pydantic import BaseModel , computed_field
5+ from pydantic import BaseModel , computed_field , model_validator
66
77
88class ElasticPath (BaseModel ):
@@ -19,20 +19,22 @@ class ElasticPath(BaseModel):
1919 key : Optional [str ] = None
2020 index : Optional [int ] = None
2121
22- def __init__ (self , * , path : str ):
23- """Convert JSON path to Elasticsearch script path.
22+ @model_validator (mode = "before" )
23+ @classmethod
24+ def validate_model (cls , data : Any ):
25+ """Set optional fields from JSON path.
2426
2527 Args:
26- path (str ): initial JSON path
28+ data (Any ): input data
2729 """
28- self . path = path .lstrip ("/" ).replace ("/" , "." )
30+ data [ " path" ] = data [ " path" ] .lstrip ("/" ).replace ("/" , "." )
2931
30- self . nest , self . partition , self . key = path .rpartition ("." )
32+ data [ " nest" ], data [ " partition" ], data [ " key" ] = data [ " path" ] .rpartition ("." )
3133
32- if self . key .isdigit ():
33- self . index = int (self . key )
34- self . path = f"{ self . nest } [{ self . index } ]"
35- self . nest , self . partition , self . key = self . nest .rpartition ("." )
34+ if data [ " key" ] .isdigit ():
35+ data [ " index" ] = int (data [ " key" ] )
36+ data [ " path" ] = f"{ data [ ' nest' ] } [{ data [ ' index' ] } ]"
37+ data [ " nest" ], data [ " partition" ], data [ " key" ] = data [ " nest" ] .rpartition ("." )
3638
3739 @computed_field # type: ignore[misc]
3840 @property
0 commit comments