Skip to content

Commit

Permalink
fix [#76] ArtistResolver의 load 함수에서 target이 리스트 타입인 경우 탐색 가능하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1hyun committed Jan 17, 2025
1 parent ca20c79 commit a6f3164
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import lombok.RequiredArgsConstructor;
import org.sopt.confeti.annotation.Resolver;
import org.sopt.confeti.global.util.IntegrateFunction;
import org.springframework.aop.support.AopUtils;

@Resolver
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -32,6 +33,16 @@ public class ArtistResolver {
);

private final ConcurrentHashMap<Class<?>, IntegrateFunction> collectByTypeMapper = new ConcurrentHashMap<Class<?>, IntegrateFunction>() {{
put(ArrayList.class, args -> {
if (!(args[0] instanceof List)) {
// TODO:
// 예외 처리 추가
throw new RuntimeException();
}

collectByListType((List<?>) args[0]);
return null;
});
put(List.class, args -> {
if (!(args[0] instanceof List)) {
// TODO:
Expand Down Expand Up @@ -86,9 +97,13 @@ private List<ConfetiArtist> searchByArtistIds(final List<String> artistIds) {
}

// 리플렉션을 사용해 타겟 오브젝트를 재귀적으로 순회하며 아티스트 아이디를 수집하는 함수
// TODO: 추후에 메소드 분리 리펙토링 예정
private void collect(final Object target) {
if (target !=null) {
System.out.println(target);
}
// 이미 탐색한 객체인 경우
if (objectTrack.containsKey(target)) {
if (target == null || objectTrack.containsKey(target)) {
return;
}

Expand All @@ -102,6 +117,12 @@ private void collect(final Object target) {
artistIds.add(artist.getArtistId());
artistMapper.put(artist.getArtistId(), artist);
return;
} else if (collectByTypeMapper.containsKey(target.getClass())) {
// 현재 오브젝트가 정해진 처리 방법이 필요한 타입인 경우 (현재는 List)
collectByTypeMapper.get(target.getClass()).apply(
target
);
return;
}

// 필드에 있는 객체 목록 확인
Expand All @@ -116,36 +137,33 @@ private void collect(final Object target) {
return;
}

setAccessibleIfPrivateOrProtected(field);

try {
collectByType(target, field);
if (collectByTypeMapper.containsKey(field.getType())) {
collectByTypeMapper.get(field.getType()).apply(
field.get(target)
);

return;
}

collect(field.get(target));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
});
}

private void collectByType(final Object target, final Field field) throws IllegalAccessException {
Class<?> fieldType = field.getType();
setAccessibleIfPrivate(field);

try {
collectByTypeMapper.get(fieldType).apply(
field.get(target)
);
} catch (NullPointerException e) {
collect(
field.get(target)
);
}
}

private void collectByListType(final List<?> objects) {
objects.forEach(this::collect);
}

private void setAccessibleIfPrivate(final Field field) {
if (Modifier.isPrivate(field.getModifiers())) {
private void setAccessibleIfPrivateOrProtected(final Field field) {
if (
Modifier.isPrivate(field.getModifiers()) || Modifier.isProtected(field.getModifiers())
) {
field.setAccessible(ACCESS_ALLOW);
}
}
Expand All @@ -169,7 +187,7 @@ private boolean isConfetiArtistClass(final Object object) {
private ConfetiArtist extractConfetiArtist(final Object target, final Field field)
throws RuntimeException {
try {
setAccessibleIfPrivate(field);
setAccessibleIfPrivateOrProtected(field);
return (ConfetiArtist) field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.neovisionaries.i18n.CountryCode;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -62,6 +63,10 @@ public Optional<ConfetiArtist> findArtistsByKeyword(final String keyword) {
}

public List<ConfetiArtist> findArtistsByArtistIds(final List<String> artistIds) {
if (artistIds.isEmpty()) {
return new ArrayList<>();
}

try {
Artist[] artists = spotifyApi.getSeveralArtists(
artistIds.toArray(new String[0])
Expand Down

0 comments on commit a6f3164

Please sign in to comment.