Skip to content

Commit

Permalink
Add custom field value resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dilanSachi committed Nov 4, 2022
1 parent 94324f6 commit 034e9b4
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.AccessibleObject;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -50,8 +51,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.ballerinalang.openapi.model.GenSrcFile.GenFileType;
import static org.ballerinalang.openapi.utils.GeneratorConstants.GenType.GEN_CLIENT;
Expand Down Expand Up @@ -414,8 +417,30 @@ private List<GenSrcFile> generateBallerinaService(BallerinaOpenApiType api) thro
*/
private String getContent(BallerinaOpenApiType object, String templateDir, String templateName) throws IOException {
Template template = compileTemplate(templateDir, templateName);
FieldValueResolver fieldValueResolver = new FieldValueResolver() {

@Override
protected Set<FieldWrapper> members(
Class<?> clazz) {
Set members = super.members(clazz);
return (Set<FieldWrapper>) members.stream()
.filter(fw -> isValidField((FieldWrapper) fw))
.collect(Collectors.toSet());
}

boolean isValidField(
FieldWrapper fw) {
if (fw instanceof AccessibleObject) {
if (isUseSetAccessible(fw)) {
return true;
}
return false;
}
return true;
}
};
Context context = Context.newBuilder(object)
.resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, FieldValueResolver.INSTANCE)
.resolver(MapValueResolver.INSTANCE, JavaBeanValueResolver.INSTANCE, fieldValueResolver)
.build();
return template.apply(context);
}
Expand Down

0 comments on commit 034e9b4

Please sign in to comment.