Edouard DUPIN 09cfcfc578 [FEAT] generate a full Zod object for write mode.
- Add @NoWriteSpecificMode to permit to remove specific object write model
  - refactor Zod Write model
  - Add .nullable() in write Optional element

Residual bug element use in APi that is mark as no write
2024-06-08 11:42:38 +02:00

68 lines
1.6 KiB
Java

package org.kar.archidata.externalRestApi.model;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class ClassEnumModel extends ClassModel {
protected ClassEnumModel(final Class<?> clazz) {
this.originClasses = clazz;
this.noWriteSpecificMode = true;
}
@Override
public String toString() {
final StringBuilder out = new StringBuilder();
out.append("ClassEnumModel [");
out.append(this.originClasses.getCanonicalName());
out.append("]");
return out.toString();
}
final Map<String, Object> listOfValues = new HashMap<>();
@Override
public void analyze(final ModelGroup group) throws IOException {
if (this.analyzeDone) {
return;
}
this.analyzeDone = true;
final Class<?> clazz = this.originClasses;
final Object[] constants = clazz.getEnumConstants();
// Try to get a get Value element to serialize:
try {
final Method getValueMethod = clazz.getMethod("getValue");
for (final Object constant : constants) {
final String name = constant.toString();
final Object value = getValueMethod.invoke(constant);
this.listOfValues.put(name, value);
}
return;
} catch (final Exception e) {
//e.printStackTrace();
}
for (final Object elem : constants) {
this.listOfValues.put(elem.toString(), elem.toString());
}
}
public Map<String, Object> getListOfValues() {
return this.listOfValues;
}
@Override
public Set<ClassModel> getAlls() {
return Set.of(this);
}
@Override
public Set<ClassModel> getDependencyGroupModels() {
return Set.of(this);
}
}