CN-121979516-A - Dynamic model conversion method, device, equipment and storage medium based on configuration
Abstract
The application discloses a dynamic model conversion method, a device, equipment and a storage medium based on configuration, wherein the method comprises the steps of converting a source object into an intermediate representation object, obtaining an attribute configuration set, traversing each attribute configuration in the attribute configuration set, obtaining a secondary attribute configuration set according to associated key information in the current attribute configuration under the condition that the field type of the current attribute configuration in the traversing process is the object type, executing recursion conversion operation by applying the secondary attribute configuration set to the nested object attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object, executing basic type conversion operation on the original attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object under the condition that the field type of the current attribute configuration is the basic data type of a non-object, calling an attribute processor, and executing custom business logic processing operation on the intermediate representation object and the current attribute configuration which finish basic type conversion.
Inventors
- SUN XUEQIANG
Assignees
- 北京五八信息技术有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20251212
Claims (11)
- 1. A method for configuration-based dynamic model conversion, the method comprising: converting the source object to be converted into an intermediate representation object; Acquiring an attribute configuration set corresponding to a service grouping identifier from an external configuration source according to the designated service grouping identifier; Traversing each attribute configuration in the attribute configuration set according to a predefined sequence in the attribute configuration set; judging the field type of the current attribute configuration in the traversal process; Under the condition that the field type of the current attribute configuration is judged to be the object type, acquiring a secondary attribute configuration set according to the associated key information in the current attribute configuration, and executing recursion conversion operation on the secondary attribute configuration set by applying a nested object attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object; And under the condition that the field type of the current attribute configuration is judged to be the basic data type of a non-object, performing basic type conversion operation on an original attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object, calling an attribute processor bound with the current attribute configuration, and performing custom business logic processing operation on the intermediate representation object and the current attribute configuration which finish basic type conversion.
- 2. The method of claim 1, wherein the invoking the property handler bound to the current property configuration performs custom business logic processing operations on the intermediate representation object and the current property configuration that complete a base type conversion, comprising: Reading an attribute processor identification field in the current attribute configuration; acquiring a corresponding attribute processor implementation class from a pre-constructed processor registry according to the attribute processor identification field; instantiating the attribute processor implementation class to obtain an executable attribute processor instance; A processing interface for inputting the intermediate representation object and the current attribute configuration as parameters to the attribute processor instance; triggering a specific business rule defined in the attribute processor instance through the processing interface, wherein the specific business rule comprises at least one rule of data mapping, format verification and structured query generation; and modifying corresponding data content in the intermediate representation object according to the execution output of the attribute processor instance.
- 3. The method of claim 2, wherein the particular business rule comprises a data mapping rule; The triggering, through the processing interface, a specific business rule defined inside the attribute processor instance, including: Analyzing a data dictionary type identifier from the association key field or the extension information field of the current attribute configuration; Acquiring a data key to be converted corresponding to a field name field of the current attribute configuration from the intermediate representation object; combining the data dictionary type identifier and the data key to be converted, and initiating a query to a remote data dictionary service; receiving a semantical description value corresponding to the combination returned by the remote data dictionary service; Integrating the semantically described value into the intermediate representation object according to at least one preset output mode in the attribute processor instance, wherein the output mode comprises at least one of a mode of directly covering an original value corresponding to the data key to be converted, a mode of adding a key formed by adding the field name field with a prefix in the intermediate representation object and taking the semantically described value as a value, and a mode of replacing the value corresponding to the data key to be converted with a composite object containing the data key to be converted and the semantically described value.
- 4. The method according to claim 1, wherein, in the case that the field type of the current attribute configuration is determined to be the object type, the obtaining the secondary attribute configuration set according to the association key information in the current attribute configuration includes: extracting character strings recorded by an associated key field in the current attribute configuration as a secondary service grouping identifier; and repeatedly executing the step of acquiring the attribute configuration set from the external configuration source by taking the secondary service grouping identifier as a new query basis to acquire the secondary attribute configuration set specially used for describing the nested object structure, wherein the internal field constitution of the secondary attribute configuration set is the same as the internal field constitution of the attribute configuration set.
- 5. The method of claim 4, wherein said applying the secondary set of attribute configurations to the nested object attribute values in the intermediate representation object corresponding to the field names of the current attribute configurations comprises: checking a Boolean value set by an array identification field in the current attribute configuration; judging whether the nested object attribute value points to a single object array according to the Boolean value; Under the condition that the nested object attribute values point to a plurality of object arrays, the nested object attribute values are interpreted as an ordered object element list, and for each independent object element in the ordered object element list, the independent object element is taken as a new source object, and the secondary attribute configuration set is taken as a new configuration input, and recursive conversion operation is performed; And under the condition that the nested object attribute value points to a single object array, taking the single object directly referenced by the nested object attribute value as a new source object, and taking the secondary attribute configuration set as a new configuration input, and executing recursive conversion operation.
- 6. The method of claim 1, wherein the obtaining, from an external configuration source, a set of attribute configurations corresponding to the service packet identification according to the specified service packet identification, comprises: Initiating a query request containing the service group identifier to a configuration management service; Receiving and analyzing response data returned by the configuration management service to obtain the structured attribute configuration set; Each attribute configuration in the set of attribute configurations includes a service grouping field for distinguishing different service scenarios, a field name field for identifying a target attribute, a field header field for describing the target attribute, a field type field for declaring an attribute data structure, an attribute processor identification field for specifying processing logic, an array identification field for indicating whether an attribute is a set, a null identification field for specifying whether an attribute value is allowed to be null, an association key field for associating other sets of configurations, an ordering field for controlling a processing order, a default value field for providing default values, and an extension information field for carrying extension parameters.
- 7. The method of claim 1, wherein performing a base type conversion operation on the original attribute value in the intermediate representation object corresponding to the field name of the current attribute configuration comprises: analyzing the type declaration information recorded in the field type field from the current attribute configuration; Determining a target basic data type matched with the current attribute configuration according to the type declaration information; Checking whether a key value pair with a value of a field name field configured by the current attribute as a key exists in the intermediate representation object; Extracting the original attribute value from the key-value pair, if the key-value pair exists; formatting and converting the original attribute value according to the target basic data type to generate a converted attribute value conforming to the target basic data type; and replacing the value of the key value pair corresponding to the field name field in the intermediate representation object with the converted attribute value.
- 8. The method of claim 1, wherein converting the source object to be converted into an intermediate representation object comprises: receiving an input initial source object needing model conversion; analyzing actual runtime type information of the initial source object; In case the actual run-time type information indicates that the initial source object itself belongs to a predefined key-value pair set type, directly designating the initial source object as the intermediate representation object; traversing all readable attributes or common fields of the initial source object through a reflection mechanism under the condition that the actual runtime type information indicates that the initial source object does not belong to a predefined key value pair set type; acquiring name character strings of each readable attribute or public field and currently stored numerical values thereof; combining each name character string with the corresponding currently stored numerical value to form a basic key value pair unit; and assembling all the basic key value pair units, constructing a container object meeting the requirement of a predefined key value pair set type, and determining the container object as the intermediate representation object.
- 9. A configuration-based dynamic model conversion apparatus, the apparatus comprising: The processing module is used for converting the source object to be converted into an intermediate representation object; Acquiring an attribute configuration set corresponding to a service grouping identifier from an external configuration source according to the designated service grouping identifier; Traversing each attribute configuration in the attribute configuration set according to a predefined sequence in the attribute configuration set; judging the field type of the current attribute configuration in the traversal process; The conversion module is used for acquiring a secondary attribute configuration set according to the associated key information in the current attribute configuration under the condition that the field type of the current attribute configuration is judged to be the object type, and executing recursion conversion operation on the secondary attribute configuration set by applying a nested object attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object; And under the condition that the field type of the current attribute configuration is judged to be the basic data type of a non-object, performing basic type conversion operation on an original attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object, calling an attribute processor bound with the current attribute configuration, and performing custom business logic processing operation on the intermediate representation object and the current attribute configuration which finish basic type conversion.
- 10. A computer device comprising a memory and a processor, the memory storing a computer program executable on the processor, characterized in that the processor implements the steps of the configuration-based dynamic model transformation method of any one of claims 1 to 8 when the program is executed.
- 11. A computer readable storage medium, on which a computer program is stored, characterized in that the computer program, when being executed by a processor, implements the steps of the configuration-based dynamic model conversion method of any one of claims 1 to 8.
Description
Dynamic model conversion method, device, equipment and storage medium based on configuration Technical Field The present application relates to, but not limited to, the field of software design technologies, and in particular, to a method, an apparatus, a device, and a storage medium for converting a dynamic model based on configuration. Background In the software program development process, object transformation is an extremely common and fundamental requirement. Typical scenarios include, but are not limited to, converting database mapped Entity objects (entities) into value objects (VO/DTOs) for front-end presentation or interface transmission, or format adaptation when transferring data between different business layers, different services. In an object-oriented programming language such as Java, when the attribute structures (names and types) of a source object and a target object are completely consistent, a developer can simplify the attribute replication process by means of a reflection mechanism, a byte code enhancement technique (such as CGLib) or an object mapping framework (such as BeanUtils, dozer, mapStruct), and automatically or semi-automatically generate the target object. However, in a practical scenario, the service is more complex, and more scenarios are encountered, such as different types, need to query other dependency information, need to modify other fields, and the like, and complex conversion logic is generally scattered in a service code or a custom converter in a hard-coded form, which results in close coupling of the conversion logic and core service logic, so that maintainability of the code is reduced, and when the conversion rule changes, the conversion logic needs to go deep into the service code to modify, thereby increasing the risk of errors and the complexity of testing. Disclosure of Invention In view of this, the embodiments of the present application at least provide a method, an apparatus, a device and a storage medium for converting a dynamic model based on configuration. Converting the source object to be converted into an intermediate representation object; Acquiring an attribute configuration set corresponding to a service grouping identifier from an external configuration source according to the designated service grouping identifier; Traversing each attribute configuration in the attribute configuration set according to a predefined sequence in the attribute configuration set; judging the field type of the current attribute configuration in the traversal process; Under the condition that the field type of the current attribute configuration is judged to be the object type, acquiring a secondary attribute configuration set according to the associated key information in the current attribute configuration, and executing recursion conversion operation on the secondary attribute configuration set by applying a nested object attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object; And under the condition that the field type of the current attribute configuration is judged to be the basic data type of a non-object, performing basic type conversion operation on an original attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object, calling an attribute processor bound with the current attribute configuration, and performing custom business logic processing operation on the intermediate representation object and the current attribute configuration which finish basic type conversion. In another aspect, an embodiment of the present application provides another configuration-based dynamic model conversion apparatus, where the method includes: The processing module is used for converting the source object to be converted into an intermediate representation object; Acquiring an attribute configuration set corresponding to a service grouping identifier from an external configuration source according to the designated service grouping identifier; Traversing each attribute configuration in the attribute configuration set according to a predefined sequence in the attribute configuration set; judging the field type of the current attribute configuration in the traversal process; The conversion module is used for acquiring a secondary attribute configuration set according to the associated key information in the current attribute configuration under the condition that the field type of the current attribute configuration is judged to be the object type, and executing recursion conversion operation on the secondary attribute configuration set by applying a nested object attribute value corresponding to the field name of the current attribute configuration in the intermediate representation object; And under the condition that the field type of the current attribute configuration is judged to be the basic data type of a non-object, perform