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

Feature/ 원하는 필드만 포함하는 printWithInclusions method 추가 #6

Merged
merged 5 commits into from
Jul 24, 2024

Conversation

JinuCheon
Copy link
Contributor

@JinuCheon JinuCheon commented Mar 16, 2024

사용 방법

출력하고 싶은 필드의 객체 path 를 명시합니다.

final String[] filedPathToInclude = {"lineItems.product.description", "lineItems.product.price"};
final String result = YamlPrinter.printWithInclusions(order, filedPathToInclude);
---
lineItems:
- product:
    description: "Bright, citrusy, with a hint of cocoa and a smooth finish."
    price: 8000
- product:
    description: "2-shot original blend Americano."
    price: 5000

객체 depth가 있다면 원하지 않는 필드도 포함되는 버그

예컨대
organization.team.user.id 를 한다면,
organization.id 와 team.id 도 나오게 됩니다

디버그 찍으면서 알게된 것

  • 예상은 했지만, 내부에서 reflection 사용해서 private 필드에 접근합니다
  • ObjectMapper filter 를 쓰기에는 필터링 표현에 한계가 있습니다
    • 기존에 기원님이 만들어두신 printWithExclusions 도 동일

Next

  • 다음과 같이 직접 reflection 활용해서 구현한다면 재미있을 것 같습니다
    • depth를 구분한다
      • 객체가 무한하게 호출 되는 것을 막는다
      • 원하는 depth 의 필드만 추출한다
    • hibernate 영속 / 비영속 상태에서 모두 동작하도록 구현한다
      • try 로 exception 먹어버리고 무시하기
// String[] input = ["lineItems.product.description", "lineItems.quantity"]

//String[] 에서 . 으로 구분된 depth 를 추출해서, 배열로 저장한다

public String printWithInclusions(Object object, String... fieldPathToInclude) {
  List<?> extracted = extract(input);
  retrun findAndPrintFieldLoop(extracted)
}

private String findAndPrintFieldLoop(List<?> extracted) {
  loop {
    if not field matched:
      continue;
    try {
      if(hasDepth()) { // 여기에서 Hibernate 영속성 exception이 나는 것을 try로 먹어버리기
        printObject()
        findFieldLoop() // 재귀
      } else {
        printField()
      }
    }
  } // end of loop
  return accumulatedString;
}

@JinuCheon JinuCheon self-assigned this Mar 16, 2024
Comment on lines +51 to +53
return Arrays.stream(fieldNamesToInclude)
.flatMap(s -> Arrays.stream(s.split("\\.")))
.toArray(String[]::new);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

코파일럿이 저보다 stream 잘 쓰는 것 같습니다 😇

Copy link
Contributor

@arctrls arctrls left a comment

Choose a reason for hiding this comment

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

👏

@JinuCheon JinuCheon merged commit 552e58b into main Jul 24, 2024
@JinuCheon JinuCheon deleted the feature/printWithInclusions branch July 24, 2024 11:06
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.

3 participants