1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.codegen.typeinfo;
7   
8   import com.squareup.javapoet.AnnotationSpec;
9   
10  import dev.metaschema.core.datatype.markup.MarkupLine;
11  import dev.metaschema.core.datatype.markup.MarkupMultiline;
12  import dev.metaschema.core.model.INamedModelInstance;
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  
15  public final class TypeInfoUtils {
16    private TypeInfoUtils() {
17      // disable construction
18    }
19  
20    /**
21     * Convert the first character of a string to lowercase.
22     *
23     * <p>
24     * This is useful for forming Javadoc sentences where a description needs to
25     * flow naturally after a prefix like "Get the" or "Set the".
26     *
27     * @param text
28     *          the text to convert
29     * @return the text with the first character lowercased, or the original text if
30     *         empty or already lowercase
31     */
32    @NonNull
33    public static String toLowerFirstChar(@NonNull String text) {
34      if (text.isEmpty() || Character.isLowerCase(text.charAt(0))) {
35        return text;
36      }
37      return Character.toLowerCase(text.charAt(0)) + text.substring(1);
38    }
39  
40    /**
41     * Builds common binding annotation values for a named model instance.
42     * <p>
43     * This method populates the annotation builder with common attributes such as
44     * formal name, description, use name, use index, and remarks.
45     *
46     * @param instance
47     *          the named model instance to extract values from
48     * @param annotation
49     *          the annotation builder to populate with values
50     */
51    public static void buildCommonBindingAnnotationValues(
52        @NonNull INamedModelInstance instance,
53        @NonNull AnnotationSpec.Builder annotation) {
54  
55      String formalName = instance.getEffectiveFormalName();
56      if (formalName != null) {
57        annotation.addMember("formalName", "$S", formalName);
58      }
59  
60      MarkupLine description = instance.getEffectiveDescription();
61      if (description != null) {
62        annotation.addMember("description", "$S", description.toMarkdown());
63      }
64  
65      annotation.addMember("useName", "$S", instance.getEffectiveName());
66  
67      Integer index = instance.getEffectiveIndex();
68      if (index != null) {
69        annotation.addMember("useIndex", "$L", index);
70      }
71  
72      // TODO: handle instance namespace as a prefix
73  
74      MarkupMultiline remarks = instance.getRemarks();
75      if (remarks != null) {
76        annotation.addMember("remarks", "$S", remarks.toMarkdown());
77      }
78    }
79  }