roryqi commented on code in PR #12162:
URL: https://github.com/apache/gravitino/pull/12162#discussion_r3655201617


##########
api/src/main/java/org/apache/gravitino/tag/SupportsTags.java:
##########
@@ -54,17 +54,30 @@ public interface SupportsTags {
   Tag getTag(String name) throws NoSuchTagException;
 
   /**
-   * Associate tags to the specific object. The tagsToAdd will be added to the 
object, and the
-   * tagsToRemove will be removed from the object. Note that: 1) Adding or 
removing tags that are
-   * not existed will be ignored. 2) If the same name tag is in both tagsToAdd 
and tagsToRemove, it
-   * will be ignored. 3) If the tag is already associated with the object, it 
will throw {@link
-   * TagAlreadyAssociatedException}
+   * Associate valueless tags to the specific object. The tagsToAdd will be 
added to the object, and
+   * the tagsToRemove will be removed from the object. Missing tags are 
ignored. If the same tag is
+   * in both tagsToAdd and tagsToRemove, it will be ignored. Repeated existing 
assignments are
+   * idempotent.

Review Comment:
   This Javadoc describes the new API contract. The follow-up storage/server 
implementation skips already-active assignment values before insert, so 
repeated assignments are idempotent. The existing String[] API is converted to 
valueless TagValue and follows the same path.



##########
api/src/main/java/org/apache/gravitino/tag/Tag.java:
##########
@@ -54,6 +54,25 @@ public interface Tag extends Auditable {
    */
   Map<String, String> properties();
 
+  /**
+   * @return The assignment value constraint of the tag.
+   */
+  default TagValueConstraint valueConstraint() {
+    return TagValueConstraint.anyValue();
+  }

Review Comment:
   The default is intentional. `ANY_VALUE` means no assignment-value 
restriction, so it allows both valueless assignments and valued assignments. It 
does not mean the tag requires a value. I clarified the Javadocs to make this 
explicit.



##########
api/src/main/java/org/apache/gravitino/tag/TagValueConstraint.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.tag;
+
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import java.util.Objects;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.annotation.Evolving;
+
+/** Describes what assignment values a tag accepts. */
+@Evolving
+public final class TagValueConstraint {
+
+  private static final TagValueConstraint ANY_VALUE =
+      new TagValueConstraint(Type.ANY_VALUE, new String[0]);
+
+  private static final TagValueConstraint WITHOUT_VALUES =
+      new TagValueConstraint(Type.WITHOUT_VALUES, new String[0]);
+
+  private final Type type;
+  private final String[] allowedValues;
+
+  private TagValueConstraint(Type type, String[] allowedValues) {
+    this.type = type;
+    this.allowedValues = allowedValues.clone();
+  }
+
+  /** The value constraint type. */
+  public enum Type {
+    /** The tag accepts any non-empty assignment value. */
+    ANY_VALUE,
+
+    /** The tag only accepts assignments without values. */
+    WITHOUT_VALUES,
+
+    /** The tag only accepts values from the allowed value list. */
+    ALLOWED_VALUES
+  }
+
+  /**
+   * Creates a constraint that accepts any non-empty assignment value.
+   *
+   * @return The value constraint.
+   */
+  public static TagValueConstraint anyValue() {
+    return ANY_VALUE;
+  }
+
+  /**
+   * Creates a constraint that only accepts assignments without values.
+   *
+   * @return The value constraint.
+   */
+  public static TagValueConstraint withoutValues() {
+    return WITHOUT_VALUES;
+  }
+
+  /**
+   * Creates a constraint that only accepts values from the allowed value list.
+   *
+   * @param allowedValues The allowed assignment values.
+   * @return The value constraint.
+   */
+  public static TagValueConstraint ofAllowedValues(String... allowedValues) {

Review Comment:
   I prefer keeping `ofAllowedValues` here because this is a static factory for 
an immutable value object, not a builder-style setter. `withAllowedValues` 
matches the DTO/PO builder pattern better, while `ofAllowedValues` is 
consistent with `TagValue.of(...)` and `TagAssignment.ofValues(...)`.



-- 
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