1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.model;
7   
8   import edu.umd.cs.findbugs.annotations.NonNull;
9   import edu.umd.cs.findbugs.annotations.Nullable;
10  
11  public interface IFieldDefinition extends IModelDefinition, IValuedDefinition, IField {
12    @Override
13    default IFieldInstance getInlineInstance() {
14      // not inline by default
15      return null;
16    }
17  
18    /**
19     * Retrieves the key to use as the field name for this field's value in JSON.
20     *
21     * @return a string or a FlagInstance value
22     */
23    @Nullable
24    default Object getJsonValueKey() {
25      Object retval = getJsonValueKeyFlagInstance();
26      if (retval == null) {
27        retval = getEffectiveJsonValueKeyName();
28      }
29      return retval;
30    }
31  
32    /**
33     * Check if a JSON value key flag is configured.
34     *
35     * @return {@code true} if a JSON value key flag is configured, or {@code false}
36     *         otherwise
37     */
38    default boolean hasJsonValueKeyFlagInstance() {
39      return getJsonValueKeyFlagInstance() != null;
40    }
41  
42    /**
43     * Retrieves the flag instance who's value will be used as the "value key".
44     *
45     * @return the configured flag instance, or {@code null} if a flag is not
46     *         configured as the "value key"
47     */
48    @Nullable
49    IFlagInstance getJsonValueKeyFlagInstance();
50  
51    /**
52     * Retrieves the configured static label to use as the value key, or the type
53     * specific name if a label is not configured.
54     *
55     * @return the value key label
56     */
57    @Nullable
58    String getJsonValueKeyName();
59  
60    /**
61     * Retrieves the configured static label to use as the value key, or the type
62     * specific name if a label is not configured.
63     *
64     * @return the value key label
65     */
66    @NonNull
67    default String getEffectiveJsonValueKeyName() {
68      String retval = getJsonValueKeyName();
69      if (retval == null || retval.isEmpty()) {
70        retval = getJavaTypeAdapter().getDefaultJsonValueKey();
71      }
72      return retval;
73    }
74  
75    /**
76     * Get the value of the field's value from the field item object.
77     *
78     * @param item
79     *          the field item
80     * @return the field's value or {@code null} if it has no value
81     */
82    default Object getFieldValue(@NonNull Object item) {
83      // no value by default
84      return null;
85    }
86  }