Skip to content
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

대소문자를 구분하지 않는 Attributes #75

Open
wants to merge 15 commits into
base: freddie-noel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/main/java/webserver/http/attribute/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,55 @@ public static Attributes from(Map<String, String> attributes) {
}

public static Attributes from(String headerText) {
Map<String, String> attributes = new LinkedHashMap<>();
Attributes attributes = new Attributes();

String[] splittedHeaderTexts = headerText.split(Const.CRLF);
for (String splittedHeaderText : splittedHeaderTexts) {
HttpRequestUtils.Pair pair = HttpRequestUtils.parseHeader(splittedHeaderText);

if (pair != null) {
attributes.put(pair.getKey(), pair.getValue());
attributes.add(pair.getKey(), pair.getValue());
Dae-Hwa marked this conversation as resolved.
Show resolved Hide resolved
}
}

return from(attributes);
return attributes;
}

public Attributes add(String key, String value) {
attributes.put(key.toUpperCase(), value);
for (String k : attributes.keySet()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

직접 사용할 변수면 명시적으로 이름을 짓는 것도 좋아보여요
eachKey currentKey 같은거라도 좋을 것 같고
그게 힘들면 매개변수 이름을 targetKey 같은 걸로 나눠줘도 좋을 것 같습니다.

Copy link
Owner Author

Choose a reason for hiding this comment

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

앗.. 너무 막지었네요.
지적 감사합니다.

if (k.equalsIgnoreCase(key)) {
return this;
}
}

attributes.put(key, value);
return this;
}

public Attributes addAll(Map<String, String> attributes) {
Map<String, String> upperAttributes = new LinkedHashMap<>();

for (String key : attributes.keySet()) {
upperAttributes.put(key.toUpperCase(), attributes.get(key));
add(key, attributes.get(key));
}

this.attributes.putAll(upperAttributes);
return this;
}

public String get(String key) {
return attributes.get(key.toUpperCase());
for (String k : attributes.keySet()) {
if (k.equalsIgnoreCase(key)) {
return attributes.get(k);
}
}
return null;
Copy link
Owner Author

Choose a reason for hiding this comment

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

null이 조금 걸리네요..ㅎㅎ

Copy link
Owner Author

Choose a reason for hiding this comment

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

exception
null을 받아야할 이유가 있다면: optional

Copy link
Collaborator

Choose a reason for hiding this comment

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

getDefault가 있는데 굳이 null을 리턴해줄 필요는 없다고 보여집니다.
꼭 써야한다면 Optional로 제공해주는 것도 고려해볼 수 있을 것 같아요

Copy link
Collaborator

Choose a reason for hiding this comment

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

그리고 null을 명시적으로 리턴해준다면 get이 null인 케이스도 테스트에서 확인을 해봐야할 것 같습니다!

Copy link
Owner Author

@sanhee sanhee Dec 10, 2021

Choose a reason for hiding this comment

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

이부분 작성하면서도 이상하다 생각했는데 감사합니다!

}

public String getOrDefault(String key, String defaultValue) {
return attributes.getOrDefault(key.toUpperCase(), defaultValue);
for (String k : attributes.keySet()) {
if (k.equalsIgnoreCase(key)) {
return attributes.get(k);
}
}
return defaultValue;
Copy link
Collaborator

Choose a reason for hiding this comment

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

코드 중복이 되는데 get이나 getOrDefault 둘 중 하나를 기준으로 중복 제거를 해볼 수 있겠네요

}

public String toHeaderText() {
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/webserver/http/statusline/RequestStatusLine.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package webserver.http.statusline;

import webserver.http.attribute.Attributes;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;

public class RequestStatusLine extends StatusLine {

Expand All @@ -13,12 +18,16 @@ public RequestStatusLine(Map<String, String> statusLineAttributes) {
super(statusLineAttributes);
}

public RequestStatusLine(Attributes statusLineAttributes) {
super(statusLineAttributes);
}

public static RequestStatusLine from(List<String> statusLine) {
Map<String, String> statusLineAttributes = new HashMap<>();
Attributes statusLineAttributes = new Attributes();

statusLineAttributes.put(METHOD_KEY, statusLine.get(0));
statusLineAttributes.put(PATH_KEY, statusLine.get(1));
statusLineAttributes.put(PROTOCOL_VERSION_KEY, statusLine.get(2));
statusLineAttributes.add(METHOD_KEY, statusLine.get(0));
statusLineAttributes.add(PATH_KEY, statusLine.get(1));
statusLineAttributes.add(PROTOCOL_VERSION_KEY, statusLine.get(2));
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 부분도 Attributes를 만들면서 자연스럽게 검증이 될 테니 Map으로 받아줘도 괜찮을 것 같아요

Copy link
Collaborator

Choose a reason for hiding this comment

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

그게 아니라 생성자로 Attributes를 넣어주는게 더 바람직하다고 생각하신거면 좋은 방법인 것 같습니다
만약 그렇다면 Map<String, String>을 받는 생성자를 완전히 대체해주는게 좋을 것 같습니다.


return new RequestStatusLine(statusLineAttributes);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/webserver/http/statusline/StatusLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public StatusLine(Map<String, String> statusLineAttributes) {
this.statusLineAttributes = Attributes.from(statusLineAttributes);
}

public StatusLine(Attributes statusLineAttributes) {
this.statusLineAttributes = statusLineAttributes;
}

public String getProtocol() {
return statusLineAttributes.get(PROTOCOL_VERSION_KEY);
}
Expand Down