|
| 1 | +package io.dinject.javalin.generator; |
| 2 | + |
| 3 | +import io.dinject.controller.BeanParam; |
| 4 | +import io.dinject.controller.Cookie; |
| 5 | +import io.dinject.controller.Default; |
| 6 | +import io.dinject.controller.Form; |
| 7 | +import io.dinject.controller.FormParam; |
| 8 | +import io.dinject.controller.Header; |
| 9 | +import io.dinject.controller.QueryParam; |
| 10 | + |
| 11 | +import javax.lang.model.element.Element; |
| 12 | +import javax.lang.model.element.TypeElement; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +class ElementReader { |
| 17 | + |
| 18 | + private final ProcessingContext ctx; |
| 19 | + private final String rawType; |
| 20 | + private final TypeHandler typeHandler; |
| 21 | + private final String varName; |
| 22 | + private final String snakeName; |
| 23 | + |
| 24 | + private String paramName; |
| 25 | + private ParamType paramType; |
| 26 | + private String paramDefault; |
| 27 | + private String docComment; |
| 28 | + |
| 29 | + ElementReader(Element element, ProcessingContext ctx, ParamType defaultType) { |
| 30 | + this.ctx = ctx; |
| 31 | + this.rawType = element.asType().toString(); |
| 32 | + this.typeHandler = TypeMap.get(rawType); |
| 33 | + |
| 34 | + this.varName = element.getSimpleName().toString(); |
| 35 | + this.snakeName = Util.snakeCase(varName); |
| 36 | + this.paramName = varName; |
| 37 | + this.docComment = ctx.docComment(element); |
| 38 | + |
| 39 | + readAnnotations(element, defaultType); |
| 40 | + } |
| 41 | + |
| 42 | + private void readAnnotations(Element element, ParamType defaultType) { |
| 43 | + Default defaultVal = element.getAnnotation(Default.class); |
| 44 | + if (defaultVal != null) { |
| 45 | + this.paramDefault = defaultVal.value(); |
| 46 | + } |
| 47 | + Form form = element.getAnnotation(Form.class); |
| 48 | + if (form != null) { |
| 49 | + this.paramType = ParamType.FORM; |
| 50 | + return; |
| 51 | + } |
| 52 | + BeanParam beanParam = element.getAnnotation(BeanParam.class); |
| 53 | + if (beanParam != null) { |
| 54 | + this.paramType = ParamType.BEANPARAM; |
| 55 | + return; |
| 56 | + } |
| 57 | + QueryParam queryParam = element.getAnnotation(QueryParam.class); |
| 58 | + if (queryParam != null) { |
| 59 | + this.paramName = nameFrom(queryParam.value(), varName); |
| 60 | + this.paramType = ParamType.QUERYPARAM; |
| 61 | + return; |
| 62 | + } |
| 63 | + FormParam formParam = element.getAnnotation(FormParam.class); |
| 64 | + if (formParam != null) { |
| 65 | + this.paramName = nameFrom(formParam.value(), varName); |
| 66 | + this.paramType = ParamType.FORMPARAM; |
| 67 | + return; |
| 68 | + } |
| 69 | + Cookie cookieParam = element.getAnnotation(Cookie.class); |
| 70 | + if (cookieParam != null) { |
| 71 | + this.paramName = nameFrom(cookieParam.value(), varName); |
| 72 | + this.paramType = ParamType.COOKIE; |
| 73 | + this.paramDefault = null; |
| 74 | + return; |
| 75 | + } |
| 76 | + Header headerParam = element.getAnnotation(Header.class); |
| 77 | + if (headerParam != null) { |
| 78 | + this.paramName = nameFrom(headerParam.value(), Util.initcapSnake(snakeName)); |
| 79 | + this.paramType = ParamType.HEADER; |
| 80 | + this.paramDefault = null; |
| 81 | + return; |
| 82 | + } |
| 83 | + if (paramType == null) { |
| 84 | + this.paramType = defaultType; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString() { |
| 90 | + return varName + " type:" + rawType + " paramType:" + paramType + " dft:" + paramDefault; |
| 91 | + } |
| 92 | + |
| 93 | + private String nameFrom(String name, String defaultName) { |
| 94 | + if (name != null && !name.isEmpty()) { |
| 95 | + return name; |
| 96 | + } |
| 97 | + return defaultName; |
| 98 | + } |
| 99 | + |
| 100 | + String getVarName() { |
| 101 | + return varName; |
| 102 | + } |
| 103 | + |
| 104 | + private boolean hasParamDefault() { |
| 105 | + return paramDefault != null && !paramDefault.isEmpty(); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean isJavalinContext() { |
| 109 | + return Constants.JAVALIN_CONTEXT.equals(rawType); |
| 110 | + } |
| 111 | + |
| 112 | + private String shortType() { |
| 113 | + if (typeHandler != null) { |
| 114 | + return typeHandler.shortName(); |
| 115 | + } else { |
| 116 | + return Util.shortName(rawType); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private String derivePathParam(Set<String> pathParams) { |
| 121 | + if (pathParams.contains(varName)) { |
| 122 | + return varName; |
| 123 | + } |
| 124 | + if (pathParams.contains(snakeName)) { |
| 125 | + return snakeName; |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + void addImports(BeanReader bean) { |
| 131 | + if (typeHandler != null) { |
| 132 | + String importType = typeHandler.getImportType(); |
| 133 | + if (importType != null) { |
| 134 | + bean.addImportType(rawType); |
| 135 | + } |
| 136 | + } else { |
| 137 | + bean.addImportType(rawType); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + void writeParamName(Append writer) { |
| 142 | + if (isJavalinContext()) { |
| 143 | + writer.append("ctx"); |
| 144 | + } else { |
| 145 | + writer.append(varName); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + void writeCtxGet(Append writer, Set<String> pathParams) { |
| 150 | + |
| 151 | + if (isJavalinContext()) { |
| 152 | + // no conversion for this parameter |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + String shortType = shortType(); |
| 157 | + writer.append(" %s %s = ", shortType, varName); |
| 158 | + if (setValue(writer, pathParams, shortType)) { |
| 159 | + writer.append(";").eol(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + void setValue(Append writer) { |
| 164 | + String shortType = shortType(); |
| 165 | + setValue(writer, Collections.emptySet(), shortType); |
| 166 | + } |
| 167 | + |
| 168 | + private boolean setValue(Append writer, Set<String> pathParams, String shortType) { |
| 169 | + |
| 170 | + if (ParamType.FORM == paramType) { |
| 171 | + writeForm(writer, shortType, varName, ParamType.FORMPARAM); |
| 172 | + return false; |
| 173 | + } |
| 174 | + if (ParamType.BEANPARAM == paramType) { |
| 175 | + writeForm(writer, shortType, varName, ParamType.QUERYPARAM); |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + // path parameters are expected to be not nullable |
| 180 | + // ... with query parameters nullable |
| 181 | + String pathParameter = derivePathParam(pathParams); |
| 182 | + String asMethod = (typeHandler == null) ? null : (pathParameter != null) ? typeHandler.asMethod() : typeHandler.toMethod(); |
| 183 | + |
| 184 | + if (asMethod != null) { |
| 185 | + writer.append(asMethod); |
| 186 | + } |
| 187 | + if (pathParameter != null) { |
| 188 | + writer.append("ctx.pathParam(\"%s\")", pathParameter); |
| 189 | + } else { |
| 190 | + if (typeHandler == null) { |
| 191 | + // assuming this is a body (POST, PATCH) |
| 192 | + writer.append("ctx.bodyAsClass(%s.class)", shortType); |
| 193 | + } else { |
| 194 | + if (hasParamDefault()) { |
| 195 | + writer.append("ctx.%s(\"%s\",\"%s\")", paramType, paramName, paramDefault); |
| 196 | + } else { |
| 197 | + writer.append("ctx.%s(\"%s\")", paramType, paramName); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (asMethod != null) { |
| 203 | + writer.append(")"); |
| 204 | + } |
| 205 | + return true; |
| 206 | + } |
| 207 | + |
| 208 | + private void writeForm(Append writer, String shortType, String varName, ParamType defaultParamType) { |
| 209 | + |
| 210 | + TypeElement formBeanType = ctx.getTypeElement(rawType); |
| 211 | + |
| 212 | + FormBeanReader form = new FormBeanReader(ctx, formBeanType, varName, shortType, defaultParamType); |
| 213 | + form.write(writer); |
| 214 | + } |
| 215 | +} |
0 commit comments