|
| 1 | +package com.metaobjects.generator.template; |
| 2 | + |
| 3 | +import com.metaobjects.field.EnumField; |
| 4 | +import com.metaobjects.field.MetaField; |
| 5 | +import com.metaobjects.identity.MetaIdentity; |
| 6 | +import com.metaobjects.generator.util.GeneratorUtil; |
| 7 | +import com.metaobjects.object.MetaObject; |
| 8 | +import com.metaobjects.relationship.MetaRelationship; |
| 9 | +import com.metaobjects.validator.MetaValidator; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.TreeMap; |
| 16 | + |
| 17 | +/** |
| 18 | + * The NEUTRAL, structural codegen template data dict (SP-1 §3.2) for the JVM. |
| 19 | + * Built as {@link LinkedHashMap}/{@link ArrayList} so the JMustache render reads |
| 20 | + * the same keys the TypeScript object exposes — the field names here are a |
| 21 | + * byte-gated cross-port contract (verified against the TS-produced |
| 22 | + * {@code fixtures/template-codegen-conformance/expected/}). Optional keys |
| 23 | + * ({@code maxLength}, {@code enumValues}) are OMITTED when absent so a |
| 24 | + * {@code {{#maxLength}}} section gates identically to TS. |
| 25 | + */ |
| 26 | +public final class TemplateData { |
| 27 | + |
| 28 | + private TemplateData() {} |
| 29 | + |
| 30 | + private static final String SUBTYPE_ENUM = "enum"; |
| 31 | + private static final String VALIDATOR_REQUIRED = "required"; |
| 32 | + |
| 33 | + /** Bare object name — {@code getName()} returns the FQN ({@code shop::Product}), |
| 34 | + * but the dict's {@code name} is the bare leaf ({@code Product}). */ |
| 35 | + public static String bareName(MetaObject o) { |
| 36 | + String n = o.getName(); |
| 37 | + int i = n.lastIndexOf("::"); |
| 38 | + return i >= 0 ? n.substring(i + 2) : n; |
| 39 | + } |
| 40 | + |
| 41 | + /** Effective package — {@code getPackage()} already returns the file package |
| 42 | + * on the JVM (e.g. {@code shop}); null collapses to "". */ |
| 43 | + public static String packageOf(MetaObject o) { |
| 44 | + String p = o.getPackage(); |
| 45 | + return p == null ? "" : p; |
| 46 | + } |
| 47 | + |
| 48 | + public static boolean isConcrete(MetaObject o) { |
| 49 | + return !GeneratorUtil.isAbstract(o); |
| 50 | + } |
| 51 | + |
| 52 | + private static boolean isRequired(MetaField f) { |
| 53 | + if (f.hasMetaAttr(MetaField.ATTR_REQUIRED, false) |
| 54 | + && "true".equals(f.getMetaAttr(MetaField.ATTR_REQUIRED, false).getValueAsString())) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + for (MetaValidator v : f.getChildren(MetaValidator.class)) { |
| 58 | + if (VALIDATOR_REQUIRED.equals(v.getSubType())) return true; |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + private static Map<String, Object> fieldData(MetaField f) { |
| 64 | + Map<String, Object> d = new LinkedHashMap<>(); |
| 65 | + d.put("name", f.getName()); |
| 66 | + d.put("type", f.getSubType()); |
| 67 | + d.put("required", isRequired(f)); |
| 68 | + d.put("isArray", f.isArrayType()); |
| 69 | + if (f.hasMetaAttr(MetaField.ATTR_MAX_LENGTH, false)) { |
| 70 | + d.put("maxLength", Integer.valueOf(f.getMetaAttr(MetaField.ATTR_MAX_LENGTH, false).getValueAsString())); |
| 71 | + } |
| 72 | + if (SUBTYPE_ENUM.equals(f.getSubType()) && f.hasMetaAttr(EnumField.ATTR_VALUES)) { |
| 73 | + Object raw = f.getMetaAttr(EnumField.ATTR_VALUES).getValue(); |
| 74 | + List<String> values = new ArrayList<>(); |
| 75 | + if (raw instanceof List<?> list) { |
| 76 | + for (Object v : list) values.add(String.valueOf(v)); |
| 77 | + } else if (raw instanceof Object[] arr) { |
| 78 | + for (Object v : arr) values.add(String.valueOf(v)); |
| 79 | + } |
| 80 | + d.put("enumValues", values); |
| 81 | + } |
| 82 | + return d; |
| 83 | + } |
| 84 | + |
| 85 | + public static Map<String, Object> entity(MetaObject o) { |
| 86 | + Map<String, Object> d = new LinkedHashMap<>(); |
| 87 | + d.put("name", bareName(o)); |
| 88 | + d.put("package", packageOf(o)); |
| 89 | + |
| 90 | + List<Map<String, Object>> fields = new ArrayList<>(); |
| 91 | + for (MetaField f : o.getMetaFields()) fields.add(fieldData(f)); |
| 92 | + d.put("fields", fields); |
| 93 | + |
| 94 | + List<Map<String, Object>> identities = new ArrayList<>(); |
| 95 | + for (MetaIdentity id : o.getChildren(MetaIdentity.class)) { |
| 96 | + Map<String, Object> i = new LinkedHashMap<>(); |
| 97 | + i.put("kind", id.getSubType()); |
| 98 | + i.put("fields", new ArrayList<>(id.getFields())); |
| 99 | + identities.add(i); |
| 100 | + } |
| 101 | + d.put("identities", identities); |
| 102 | + |
| 103 | + List<Map<String, Object>> relationships = new ArrayList<>(); |
| 104 | + for (MetaRelationship r : o.getChildren(MetaRelationship.class)) { |
| 105 | + Map<String, Object> rel = new LinkedHashMap<>(); |
| 106 | + rel.put("name", r.getName()); |
| 107 | + rel.put("cardinality", r.getCardinality() == null ? "" : r.getCardinality()); |
| 108 | + rel.put("targetRef", r.getObjectRef() == null ? "" : r.getObjectRef()); |
| 109 | + relationships.add(rel); |
| 110 | + } |
| 111 | + d.put("relationships", relationships); |
| 112 | + return d; |
| 113 | + } |
| 114 | + |
| 115 | + public static Map<String, Object> pkg(String p, List<MetaObject> entities) { |
| 116 | + Map<String, Object> d = new LinkedHashMap<>(); |
| 117 | + d.put("package", p); |
| 118 | + List<Map<String, Object>> ents = new ArrayList<>(); |
| 119 | + for (MetaObject o : entities) ents.add(entity(o)); |
| 120 | + d.put("entities", ents); |
| 121 | + return d; |
| 122 | + } |
| 123 | + |
| 124 | + /** Concrete-only, grouped by package ascending, entities in iteration order. */ |
| 125 | + public static Map<String, Object> model(List<MetaObject> objects) { |
| 126 | + Map<String, List<MetaObject>> byPkg = new TreeMap<>(); |
| 127 | + for (MetaObject o : objects) { |
| 128 | + if (!isConcrete(o)) continue; |
| 129 | + byPkg.computeIfAbsent(packageOf(o), k -> new ArrayList<>()).add(o); |
| 130 | + } |
| 131 | + List<Map<String, Object>> packages = new ArrayList<>(); |
| 132 | + for (Map.Entry<String, List<MetaObject>> e : byPkg.entrySet()) { |
| 133 | + packages.add(pkg(e.getKey(), e.getValue())); |
| 134 | + } |
| 135 | + Map<String, Object> d = new LinkedHashMap<>(); |
| 136 | + d.put("packages", packages); |
| 137 | + return d; |
| 138 | + } |
| 139 | +} |
0 commit comments