ptupitsyn commented on a change in pull request #455:
URL: https://github.com/apache/ignite-3/pull/455#discussion_r768576778



##########
File path: 
modules/api/src/main/java/org/apache/ignite/table/mapper/MapperBuilder.java
##########
@@ -37,93 +50,205 @@
     /** Column-to-field name mapping. */
     private Map<String, String> columnToFields;
 
+    /** Column converters. */
+    private Map<String, TypeConverter<?, ?>> columnConverters = new 
HashMap<>();
+
+    /** Column name for one-column mapping. */
+    private final String mappedToColumn;
+
+    /** Flag indicates, if all unmapped object fields should be mapped to the 
columns with same names automatically. */
+    private boolean automapFlag = false;
+
+    /** {@code True} if the {@link #build()} method was called, {@code false} 
otherwise. */
+    private boolean isStale;
+
     /**
-     * Creates a mapper builder for a type.
+     * Creates a mapper builder for a POJO type.
      *
      * @param targetType Target type.
      */
     MapperBuilder(@NotNull Class<T> targetType) {
-        this.targetType = targetType;
+        this.targetType = Mapper.ensureValidPojo(targetType);
 
+        mappedToColumn = null;
         columnToFields = new HashMap<>(targetType.getDeclaredFields().length);
     }
 
     /**
-     * Add mapping for a field to a column.
+     * Creates a mapper builder for a natively supported type.
+     *
+     * @param targetType   Target type.
+     * @param mappedColumn Column name to map to.
+     */
+    MapperBuilder(@NotNull Class<T> targetType, String mappedColumn) {
+        this.targetType = Mapper.ensureNativelySupported(targetType);
+
+        mappedToColumn = mappedColumn;
+        columnToFields = null;
+    }
+
+    /**
+     * Ensure this instance is valid.
      *
-     * @param fieldName  Field name.
-     * @param columnName Column name.
-     * @return {@code this} for chaining.
-     * @throws IllegalArgumentException if a column was already mapped to some 
field.
      * @throws IllegalStateException if tries to reuse the builder after a 
mapping has been built.
      */
-    public MapperBuilder<T> map(@NotNull String fieldName, @NotNull String 
columnName) {
-        if (columnToFields == null) {
+    private void ensureNotStale() {
+        if (isStale) {
             throw new IllegalStateException("Mapper builder can't be reused.");
         }
+    }
+
+    /**
+     * Ensure field name is valid and field with this name exists.
+     *
+     * @param fieldName Field name.
+     * @return Field name for chaining.
+     * @throws IllegalArgumentException If field is {@code null} or class has 
no declared field with given name.
+     */
+    private String requireValidField(String fieldName) {
+        try {
+            if (fieldName == null || targetType.getDeclaredField(fieldName) == 
null) {
+                throw new IllegalArgumentException("Mapping for a column 
already exists: " + fieldName);
+            }
+        } catch (NoSuchFieldException e) {
+            throw new IllegalArgumentException(
+                    String.format("Field not found for class: field=%s, 
class=%s", fieldName, targetType.getName()));
+        }
+
+        return fieldName;
+    }
+
+    /**
+     * Maps a field to a column.
+     *
+     * @param fieldName        Field name.
+     * @param columnName       Column name.
+     * @param fieldColumnPairs Vararg that accepts (fieldName, columnName) 
pairs.
+     * @return {@code this} for chaining.
+     * @throws IllegalArgumentException If a field name has not paired column 
name in {@code fieldColumnPairs}, or a column was already
+     *                                  mapped to another field.
+     * @throws IllegalStateException    if tries to reuse the builder after a 
mapping has been built.
+     */
+    public MapperBuilder<T> map(@NotNull String fieldName, @NotNull String 
columnName, String... fieldColumnPairs) {
+        ensureNotStale();
 
-        if (columnToFields.put(Objects.requireNonNull(columnName), 
Objects.requireNonNull(fieldName)) != null) {
+        if (columnToFields == null) {
+            throw new IllegalArgumentException("Natively supported types 
doesn't support field mapping.");
+        } else if (fieldColumnPairs.length % 2 != 0) {
+            throw new IllegalArgumentException("Missed a column name, which 
the field is mapped to.");
+        } else if (columnToFields.put(Objects.requireNonNull(columnName), 
requireValidField(fieldName)) != null) {
             throw new IllegalArgumentException("Mapping for a column already 
exists: " + columnName);
         }
 
+        for (int i = 0; i < fieldColumnPairs.length; i += 2) {
+            if (columnToFields.put(Objects.requireNonNull(fieldColumnPairs[i + 
1]), requireValidField(fieldColumnPairs[i])) != null) {
+                throw new IllegalArgumentException("Mapping for a column 
already exists: " + columnName);

Review comment:
       Ok.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to