Created
August 9, 2020 05:50
-
-
Save arahansa/c62e3c750773d5dd75abd2051e326917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [숫자][문자열]자바설명 (필수) (3~50글자) | |
*/ | |
static class SilmuconDescriptor { | |
private boolean isRequired; | |
private TypeDescriptor typeDescriptor; | |
private LengthDescriptor lengthDescriptor; | |
private String comments; | |
private String name; | |
public SilmuconDescriptor(String name, boolean isRequired, TypeDescriptor typeDescriptor, LengthDescriptor lengthDescriptor, String comments) { | |
this.name = name; | |
this.isRequired = isRequired; | |
this.typeDescriptor = typeDescriptor; | |
this.lengthDescriptor = lengthDescriptor; | |
this.comments = comments; | |
} | |
public FieldDescriptor toRestDocsFields(){ | |
String required = isRequired ? " [필수] " :" [선택] "; | |
String lengthDescription = lengthDescriptor == null ? "" : lengthDescriptor.toString(); | |
FieldDescriptor description = fieldWithPath(name).description(this.typeDescriptor + comments + required + lengthDescription); | |
if(!isRequired){ | |
description = description.optional(); | |
} | |
return description; | |
} | |
@Override | |
public String toString() { | |
return "FieldDescriptor{" + | |
"isRequired=" + isRequired + | |
", typeDescriptor=" + typeDescriptor + | |
", lengthDescriptor=" + lengthDescriptor + | |
", comments='" + comments + '\'' + | |
'}'; | |
} | |
} | |
static class TypeDescriptor{ | |
private JsonFieldType type; | |
public TypeDescriptor(Class<?> type) { | |
if(type.isAssignableFrom(Integer.class) || type.isAssignableFrom(Long.class)){ | |
this.type = JsonFieldType.NUMBER; | |
}else if(type.isAssignableFrom(String.class)){ | |
this.type = JsonFieldType.STRING; | |
}else if(type.isAssignableFrom(Map.class)){ | |
this.type = JsonFieldType.OBJECT; | |
}else if(type.isAssignableFrom(List.class)){ | |
this.type = JsonFieldType.ARRAY; | |
}else if(type.isAssignableFrom(Boolean.class)){ | |
this.type = JsonFieldType.BOOLEAN; | |
}else{ | |
this.type = JsonFieldType.OBJECT; | |
} | |
} | |
public String toString(){ | |
switch (this.type){ | |
case NUMBER : return "(숫자) "; | |
case STRING : return "(문자열) "; | |
case ARRAY : return "(배열) "; | |
case BOOLEAN : return "(참/거짓) "; | |
default : return "(객체) "; | |
} | |
} | |
} | |
static class LengthDescriptor { | |
private int min; | |
private int max; | |
public LengthDescriptor(Length length) { | |
this.min = length.min(); | |
this.max = length.max(); | |
} | |
@Override | |
public String toString() { | |
return "최소 " + min + " 글자, 최대 " + max + " 글자"; | |
} | |
} | |
public static class FieldDiscriptorHolder { | |
final private List<FieldDescriptor> descriptors; | |
public FieldDiscriptorHolder(List<FieldDescriptor> descriptors) { | |
this.descriptors = new ArrayList<>(descriptors); | |
} | |
public FieldDiscriptorHolder paged(){ | |
descriptors.addAll(new ArrayList<>(Arrays.asList(fieldWithPath("pageable.sort").type(JsonFieldType.OBJECT).description("페이징 정렬 객체"), | |
fieldWithPath("pageable.sort.sorted").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("pageable.sort.unsorted").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("pageable.sort.empty").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("pageable.offset").type(JsonFieldType.NUMBER).description(""), | |
fieldWithPath("pageable.pageNumber").type(JsonFieldType.NUMBER).description(""), | |
fieldWithPath("pageable.pageSize").type(JsonFieldType.NUMBER).description(""), | |
fieldWithPath("pageable.paged").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("pageable.unpaged").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("totalPages").type(JsonFieldType.NUMBER).description("총 페이지"), | |
fieldWithPath("last").type(JsonFieldType.BOOLEAN).description("마지막 인지"), | |
fieldWithPath("totalElements").type(JsonFieldType.NUMBER).description("총 요소 갯수"), | |
fieldWithPath("first").type(JsonFieldType.BOOLEAN).description("첫번째인지"), | |
fieldWithPath("size").type(JsonFieldType.NUMBER).description("페이지당 요소 갯수"), | |
fieldWithPath("number").type(JsonFieldType.NUMBER).description("페이지 숫자"), | |
fieldWithPath("sort").type(JsonFieldType.OBJECT).description("정렬 객체"), | |
fieldWithPath("sort.sorted").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("sort.unsorted").type(JsonFieldType.BOOLEAN).description(""), | |
fieldWithPath("sort.empty").type(JsonFieldType.BOOLEAN).description("?"), | |
fieldWithPath("numberOfElements").type(JsonFieldType.NUMBER).description("마지막인지"), | |
fieldWithPath("empty").type(JsonFieldType.BOOLEAN).description("비어있는지")))); | |
return this; | |
} | |
public FieldDiscriptorHolder add(FieldDescriptor descriptor){ | |
this.descriptors.add(descriptor); | |
return this; | |
} | |
public List<FieldDescriptor> getDescriptors(){ | |
return descriptors; | |
} | |
public RequestFieldsSnippet getReq(){ | |
return requestFields(getDescriptors()); | |
} | |
public ResponseFieldsSnippet getRes(){ | |
return responseFields(getDescriptors()); | |
} | |
} | |
/** | |
* 1. 자바독 설명 (자바독 설명이 없어도 문자열을 추가한다.) | |
* 2. 어노테이션 NotBlank, NotNull 이 있을 시 (필수)로 입력, 없을 시는 (선택) | |
* 3. 숫자타입 Long, int, Double, Float 인 경우 숫자로 처리 | |
* 4. Length 가 있을 경우 추가 글자 처리. 완료된 문자열은 다음과 같다. | |
* | |
* [숫자][문자열]자바설명 (필수) (3~50글자) | |
* @param clazz | |
* @throws IOException | |
* @throws NoSuchFieldException | |
*/ | |
public static FieldDiscriptorHolder transFieldDiscriptorHolder(Class clazz) { | |
Map<String, String> docuMap = new HashMap<>(); | |
ClassJavadoc classDoc = RuntimeJavadoc.getJavadoc(clazz.getName()); | |
for (FieldJavadoc fieldJavadoc : classDoc.getFields()) { | |
docuMap.put(fieldJavadoc.getName(), fieldJavadoc.getComment().toString()); | |
} | |
List<FieldDescriptor> fieldDescriptors = new ArrayList<>(); | |
for (Field field : clazz.getDeclaredFields()) { | |
field.setAccessible(true); | |
String name = field.getName(); | |
boolean isRequired = false; | |
LengthDescriptor lengthDescriptor = null; | |
TypeDescriptor typeDescriptor = new TypeDescriptor(field.getType()); | |
for (Annotation anno : field.getAnnotations()) { | |
if (anno.annotationType().isAssignableFrom(NotNull.class) || | |
anno.annotationType().isAssignableFrom(NotBlank.class)) { | |
isRequired = true; | |
} | |
if (anno.annotationType().isAssignableFrom(Length.class)) { | |
lengthDescriptor = new LengthDescriptor((Length) anno); | |
} | |
} | |
String comment = docuMap.get(name); | |
comment = comment == null ? "" : comment; | |
SilmuconDescriptor silmuconFieldDescriptor = new SilmuconDescriptor(name, isRequired, typeDescriptor, lengthDescriptor, comment); | |
fieldDescriptors.add(silmuconFieldDescriptor.toRestDocsFields()); | |
} | |
return new FieldDiscriptorHolder(fieldDescriptors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment