Method for converting front-end entity data into entity object
By defining and using enumeration PropType to distinguish entity attribute types and defining attribute description interfaces and entity description interfaces, the problem that front-end applications cannot correctly convert back-end data into entity objects is solved, and data processing is achieved accuracy and robustness.
Patent Information
- Application Number
- CN202510102661.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-22
- Publication Date
- 2025-05-16
AI Technical Summary
In the prior art, front-end applications cannot correctly convert the data received from the back-end based on entity description information to entity objects that conform to a specific structure, especially when the data contains associated entities, entity arrays, and conversions are inefficient and error-prone.
By defining three types of entity attributes: original attributes, entity attributes, and entity array attributes, using enumeration PropType to distinguish them, and defining attribute description interfaces and entity description interfaces, we ensure that the correct conversion and verification can be performed according to the specific attribute type when creating entity objects.
It realizes that front-end applications can correctly convert data based on entity description information, avoid data conversion errors caused by type confusion, and enhance the robustness and accuracy of data processing.
Smart Images

Figure CN120010962A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of data processing technology, and specifically relates to a method for converting front-end entity data into entity objects. Background Art
[0002] With the increasing popularity and complexity of Web applications, the front-end and back-end separation architecture has become one of the standard models of modern Web development. In this model, the front-end is mainly responsible for the display and interaction of the user interface, while the back-end provides data processing services. In order to achieve efficient collaboration between the front-end and the back-end, data transmission is usually carried out in JSON format. After the front-end receives the JSON data returned by the back-end, it needs to convert it into an object that the front-end can understand and operate, that is, an entity object. The entity object is a front-end data model corresponding to the back-end data model. It can be easily bound to the front-end framework or library (such as React, Vue, Angular, etc.) to achieve the effect of data-driven view.
[0003] In existing technologies, front-end developers usually manually write code to convert JSON data obtained from the back-end into entity objects. Although this approach can meet basic needs, when faced with complex entity objects, especially when the entity objects contain associated entities, entity arrays, and when the original data needs to be converted to a specific format, the manual conversion method is not only inefficient but also prone to errors. Summary of the invention
[0004] The present application provides a method for converting front-end entity data into entity objects, so as to solve the problem in the prior art that the front-end application cannot correctly convert the data received from the back-end into entity objects conforming to a specific structure according to the entity description information.
[0005] The technical solution adopted in this application is:
[0006] The present application embodiment provides a method for converting front-end entity data into entity objects, comprising:
[0007] Define three types of entity attributes: primitive attributes, entity attributes, and entity array attributes, which are distinguished by enumeration PropType;
[0008] Define the attribute description interface as the parent interface of various attribute description interfaces;
[0009] Subdividing the primitive attributes to ensure correct conversion and verification according to the specific primitive types when creating entity objects;
[0010] Define a primitive attribute interface, an entity attribute interface and an entity array attribute interface for describing the entity attributes, define an entity description interface and an entity creator interface for describing the structure of an entity and creating an entity object;
[0011] The type of each of the entity attributes is checked, and corresponding conversion logic is executed according to the different types.
[0012] The method for converting front-end entity data into entity objects provided by the present application also includes the following additional technical features: the three types of entity attributes defined: original attributes, entity attributes, and entity array attributes are distinguished by enumerating PropType, specifically:
[0013] The primitive property refers to a property whose value type is a basic data type, and such properties are identified by enumerating the Primitive member in PropType;
[0014] The entity property and the entity array property represent a single entity object and a collection of entity objects respectively, and these two property types are distinguished by enumerating the Entity and EntityArray members in PropType.
[0015] According to one embodiment of the present application, the attribute description interface is defined as the parent interface of various attribute description interfaces, specifically: the attribute description interface is defined as the parent interface, so that the attribute description interface contains basic information common to all attributes, so as to facilitate inheritance and extension of primitive attributes, entity attributes and entity array attribute sub-interfaces.
[0016] According to an embodiment of the present application, the original attributes are subdivided to ensure that correct conversion and verification can be performed according to the specific original type when creating the entity object, specifically:
[0017] Create an enumeration type to list all primitive data types and reference the type in the attribute description interface: when defining the description interface of the primitive attribute, add an attribute valueType, whose value is a member of the PrimitiveValueType enumeration; when creating an entity object, perform corresponding data conversion according to the specific value of valueType.
[0018] According to an embodiment of the present application, the definition of the original attribute interface, the entity attribute interface and the entity array attribute interface is used to describe the entity attributes, specifically:
[0019] The definition of the primitive property interface is to define an interface PrimitivePropSchema, which is used to describe properties whose value types are basic data types. This interface is extended from the PropSchema interface and adds a valueType attribute to further subdivide the specific value type;
[0020] The definition of the entity property interface is to define an interface EntityPropSchema, which is used to describe the properties whose value type is another entity object. This interface is also extended from the PropSchema interface and contains an associatedEntitySchema attribute to describe the entity structure associated therewith;
[0021] The definition of the entity array property interface is to define an interface EntityArrayPropSchema, which is used to describe properties whose value type is a collection of entity objects. This interface is also extended from the PropSchema interface and includes a childEntitySchema property to describe the structure of a child entity.
[0022] According to an embodiment of the present application, the definition of the entity description interface and the entity creator interface is used to describe the structure of the entity and create the entity object, specifically:
[0023] The definition of the entity description interface is to define an interface EntitySchema, which is used to describe the overall structure of the entity. This interface contains a propSchemas array, which contains the description information of all entity properties, and an entityConstructor property, which is used to specify the constructor used when creating an entity object;
[0024] The definition of the entity creator interface is to define an interface IEntityCreator, which is used to describe the method of creating an entity object. The interface includes a create method, which receives entity description information and entity data as input parameters and returns an entity object.
[0025] According to an embodiment of the present application, the type of each entity attribute is checked, and corresponding conversion logic is executed according to different types, specifically:
[0026] Before creating an entity object, you must first obtain the entity description information, which contains the type information of each attribute;
[0027] For each attribute, read the type information from its corresponding description information;
[0028] For the original attribute, perform the corresponding data conversion according to the specific value of valueType;
[0029] For entity attributes, you need to create a new entity object based on the associated entity description information and pass the relevant data to the creation logic of the entity object;
[0030] For entity array attributes, you need to traverse each element in the array and create a child entity object for each element, using the description information of the child entity to perform the conversion.
[0031] A computer program product containing instructions, when running on a device, enables the device to execute the steps in a method for converting front-end entity data into entity objects.
[0032] A computer-readable storage medium stores a program, which, when executed by a processor, implements the steps in a method for converting front-end entity data into entity objects.
[0033] An electronic device comprises a memory, a processor and a program stored in the memory and executable on the processor. When the processor executes the program, the steps in the method of converting front-end entity data into entity objects are implemented.
[0034] Due to the adoption of the above technical solution, the beneficial effects achieved by this application are as follows:
[0035] This application clarifies the attribute type and distinguishes the three types of entity attributes - primitive attributes, entity attributes and entity array attributes - by defining the enumeration PropType, so that the front-end application can clearly identify the type of each attribute. This avoids data conversion errors caused by type confusion and enhances the robustness of data processing. This application unifies the attribute description interface and defines a general attribute description interface PropSchema as the parent interface of all attribute description interfaces to ensure the consistency and scalability of attribute description. This design makes it easier to add new attribute types and also facilitates the management and maintenance of attribute description information. This application refines the primitive attribute type and further subdivides the primitive attributes into specific types, such as strings, integers, dates, etc., and identifies these types through the enumeration PrimitiveValueType. This measure ensures that when creating an entity object, correct data conversion and verification can be performed according to the specific primitive type, avoiding errors caused by type mismatch. This application standardizes the entity description and creation process, and defines a special primitive attribute interface, entity attribute interface and entity array attribute interface to describe the characteristics of different types of entity attributes. In addition, by defining the entity description interface EntitySchema and the entity creator interface IEntityCreator, the description method of the entity structure and the creation logic of the entity object are clarified, and the standardization of the entity object creation process is improved. This application supports the conversion of dynamic data. When creating an entity object, the corresponding conversion logic is executed according to the type of entity attribute to ensure the accuracy and consistency of the data. This dynamic conversion mechanism enables the front-end application to flexibly respond to different types of data and reduce the maintenance cost caused by hard coding. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0037] Figure 1 A flowchart of a method for converting front-end entity data into entity objects provided in an embodiment of the present application. DETAILED DESCRIPTION
[0038] In order to more clearly illustrate the overall concept of the present application, a detailed description is given below in an illustrative manner in conjunction with the accompanying drawings.
[0039] In the following description, many specific details are set forth to facilitate a full understanding of the present application. However, the present application may also be implemented in other ways different from those described herein. Therefore, the protection scope of the present application is not limited by the specific embodiments disclosed below. It should be noted that the embodiments of the present application and the features in each embodiment may be combined with each other without conflict.
[0040] In the present application, unless otherwise clearly specified and limited, a first feature "above" or "below" a second feature may be that the first and second features are in direct contact, or the first and second features are in indirect contact through an intermediate medium. In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in an appropriate manner in any one or more embodiments or examples.
[0041] Example 1
[0042] like Figure 1 As shown, a method for converting front-end entity data into entity objects includes:
[0043] S100. Define three types of entity attributes: primitive attributes, entity attributes, and entity array attributes, which are distinguished by enumeration PropType.
[0044] Specifically, primitive attributes: Primitive attributes refer to those attributes whose value types are basic data types, such as strings, integers, floating-point numbers, Boolean values, dates, etc. Such attributes are identified by enumerating the Primitive member in PropType. Function: Clarify the data type of the attribute to facilitate the front-end to perform correct data conversion when creating entity objects. Provides type checking capabilities to ensure the consistency and correctness of data types when creating entity objects.
[0045] Entity attributes: Entity attributes are attributes whose value type is another entity object. For example, an employee entity may have a department attribute, which is also an entity. Such attributes are identified by the Entity member in the PropType enumeration. Purpose: Describes the association relationship between entities so that the front end can understand the dependency relationship between entities when processing entity data. Supports mutual references between entities to facilitate the correct association of related entities when creating entity objects.
[0046] Entity Array Properties: Entity array properties are properties whose value type is multiple sub-entity objects. For example, a project entity may have a list of participants, each of which is a separate entity. Such properties are identified by the EntityArray member in the PropType enumeration. Purpose: Describes the relationship between an entity and multiple sub-entities, supporting one-to-many association relationships. When creating entity objects, entity array properties can be properly handled to create and associate multiple sub-entity objects.
[0047] Use the enumeration PropType to distinguish property types: By defining the enumeration PropType to distinguish the above three property types, the following effects can be achieved: Clear type: Enumeration members provide clear type information, so that the front-end application can quickly identify the type of the property when processing entity properties. Code readability: Using enumeration types to represent property types improves code readability and maintainability. Type safety: Enumeration types provide a type-safe way to avoid type errors when processing entity properties.
[0048] For example, suppose there is an application to manage the book information of a library, including books, authors, and publishers. Each book can have multiple authors and belong to a publisher. Entity definition: Book: ID (bookId): primitive attribute, string type. Title (title): primitive attribute, string type. Publication date (publicationDate): primitive attribute, date type. Price (price): primitive attribute, floating point type. Authors (authors): entity array attribute, consisting of multiple Author entities. Publisher (publisher): entity attribute, pointing to a Publisher entity. Author (Author): ID (authorId): primitive attribute, string type. Name (name): primitive attribute, string type. Birthday (birthdate): primitive attribute, date type. Publisher (Publisher): ID (publisherId): primitive attribute, string type. Name (name): primitive attribute, string type. Founded date (foundedDate): primitive attribute, date type. Enumeration PropType: Define enumeration PropType to distinguish the types of entity attributes:
[0049]
[0050] Property description interface: defines the property description interface PropSchema and its sub-interfaces:
[0051]
[0052]
[0053] Entity description interface: Define the entity description interface EntitySchema:
[0054]
[0055] Entity description information: Define specific entity description information:
[0056]
[0057]
[0058] Create entity objects: When creating entity objects, execute the corresponding conversion logic according to the enumeration PropType:
[0059]
[0060]
[0061] Through the above steps, we have defined three types of entity attributes and distinguished them through the PropType enumeration. This enables correct data conversion and verification based on specific attribute types when creating entity objects, ensuring the accuracy and consistency of data processing.
[0062] Furthermore, you can also add attribute validation logic. In addition to basic type conversion, you can also add validation logic in the attribute description interface, for example: Length validation: For string type attributes, you can set the maximum length or minimum length. Format validation: For attributes such as date, time, and email, you can set format validation rules.
[0063] S200. Define a property description interface as a parent interface of various property description interfaces.
[0064] Specifically, define a common property description interface: First, define a common property description interface PropSchema, which will contain the basic information common to all properties.
[0065] This interface should at least contain the name and type of the property. The property name is used to identify the property, while the type is used to distinguish whether the property is a primitive property, an entity property, or an entity array property. Various property description interfaces inherit from the general interface: Next, define specific property description interfaces, which will inherit from the PropSchema interface and add additional information required for specific types of properties.
[0066] Advantages of a generic interface: By defining a generic property description interface as a parent interface, you can gain the following advantages:
[0067] a. Unified data structure: All property descriptions follow a unified data structure, which helps maintain code consistency and readability. Whether dealing with primitive properties, entity properties, or entity array properties, they can be processed in a similar way. b. Easy to expand: When you need to add a new property type, you can directly inherit from the PropSchema interface and add the necessary information. This makes it easy to expand the property type without disrupting the existing code structure. c. Reduce coupling: By defining a common parent interface, the coupling between different property types can be reduced. Each type of property has its own characteristics, but follows the same design pattern at a high level, making it easier for them to work together. d. Improve maintainability: When you need to modify some common information of the property description, you only need to modify the PropSchema interface. This allows the core information of the property description to be centrally managed and maintained, reducing code redundancy and improving maintenance efficiency.
[0068] For example, suppose there is a library management system that contains the following entities: Book, Author, and Publisher. Each book can have multiple authors and belong to a publisher. Interface definition: Define enumeration PropType: First, define an enumeration PropType to distinguish different property types:
[0069]
[0070] Define the enumeration PrimitiveValueType: Next, define an enumeration PrimitiveValueType to subdivide the type of primitive attributes:
[0071]
[0072] Define a general property description interface PropSchema: Define a general property description interface PropSchema as the parent interface of various property description interfaces:
[0073]
[0074] Define specific property description interfaces: Next, define specific property description interfaces, which will inherit from the PropSchema interface and add additional information required for specific types of properties:
[0075]
[0076]
[0077] Entity Description: Now, define the specific description for each entity:
[0078]
[0079]
[0080] Create entity objects: When creating entity objects, perform corresponding conversion logic according to the attribute description information:
[0081]
[0082]
[0083] Through the above steps, a general property description interface PropSchema is defined as the parent interface, and specific property description interfaces are defined to describe primitive properties, entity properties, and entity array properties. The definition of these interfaces enables correct data conversion and verification according to specific property types when creating entity objects, ensuring the accuracy and consistency of data processing.
[0084] Furthermore, you can also add attribute validation logic to the attribute description interface to ensure that when creating entity objects, the data is not only converted correctly, but also meets specific business rules. For example, you can add length validation, format validation, range validation, etc.
[0085] S300: Subdivide the original attributes to ensure that correct conversion and verification can be performed according to the specific original type when creating an entity object.
[0086] Specifically, the purpose of primitive attribute segmentation is that primitive attributes usually refer to those attributes whose value types are basic data types (such as strings, integers, floating-point numbers, Boolean values, dates, etc.). In the process of creating entity objects, we need to ensure that the values of these basic types can be correctly converted and verified. The purpose of segmenting primitive attributes is: Clear type: Ensure that the type definition of each attribute is clear to avoid data conversion errors caused by type confusion. Accurate conversion: When creating entity objects, correct data conversion can be performed according to the specific type, such as converting a string to a date or integer. Validation: Effectively verify the incoming data to ensure that the data conforms to the expected format and range, and prevent invalid or illegal data from entering the system.
[0087] How to subdivide primitive attributes: In order to achieve the above goals, it is necessary to subdivide primitive attributes and add corresponding type information in the attribute description interface. The following are the specific steps to subdivide primitive attributes: Define the enumeration PrimitiveValueType: Create an enumeration type that lists all possible basic data types, such as String, Integer, Float, Boolean, Date, DateTime, and Enum. Add type information in the attribute description interface: When defining the description interface of the primitive attribute, add a valueType attribute whose value is a member of the PrimitiveValueType enumeration. Apply type information for conversion and verification: When creating an entity object, perform corresponding data conversion and verification according to the specific value of valueType. For example, if valueType is Date, you need to convert the incoming string data into a Date object and verify whether its format is correct; if it is Integer, you need to ensure that the data is an integer and verify whether it is within a reasonable range.
[0088] For example, suppose there is a simple user management system that contains a user entity (User) with the following primitive attributes: User ID (userId): string type. Age (age): integer type. Date of birth (dob): date type. Is active (isActive): Boolean type. Define the enumeration PrimitiveValueType: First, define an enumeration PrimitiveValueType to distinguish different primitive types:
[0089]
[0090] Define the primitive property description interface PrimitivePropSchema, then add type information to the property description interface to ensure that the type definition of each property is clear:
[0091] interface PrimitivePropSchema extends PropSchema{
[0092] valueType:PrimitiveValueType;
[0093] }
[0094] It is assumed here that there is already a general property description interface PropSchema, which contains the basic information of the property.
[0095] Define the attribute description information of the user entity. Next, define the attribute description information of the user entity and subdivide the type of each attribute:
[0096]
[0097]
[0098] PropType.Primitive is used here to mark these properties as primitive properties, and the specific type is specified through valueType.
[0099] Perform type conversion and verification when creating entity objects. Finally, when creating entity objects, perform corresponding conversion and verification according to the type of the attribute:
[0100]
[0101]
[0102] In this function, the following operations are performed according to the valueType of the attribute: String type attributes are directly assigned. Integer type attributes are converted through parseInt and verified to be a legal integer. Date type attributes are converted through new Date and verified to be a legal date. Boolean type attributes are converted to Boolean values through string comparison.
[0103] Now, you can use this function to create a user entity object:
[0104]
[0105] In this example, rawData contains the original data, a User entity object is created through the createUser function, and the corresponding type conversion and verification are performed. In this way, it is ensured that when the entity object is created, the original attributes can be correctly converted and verified according to the specific type, thereby improving the accuracy and reliability of data processing.
[0106] Furthermore, an event-driven architecture can be introduced to trigger corresponding events when attribute values change, so as to notify other systems or components to perform corresponding processing. Furthermore, data encryption can be supported. For sensitive data, encryption can be performed before storage and automatically decrypted when reading.
[0107] S400, defining a primitive attribute interface, an entity attribute interface and an entity array attribute interface for describing the entity attributes, and defining an entity description interface and an entity creator interface for describing the structure of an entity and creating an entity object.
[0108] Specifically, the primitive attribute interface: The primitive attribute interface is used to describe simple attributes of an entity, that is, attributes whose value types are basic data types (such as strings, integers, floating-point numbers, Boolean values, dates, etc.). Fixed type: The type of a primitive attribute is usually fixed, such as a string or integer. Simple conversion: When creating an entity object, the value of a primitive attribute usually only requires simple type conversion.
[0109] Entity attribute interface: The entity attribute interface is used to describe a reference attribute of an entity, that is, the attribute references another entity object.
[0110] Type is Entity: The value type of an entity attribute is another entity type. Dependency: An entity attribute represents the relationship between two entities.
[0111] Entity array attribute interface: The entity array attribute interface is used to describe a reference array attribute of an entity, that is, the attribute references an array of multiple entity objects.
[0112] Type is entity array: The value type of the entity array attribute is an array of multiple entity types. Collection relationship: The entity array attribute represents the relationship between an entity and multiple other entities.
[0113] Entity Description Interface: The entity description interface is used to describe the overall structure of an entity, including all attributes of the entity and their types.
[0114] Attribute collection: The entity description interface contains all the attribute descriptions of the entity. Constructor: The entity description interface also contains the constructor information used to create the entity object.
[0115] Entity creator interface: The entity creator interface is used to describe the process of how to create entity objects from raw data, including how to handle different types of data conversion and validation.
[0116] Data conversion: responsible for converting raw data into entity objects. Data validation: responsible for verifying whether the raw data conforms to the attribute description of the entity.
[0117] For example, suppose there is a simple library management system that includes the following entities: Book, Author, and Publisher. Each book can have multiple authors and belong to a publisher.
[0118] Define enumeration PropType and PrimitiveValueType: First, define enumeration PropType and PrimitiveValueType to distinguish different property types and primitive types:
[0119]
[0120]
[0121] Define the property description interface
[0122] Next, define a specific attribute description interface to describe various attribute types of entities:
[0123]
[0124] Define entity description interface
[0125] Define the entity description interface to describe the overall structure of the entity:
[0126]
[0127] Defining entity constructors
[0128] Define the entity constructor to create entity objects:
[0129]
[0130]
[0131] Defines the entity description
[0132] Define the description information of each entity, including the type of attributes and constructor information:
[0133]
[0134]
[0135] Define the entity creator interface and its implementation: Define the entity creator interface and implement the specific creation logic:
[0136]
[0137]
[0138] Creating entity objects using the entity creator
[0139] Now you can use the EntityCreatorImpl class to create entity objects:
[0140]
[0141] Through the above steps, the primitive attribute interface, entity attribute interface and entity array attribute interface are defined, and the entity description interface and entity creator interface are used to describe the entity structure and the process of creating entity objects. This method not only helps to organize and manage the code, but also improves the readability and maintainability of the system.
[0142] Furthermore, lifecycle management can be introduced to add a lifecycle management mechanism for entity attributes, such as the state of the attribute (such as activation, deactivation, deletion, etc.), and corresponding processing can be performed according to the state when the entity is created or updated. Furthermore, the error handling mechanism can be enhanced when creating entity objects, which is not limited to basic type conversion and validation errors, but also includes more complex business logic error handling.
[0143] S500: Check the type of each entity attribute, and execute corresponding conversion logic according to the type.
[0144] Specifically, when creating an entity object, we need to ensure that the incoming data conforms to the type defined by the entity attribute, and convert the data appropriately if necessary to ensure the correctness and availability of the data. In this way, we can prevent problems caused by type mismatch or incorrect data format.
[0145] Identify attribute types: Determine the data type of each entity attribute. Perform type conversion: Perform appropriate conversion on the incoming data based on the attribute type. Perform data validation: Ensure that the converted data conforms to the expected format and range.
[0146] Identify the property type: First, you need to know the specific type of each property. This can be achieved by defining a property description interface, which contains the name and type information of the property. For example, you can define a PropSchema interface, which contains the basic description information of the property.
[0147]
[0148]
[0149] The type of the attribute can be a primitive type (such as string, integer, date, etc.), or an entity type or entity array type.
[0150] Perform type conversion: Once the type of the attribute is determined, the next step is to perform the corresponding conversion logic based on the type of the attribute. The conversion logic may include but is not limited to: String to integer conversion: If the attribute type is an integer and the incoming data is in string form, it needs to be converted to an integer. String to date conversion: If the attribute type is a date and the incoming data is in string form, it needs to be converted to a date object. Boolean value conversion: If the attribute type is a Boolean value and the incoming data is in string form (such as "true" or "false"), it needs to be converted to a Boolean value. Creation of entity objects: If the attribute type is an entity, an entity object needs to be created based on the description information of the child entity. Creation of entity arrays: If the attribute type is an entity array, an entity array needs to be created based on the description information of the child entity.
[0151] Perform data validation: After performing type conversion, the converted data needs to be validated to ensure that the data conforms to the expected format and range. The validation steps may include but are not limited to: Format validation: Ensure that the converted data format is correct, such as date format, email format, etc. Range validation: Ensure that the converted data value is within the allowed range, such as age should be greater than zero. Uniqueness validation: For attributes that need to be unique, it is necessary to verify whether they are repeated. Non-empty validation: Ensure that certain required attributes are not empty.
[0152] For example, suppose there is a simple user management system that contains a user entity (User) with the following attributes: User ID (userId): string type. Age (age): integer type. Date of birth (dob): date type. Is active (isActive): Boolean type.
[0153] Define the attribute description interface: First, you need to define an attribute description interface to describe the attributes of an entity, including the name and type information of the attribute.
[0154]
[0155]
[0156] Check the property type and perform conversion logic: Now, focus on how to check the type of each property and perform the corresponding conversion logic according to the type. You will write a function convertProperty that receives a property description and the original data value and returns the converted data.
[0157]
[0158]
[0159] Apply conversion logic: Now, you can use the convertProperty function to check the type of each property and perform the corresponding conversion logic based on the type. Assume that you have the following original data:
[0160]
[0161] You can get the transformed data by traversing the attribute description information and applying the transformation logic:
[0162]
[0163] Output
[0164] After running the above code, the convertedData variable will contain the converted data:
[0165]
[0166] In this example, we only focus on how to check the type of each entity attribute and perform corresponding conversion logic according to the type. In this way, we ensure that the data has been correctly converted before entering the entity object, thereby improving the accuracy and reliability of data processing.
[0167] Furthermore, the type conversion logic can be enhanced. On top of the basic type conversion logic, the conversion logic can be enhanced to support more complex data processing requirements. Furthermore, more data types can be supported. In addition to basic string, integer, floating point, Boolean and date types, more data types can be supported, such as enumeration types, JSON objects, UUID, etc. Further, type inference can also be supported. In some cases, the original data may not have an explicitly specified type. At this time, you can try to infer the type based on the content of the data. For example, for a string like '123', it can be automatically inferred to be an integer. In some embodiments of the present application, the three types of entity attributes defined: primitive attributes, entity attributes, and entity array attributes are distinguished by enumerating PropType, specifically:
[0168] The primitive property refers to a property whose value type is a basic data type, and such properties are identified by enumerating the Primitive member in PropType;
[0169] The entity property and the entity array property represent a single entity object and a collection of entity objects respectively, and these two property types are distinguished by enumerating the Entity and EntityArray members in PropType.
[0170] Specifically, primitive property: primitive property refers to a property whose value type is a basic data type. These basic data types usually include but are not limited to: string: used to store text information. Integer: used to store integer values. Float: used to store numerical values with decimal points. Boolean: used to store logical values of true or false. Date: used to store date and time information. The characteristic of primitive properties is that their values can be stored directly in the database without further object reference or collection processing. Such properties are usually used to store direct data, such as name, age, email address, etc. Entity property: an entity property refers to a property whose value type is a single entity object. The entity object itself is also a complete object with its own properties and methods. The characteristic of an entity property is that it refers to a separate object, which can have its own business logic and data. Entity properties are usually used to represent one-to-many or many-to-one relationships. For example, in an order management system, the order entity can have an attribute customer, which is a reference to a customer. In this case, customer is an entity property. Entity ArrayProperty: An entity array property is a property whose value type is a collection of entity objects. This type of property is usually used to represent a many-to-many relationship, or a one-to-many relationship, in which one party needs to store multiple objects of the other party. The characteristic of an entity array property is that it does not store a single object, but a collection of objects. For example, in a blog system, the Article entity can have a property comments, which is an array of Comment objects. In this case, comments is an entity array property. The PropType enumeration is a tool used to distinguish the three types of properties mentioned above. It defines several members to identify different property types: Primitive: identifies primitive properties. Entity: identifies entity properties. EntityArray: identifies entity array properties. By using the PropType enumeration, different types of properties can be clearly distinguished in program design, and different processing logic can be adopted according to the type of property. For example, when creating an entity object, corresponding conversion and validation logic can be performed for different types of properties. By defining primitive properties, entity properties, and entity array properties, and using the PropType enumeration to distinguish them, the following benefits can be achieved: Clear type distinction: Clearly indicate which properties are basic data types, which properties are entity objects, and which properties are collections of entity objects.Unified processing logic: When processing different types of attributes, you can perform specific conversions, validations, or business logic based on enumeration members. Easy to maintain: Using enumerations to identify attribute types can make the code more readable and maintainable. This design approach not only helps organize and manage code, but also improves the readability and maintainability of the system.
[0171] In some embodiments of the present application, the attribute description interface is defined as the parent interface of various attribute description interfaces, specifically: the attribute description interface is defined as the parent interface, so that the attribute description interface contains basic information common to all attributes, so as to facilitate inheritance and extension of primitive attribute, entity attribute and entity array attribute sub-interfaces.
[0172] Specifically, the role of the property description interface, the property description interface (Property DescriptionInterface) is an abstract layer used to describe the basic information of entity properties. Its main purpose is to define the basic information common to all properties, so that different property types (primitive properties, entity properties, entity array properties) can obtain common basic characteristics by inheriting and extending this interface. The information contained in the property description interface, the property description interface should contain the basic information common to all properties. This information may include but is not limited to: Property name (Name): Identifies the name of the property for reference in the entity. Property type (Type): Identifies the specific type of the property, such as primitive type, entity type or entity array type. Required: Identifies whether the property must be provided. Unique: Identifies whether the property value needs to be unique in the entire entity collection. Default value (Default Value): Provides a default value for the property if it is not explicitly set. Description (Description): Provides descriptive information about the purpose of the property. By defining a common attribute description interface and letting different types of attribute sub-interfaces inherit and extend this parent interface, the following advantages can be obtained: Unified interface specification, all attribute types follow the same interface specification, which allows a consistent way to handle different types of attributes. For example, whether the attribute is a primitive type or an entity type, its name and type can be obtained through the same method. Simplified implementation, inheritance and extension can simplify the implementation of specific attribute types. Sub-interfaces can reuse the methods and properties defined in the parent interface, and only need to add or override sub-type-specific features. Easy to maintain, when you need to modify or extend the attribute description, you only need to make changes in one place (that is, the parent interface), and the sub-interface will automatically inherit these changes. This reduces the amount of duplicate code and improves the maintainability of the code. Enhanced flexibility, by defining a common attribute description interface, you don't have to redesign the entire system when you expand new attribute types in the future. New types of attributes can easily inherit existing interfaces and add necessary features and methods.
[0173] In some embodiments of the present application, the primitive attributes are subdivided to ensure that correct conversion and verification can be performed according to the specific primitive type when creating the entity object, specifically:
[0174] Create an enumeration type to list all primitive data types and reference the type in the attribute description interface: when defining the description interface of the primitive attribute, add an attribute valueType, whose value is a member of the PrimitiveValueType enumeration; when creating an entity object, perform corresponding data conversion according to the specific value of valueType.
[0175] Specifically, the purpose of primitive attribute segmentation is that when creating entity objects, primitive attributes (such as strings, integers, floating-point numbers, Boolean values, dates, etc.) need to be correctly converted and verified. This is because the data type of the primitive attribute directly affects the accuracy and validity of the data. By segmenting the primitive attributes, it can be ensured that when creating entity objects, the incoming data is consistent with the data type defined by the entity attributes and complies with business rules. The content of primitive attribute segmentation is that the segmentation of primitive attributes mainly includes the following aspects: Type identification: Identify the specific type of each primitive attribute. For example, an attribute may be a string type (such as name, address), an integer type (such as age, number), or a date type (such as date of birth, registration date), etc. Type conversion: According to the identified type, convert the incoming data into the corresponding type. For example, if the attribute type is an integer, and the incoming number is in the form of a string, the string needs to be converted into an integer. Type verification: After the conversion, it is necessary to verify whether the converted data conforms to the expected format and range. For example, the age attribute should be a positive integer, and the date attribute should be in a legal date format, etc. How to perform segmentation, Type identification: In the design phase of the entity, the type of each attribute needs to be clarified and recorded in the attribute description interface. For example, different primitive types can be distinguished by enumeration or other type identifiers. Type conversion: When creating an entity object, the incoming data is converted accordingly according to the type information recorded in the attribute description interface. For example, if the attribute type is an integer, the incoming string number needs to be converted to an integer. Type verification: After the conversion, the converted data needs to be verified to ensure that it conforms to the expected format and range. For example, for an integer type, it is necessary to check whether the converted value is a valid integer; for a date type, it is necessary to verify whether the converted date is in a legal date format. Benefits of segmentation, By segmenting the original attributes, the following points can be ensured: Data accuracy: Through correct type conversion, it can be ensured that the incoming data conforms to the data type defined by the entity attribute. Data integrity: Through type verification, it can be ensured that the converted data conforms to the expected format and range to avoid data errors. Business logic consistency: By segmenting the original attributes, the consistency of the data processing logic can be ensured when creating the entity object, avoiding business logic problems caused by data type mismatch or data format errors. Easy to maintain: Through segmentation, the attribute type and its validation rules can be clearly recorded in the attribute description interface, which is convenient for later maintenance and expansion.
[0176] In some embodiments of the present application, the definition of the original attribute interface, the entity attribute interface and the entity array attribute interface is used to describe the entity attributes, specifically:
[0177] The definition of the primitive property interface is to define an interface PrimitivePropSchema, which is used to describe properties whose value types are basic data types. This interface is extended from the PropSchema interface and adds a valueType attribute to further subdivide the specific value type;
[0178] The definition of the entity property interface is to define an interface EntityPropSchema, which is used to describe the properties whose value type is another entity object. This interface is also extended from the PropSchema interface and contains an associatedEntitySchema attribute to describe the entity structure associated therewith;
[0179] The definition of the entity array property interface is to define an interface EntityArrayPropSchema, which is used to describe properties whose value type is a collection of entity objects. This interface is also extended from the PropSchema interface and includes a childEntitySchema property to describe the structure of a child entity.
[0180] Specifically, the primitive property interface (Primitive Property Schema), the primitive property interface is used to describe properties whose value types are primitive data types. Such properties usually include primitive data types such as strings, integers, floating-point numbers, Boolean values, dates, etc. The main purpose of defining the primitive property interface is to describe the properties of these primitive data types and provide further subdivisions so that they can be correctly converted and validated according to specific primitive types when creating entity objects. Extended from PropSchema interface: The primitive property interface extends from a general property description interface PropSchema, which means that it inherits all the properties and methods defined in PropSchema. Add valueType attribute: In the PrimitivePropSchema interface, a valueType attribute is added to further subdivide the specific value type. For example, the enumeration PrimitiveValueType can be used to identify different primitive data types such as strings, integers, floating-point numbers, Boolean values, dates, etc. Entity property interface (Entity Property Schema), the entity property interface is used to describe properties whose value type is another entity object. Such attributes are usually used to represent one-to-many or many-to-one relationships, that is, an attribute in an entity references another entity object. The main purpose of defining the entity property interface is to describe those properties that refer to other entity objects and provide a way to describe the structure of the entity associated with them. Extended from PropSchema interface: The entity property interface also extends from PropSchema interface, which means that it inherits all the properties and methods defined in PropSchema. Contains associatedEntitySchema attribute: In the EntityPropSchema interface, an associatedEntitySchema attribute is included to describe the structure of the entity associated with it. This makes it clear which entity the entity property refers to and what the structure of the entity is. Entity Array Property Schema, the entity array property interface is used to describe those properties whose value type is a collection of entity objects. This type of property is usually used to represent a many-to-many relationship, or a one-to-many relationship, where one party needs to store multiple objects of the other party. The main purpose of defining the entity array property interface is to describe these properties that store multiple entity objects and provide a way to describe the structure of the child entity. Extended from PropSchema interface: The entity array property interface also extends from PropSchema interface, which means that it inherits all the properties and methods defined in PropSchema.Contains childEntitySchema attribute: In the EntityArrayPropSchema interface, a childEntitySchema attribute is included to describe the structure of the child entity. This makes it clear which entity each element in the entity array property corresponds to and what the structure of the entity is. Primitive Property Interface (PrimitivePropSchema), PropSchema interface: This is a general property description interface that contains basic information common to all properties, such as property name and type. ValueType attribute: In the primitive property interface, the valueType attribute is added to identify the specific value type, such as string, integer, floating point number, Boolean value, date, etc. Entity Property Interface (EntityPropSchema), PropSchema interface: It is also inherited from the general property description interface. AssociatedEntitySchema attribute: It is used to describe the entity structure associated with the entity property, that is, which entity the entity property refers to and the structure of the entity. Entity Array Property Interface (EntityArrayPropSchema) PropSchema interface: It is inherited from the general property description interface. ChildEntitySchema attribute: It is used to describe the entity structure corresponding to each element in the entity array property, that is, what type of entity each element in the entity array is and the structure of the entity. By defining primitive attribute interface, entity attribute interface and entity array attribute interface, the following goals can be achieved: Unified interface specification: All attribute types follow the same interface specification, which allows a consistent approach to different types of attributes. Refined attribute description: Each interface contains a detailed description of a specific type, such as the specific value type of the primitive attribute, the entity structure associated with the entity attribute, and the sub-entity structure in the entity array attribute. Easy to maintain and expand: By inheriting and extending the general attribute description interface, when expanding new attribute types in the future, there is no need to redesign the entire system, but only to expand on the basis of the existing framework. The design of these interfaces not only helps to organize and manage the code, but also improves the readability and maintainability of the system, while also providing a solid foundation for data conversion and validation when creating entity objects.
[0181] In some embodiments of the present application, the definition of the entity description interface and the entity creator interface is used to describe the structure of the entity and create the entity object, specifically:
[0182] The definition of the entity description interface is to define an interface EntitySchema, which is used to describe the overall structure of the entity. This interface contains a propSchemas array, which contains the description information of all entity properties, and an entityConstructor property, which is used to specify the constructor used when creating an entity object;
[0183] The definition of the entity creator interface is to define an interface IEntityCreator, which is used to describe the method of creating an entity object. The interface includes a create method, which receives entity description information and entity data as input parameters and returns an entity object.
[0184] Specifically, the entity description interface (Entity Schema) is used to describe the overall structure of the entity, including all the attributes of the entity and its type information, and specifies the constructor used when creating an entity object.
[0185] PropSchemas array: This array contains the description information of all entity properties. Each property description information is derived from the PropSchema interface, including PrimitivePropSchema, EntityPropSchema or EntityArrayPropSchema.
[0186] EntityConstructor property: This property is used to specify the constructor used when creating an entity object. The constructor is usually a class constructor in JavaScript, or any factory function that can be used to create an entity object.
[0187] Describe the entity structure: Through the propSchemas array, you can describe the structural information of all attributes in the entity, including attribute name, type, and other related information. Specify the constructor: Through the entityConstructor property, you can specify the specific constructor used to create the entity object, so that you can use this constructor when creating the entity object. Entity Creator interface (Entity Creator), the entity creator interface is used to describe the method of creating entity objects, especially how to create entity objects based on entity description information and entity data. Create method: This method receives entity description information (usually an instance of the EntitySchema interface) and entity data (usually a raw data object), and returns an entity object.
[0188] Create entity objects: Through the create method, you can create entity objects that meet the structural requirements based on the entity description information and the passed in data. Handle different types of data: The create method needs to be able to handle different types of attribute data and perform corresponding conversions and validations.
[0189] Entity Description Interface (EntitySchema), PropSchemas array: This array contains the description information of all entity properties. For example, a user entity may contain properties such as username (string type), age (integer type), date of birth (date type), etc. The description information of these properties will be included in this array. EntityConstructor property: This property specifies the constructor used to create the entity object. For example, if the user entity is created by a class named User, then entityConstructor will point to the constructor of the User class.
[0190] Entity Creator Interface (IEntityCreator), Create method: This method receives two parameters: Entity description information: describes the structure of the entity, including the types of all attributes and constructor information. Entity data: contains the actual data required to create the entity. The create method needs to create an entity object based on the passed in entity description information and entity data. During the creation process, it needs to handle different types of data and perform corresponding conversions and validations.
[0191] In some embodiments of the present application, the type of each entity attribute is checked, and corresponding conversion logic is executed according to the type, specifically:
[0192] Before creating an entity object, you must first obtain the entity description information, which contains the type information of each attribute;
[0193] For each attribute, read the type information from its corresponding description information;
[0194] For the original attribute, perform the corresponding data conversion according to the specific value of valueType;
[0195] For entity attributes, you need to create a new entity object based on the associated entity description information and pass the relevant data to the creation logic of the entity object;
[0196] For entity array attributes, you need to traverse each element in the array and create a child entity object for each element, using the description information of the child entity to perform the conversion.
[0197] Specifically, get the entity description information. Before creating an entity object, you first need to get the entity description information. This information is usually contained in the EntitySchema interface, which includes the description information of all entity attributes and the constructor information required to create an entity object. Read the attribute type information. For each entity attribute, we need to read the type information from its corresponding description information. These description information are usually stored in the propSchemas array of the EntitySchema interface. Each attribute has a corresponding description information object, such as PrimitivePropSchema, EntityPropSchema, or EntityArrayPropSchema. Process primitive attributes. For primitive attributes (such as strings, integers, floating-point numbers, Boolean values, dates, etc.), we need to perform corresponding data conversions based on the specific value of valueType. The specific steps are as follows: Read valueType: Read the valueType attribute from the attribute description information to determine the specific basic data type. Execute conversion logic: Execute the corresponding conversion logic based on the value of valueType. For example, if valueType represents an integer type, the integer in string form passed in needs to be converted to an integer type; if it is a date type, the date in string form passed in needs to be converted to a date object. Processing entity attributes, for entity attributes (i.e. attributes whose value type is another entity object), we need to create a new entity object based on the associated entity description information and pass the relevant data to the creation logic of the entity object. The specific steps are as follows: Read associatedEntitySchema: Read the associatedEntitySchema attribute from the attribute description information to determine the entity structure associated with it. Create entity object: Create a new entity object based on the description information provided by associatedEntitySchema and pass the relevant data to the creation logic of the entity object. For example, if a user entity contains an address entity attribute, you need to create an address entity object first and pass the address data to the creation logic of the address entity. Processing entity array attributes, for entity array attributes (i.e. attributes whose value type is a collection of entity objects), you need to traverse each element in the array and create a child entity object for each element. The specific steps are as follows: Read childEntitySchema: Read the childEntitySchema attribute from the attribute description information to determine the structure of the child entity. Traverse array elements: Traverse the array elements in the passed in data. Create child entity objects: For each element in the array, create a child entity object based on the description information provided by childEntitySchema, and pass the data in the array element to the creation logic of the child entity object.For example, if an Order entity contains an array of order items, you need to create an Order Item entity object for each order item and pass the order item data to the Order Item entity creation logic.
[0198] Example 2
[0199] A computer program product containing instructions, when running on a device, enables the device to execute the steps in a method for converting front-end entity data into entity objects.
[0200] Example 3
[0201] A computer-readable storage medium stores a program, which, when executed by a processor, implements the steps in a method for converting front-end entity data into entity objects.
[0202] Example 4
[0203] An electronic device comprises a memory, a processor and a program stored in the memory and executable on the processor. When the processor executes the program, the steps in the method of converting front-end entity data into entity objects are implemented.
[0204] Anything not described in this application can be achieved by adopting or drawing on existing technologies.
[0205] The various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referenced to each other, and each embodiment focuses on the differences from other embodiments.
[0206] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.
Claims
1. A method for converting front-end entity data into entity objects, characterized in that: include: Define three types of entity attributes: primitive attributes, entity attributes, and entity array attributes, which are distinguished by enumeration PropType; Define the attribute description interface as the parent interface of various attribute description interfaces; Subdividing the primitive attributes to ensure correct conversion and verification according to the specific primitive types when creating entity objects; Define a primitive attribute interface, an entity attribute interface and an entity array attribute interface for describing the entity attributes, define an entity description interface and an entity creator interface for describing the structure of an entity and creating an entity object; The type of each of the entity attributes is checked, and corresponding conversion logic is executed according to the different types.
2. The method according to claim 1, characterized in that The three types of entity attributes defined above: primitive attributes, entity attributes, and entity array attributes are distinguished by enumerating PropType, specifically: The primitive property refers to a property whose value type is a basic data type, and such properties are identified by enumerating the Primitive member in PropType; The entity property and the entity array property represent a single entity object and a collection of entity objects respectively, and these two property types are distinguished by enumerating the Entity and EntityArray members in PropType.
3. The method according to claim 2, characterized in that The defining of the attribute description interface as the parent interface of various attribute description interfaces is specifically as follows: defining the attribute description interface as the parent interface so that the attribute description interface contains basic information common to all attributes, so as to facilitate inheritance and extension of primitive attribute, entity attribute and entity array attribute sub-interfaces.
4. The method according to claim 3, characterized in that The original attributes are subdivided to ensure that they can be correctly converted and verified according to the specific original type when creating the entity object, specifically: Create an enumeration type to list all primitive data types and reference the type in the attribute description interface: when defining the description interface of the primitive attribute, add an attribute valueType, whose value is a member of the PrimitiveValueType enumeration; when creating an entity object, perform corresponding data conversion according to the specific value of valueType.
5. The method according to claim 4, characterized in that The definition of the original attribute interface, entity attribute interface and entity array attribute interface is used to describe the entity attributes, specifically: The definition of the primitive property interface is to define an interface PrimitivePropSchema, which is used to describe properties whose value types are basic data types. This interface is extended from the PropSchema interface and adds a valueType attribute to further subdivide the specific value type; The definition of the entity property interface is to define an interface EntityPropSchema, which is used to describe the properties whose value type is another entity object. This interface is also extended from the PropSchema interface and contains an associatedEntitySchema attribute to describe the entity structure associated therewith; The definition of the entity array property interface is to define an interface EntityArrayPropSchema, which is used to describe properties whose value type is a collection of entity objects. This interface is also extended from the PropSchema interface and includes a childEntitySchema property to describe the structure of a child entity.
6. The method according to claim 5, characterized in that The entity description interface and entity creator interface are defined to describe the structure of the entity and create entity objects, specifically: The definition of the entity description interface is to define an interface EntitySchema, which is used to describe the overall structure of the entity. This interface contains a propSchemas array, which contains the description information of all entity properties, and an entityConstructor property, which is used to specify the constructor used when creating an entity object; The definition of the entity creator interface is to define an interface IEntityCreator, which is used to describe the method of creating an entity object. The interface includes a create method, which receives entity description information and entity data as input parameters and returns an entity object.
7. The method according to claim 6, characterized in that The type of each entity attribute is checked, and corresponding conversion logic is executed according to the type, specifically: Before creating an entity object, you must first obtain the entity description information, which contains the type information of each attribute; For each attribute, read the type information from its corresponding description information; For the original attribute, perform the corresponding data conversion according to the specific value of valueType; For entity attributes, you need to create a new entity object based on the associated entity description information and pass the relevant data to the creation logic of the entity object; For entity array attributes, you need to traverse each element in the array and create a child entity object for each element, using the description information of the child entity to perform the conversion.
8. A computer program product comprising instructions, which, when executed on a device, is characterized in that: The device is enabled to execute the steps in the method for converting front-end entity data into entity objects as described in any one of claims 1 to 7.
9. A computer-readable storage medium having a program stored thereon, characterized in that: When the program is executed by a processor, the steps in the method for converting front-end entity data into entity objects as described in any one of claims 1 to 7 are implemented.
10. An electronic device comprising a memory, a processor, and a program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps in the method for converting front-end entity data into entity objects as described in any one of claims 1-7 are implemented.