Skip to content

πŸš€ 4단계 - Controller λ©”μ„œλ“œ 인자 λ§€ν•‘ #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: woo-yu
Choose a base branch
from

Conversation

woo-yu
Copy link

@woo-yu woo-yu commented Aug 19, 2024

μ•ˆλ…•ν•˜μ„Έμš” μ •μ™„λ‹˜!
리뷰 μš”μ²­μ΄ 많이 λŠ¦μ–΄μ Έμ„œ μ£„μ†‘ν•©λ‹ˆλ‹€ πŸ™‡β€β™€οΈ

λ§ˆμ§€λ§‰ μŠ€ν…λ„ 잘 λΆ€νƒλ“œλ¦¬κ² μŠ΅λ‹ˆλ‹€!

Copy link
Member

@ghojeong ghojeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ˜€λžœλ§Œμž…λ‹ˆλ‹€ μš°ν˜œμ§„λ‹˜!
λκΉŒμ§€ ν¬κΈ°ν•˜μ§€ μ•Šκ³  계속 μ§„ν–‰ν•΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.
λ§ˆμ§€λ§‰ λ‹¨κ³„λΌμ„œ μ„Έμ„Έν•˜κ²Œ νŒŒκ³ λ“  ν”Όλ“œλ°±μ„ λ‚¨κ²¨λ³΄μ•˜μŠ΅λ‹ˆλ‹€.
λ§Œμ•½ ν”Όλ“œλ°±μ„ λ°˜μ˜ν•˜κΈ°μ— μ§€λ‚˜μΉ˜κ²Œ 양이 λ§Žλ‹€ μ‹ΆμœΌλ©΄ κ³Όκ°ν•˜κ²Œ μ—¬κΈ°κΉŒμ§€ λ―Έμ…˜ μ§„ν–‰ν•˜κ² λ‹€κ³  λ§μ”€μ£Όμ„Έμš”!

Comment on lines +5 to +6
public class PathPatternUtil {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class PathPatternUtil {
public final class PathPatternUtil {
private PathPatternUtil() {}

μ €λŠ” μœ ν‹Έμ„± 클래슀의 경우 μΈμŠ€ν„΄μŠ€μ˜ 생성이 λΆˆκ°€λŠ₯ν•˜λ„λ‘,
μƒμ„±μžλ₯Ό private 으둜 μ„ μ–Έν•΄μ£ΌλŠ” 것을 μ’‹μ•„ν•©λ‹ˆλ‹€.

Comment on lines +10 to +12
public static void add(TypedParser... parsers) {
parserList.addAll(List.of(parsers));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš” λ©”μ„œλ“œλŠ” 어디에 μ“°μ΄λŠ”μ§€, 무슨 μ˜λ„λ‘œ λ§Œλ“  것인지 κΆκΈˆν•΄μ„œ μ—¬μ­ˆμ–΄λ΄…λ‹ˆλ‹€.
λŸ°νƒ€μž„ 쀑에 TypedParser κ°€ μΆ”κ°€λ˜λŠ” κ²½μš°κ°€ 상상이 κ°€μ§€λ₯Ό μ•Šμ•„μ„œμš”.

Comment on lines +6 to +21
public class TypedParser {
protected static final TypedParser STRING = new TypedParser(String.class, (s) -> s);
protected static final TypedParser INT = new TypedParser(int.class, Integer::parseInt);
protected static final TypedParser WRAPPER_INTEGER = new TypedParser(Integer.class, Integer::parseInt);
protected static final TypedParser LONG = new TypedParser(long.class, Long::parseLong);
protected static final TypedParser WRAPPER_LONG = new TypedParser(Long.class, Integer::parseInt);
protected static final TypedParser BOOLEAN = new TypedParser(boolean.class, Boolean::parseBoolean);
protected static final TypedParser WRAPPER_BOOLEAN = new TypedParser(Boolean.class, Boolean::parseBoolean);
protected static final TypedParser SHORT = new TypedParser(short.class, Short::parseShort);
protected static final TypedParser WRAPPER_SHORT = new TypedParser(Short.class, Short::parseShort);
protected static final TypedParser FLOAT = new TypedParser(float.class, Float::parseFloat);
protected static final TypedParser WRAPPER_FLOAT = new TypedParser(Float.class, Float::parseFloat);
protected static final TypedParser DOUBLE = new TypedParser(double.class, Double::parseDouble);
protected static final TypedParser WRAPPER_DOUBLE = new TypedParser(Double.class, Double::parseDouble);
protected static final TypedParser CHAR = new TypedParser(char.class, s -> s.charAt(0));
protected static final TypedParser WRAPPER_CHAR = new TypedParser(Character.class, s -> s.charAt(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypedParser 내뢀에 μ„ μ–Έλœ static μƒμˆ˜λ“€μ€ TypedParsers μ—μ„œλ§Œ μ“°μ΄λŠ” λ“―ν•œλ°,
ꡳ이 μƒμˆ˜λͺ…κΉŒμ§€ ν•˜λ‚˜ν•˜λ‚˜ μ§€μ–΄κ°€λ©° protected ν•˜κ²Œ 곡개λ₯Ό ν•  ν•„μš”κ°€ μžˆμ„μ§€ κΆκΈˆν•©λ‹ˆλ‹€.

TypedParsers 클래슀 μ•ˆμ—μ„œ 읡λͺ…μœΌλ‘œ μ„ μ–Έν•  μˆ˜λ„ μžˆμ„ 것 κ°™μ•„μ„œμš”.

Comment on lines +14 to +18
public static Object parse(final Class<?> type, final String value) {
return parserList.stream().filter(parser -> parser.canParse(type)).findFirst()
.orElseThrow(IllegalArgumentException::new)
.parse(value);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν˜„μž¬ TypeParser λ₯Ό 찾을 λ•Œ List μ—μ„œ findFirst λ₯Ό ν•˜λŠ”λ°,
μΌκΈ‰μ»¬λ ‰μ…˜μ„ ν™œμš©ν•˜κ³  μ‹ΆμœΌμ…¨λ‹€κ³  μ˜λ„λ₯Ό μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€.
μΌκΈ‰μ»¬λ ‰μ…˜ 객체에 λŒ€ν•œ ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±λ„ 같이 ν•΄μ£Όμ‹œλ©΄ μ–΄λ–¨κΉŒ μ œμ•ˆν•΄λ΄…λ‹ˆλ‹€.

Comment on lines +27 to +41
public boolean matches(String path) {
return pattern.matcher(path).matches();
}

public Map<String, String> extractUriVariables(String path) {
var matcher = pattern.matcher(path);
if (!matcher.matches()) {
return Collections.emptyMap();
}
Map<String, String> variables = new HashMap<>();
for (int i = 0; i < variableNames.size(); i++) {
variables.put(variableNames.get(i), matcher.group(i + 1));
}
return variables;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PathPatternUtil 에 λŒ€ν•œ ν…ŒμŠ€νŠΈλŠ” μž‘μ„±ν•΄μ£Όμ…¨μ§€λ§Œ, μ •μž‘ PathPatternParser 객체에 λŒ€ν•΄μ„œλŠ” ν…ŒμŠ€νŠΈ μž‘μ„±ν•΄μ£Όμ‹œμ§€ μ•Šμ•˜λŠ”λ° 보완 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.

Comment on lines +8 to +11
public interface ParameterParser {
Object parse(final Method method, final Parameter parameter, final HttpServletRequest request);

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParameterParser λ₯Ό μΈν„°νŽ˜μ΄μŠ€λ‘œ μ •μ˜ν•΄μ£Όμ‹œκ³ ,
이에 λŒ€ν•œ 3κ°€μ§€ κ΅¬ν˜„μ²΄λ₯Ό λ§Œλ“€μ–΄μ£Όμ‹ μ  μ’‹μŠ΅λ‹ˆλ‹€. πŸ‘
(ParameterTypedParser, PathVariableParser, QueryParamParser)

Comment on lines +13 to +23
public class ParameterParsers {

private final List<ParameterParser> parsers = new ArrayList<>(Arrays.asList(new PathVariableParser(), new QueryParamParser()));

public ParameterParsers() {

}

public ParameterParsers(ParameterParser... parsers) {
this.parsers.addAll(Arrays.asList(parsers));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParameterParsers μΌκΈ‰μ»¬λ ‰μ…˜μ€ λ””ν΄νŠΈλ‘œ PathVariableParser 와 QueryParamParser λ₯Ό κ°€μ§€κ³ ,
νŒŒλΌλ―Έν„°λ‘œ λ„˜κ²¨λ°›μ€ parsers λ₯Ό μΆ”κ°€λ‘œ List 에 μΆ”κ°€λ₯Ό ν•˜λŠ” κ΅°μš”

@@ -18,8 +21,9 @@ public class DispatcherServlet extends HttpServlet {
private final HandlerAdapterRegistry handlerAdapters = new HandlerAdapterRegistry();

public DispatcherServlet() {
requestHandlers.addHandlerMapping(new ControllerHandlerMapping(BASE_PACKAGE));
exceptionHandlers.addHandlerMapping(new ControllerAdviceHandlerMapping(BASE_PACKAGE));
var parameterParsers = new ParameterParsers(new PathVariableParser(), new QueryParamParser());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 λ””ν΄νŠΈλ‘œ PathVariableParser 와 QueryParamParser λ₯Ό
ParameterParsers κ°€ κ°€μ§€κ³  μžˆλŠ”λ°,
λ‹€μ‹œ νŒŒλΌλ―Έν„°λ‘œ λ„˜κ²¨μ£Όμ–΄μ„œ addAll 을 ꡳ이 ν•˜λŠ” μ΄μœ κ°€ λ¬΄μ—‡μΈκ°€μš”?


private ControllerHandlerMapping handlerMapping;
private HttpRequestHandlers handlers = new HttpRequestHandlers();
private ParameterParsers parsers = new ParameterParsers(new ParameterTypedParser(TestUser.class));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ParameterTypedParser 의 경우 μ‚¬μš©λ²•μ΄,
μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œμžκ°€ μžμ‹ μ΄ λ§Œλ“  클래슀 정보λ₯Ό ParameterTypedParser μΈμŠ€ν„΄μŠ€λ‘œ λ§Œλ“€μ–΄μ„œ ParameterParsers 에 λ„˜κ²¨μ£Όμ–΄μ•Όν•œλ‹€κ³  μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€.

μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œμžμ—κ²Œ ν”„λ ˆμž„μ›Œν¬μ˜ μ‘°μž‘μ„ μ§€λ‚˜μΉ˜κ²Œ 많이 λ§‘κΈ°λŠ” 것은 μ•„λ‹Œκ°€ ν•˜λŠ” 생각이 λ“­λ‹ˆλ‹€.

Comment on lines +35 to 36
@RequestMapping(value = "/users/bean", method = RequestMethod.POST)
public ModelAndView create_javabean(TestUser testUser) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

컨트둀러 λ©”μ„œλ“œμ˜ νŒŒλΌλ―Έν„° νƒ€μž… 클래슀 정보λ₯Ό 직접 μ½μ–΄μ™€μ„œ,
μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ κ°œλ°œμžκ°€ μ•„λ‹Œ ν”„λ ˆμž„μ›Œν¬ μ°¨μ›μ—μ„œ new ParameterTypedParser(TestUser.class) μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“€ 수 μžˆλ„λ‘ λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.
ν˜„μž¬λŠ” ν”„λ ˆμž„μ›Œν¬μ—μ„œ ParameterTypedParser μΈμŠ€ν„΄μŠ€κ°€ μƒμ„±λ˜κ³  λ“±λ‘λ˜λŠ” 둜직이 λ―Έκ΅¬ν˜„λ˜μ–΄ μžˆλ‹€λŠ” λŠλ‚Œμ„ λ°›μ•˜μŠ΅λ‹ˆλ‹€.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants