Skip to content

Commit

Permalink
Fix/audio tag unsupported in format 1.0.0 (#60)
Browse files Browse the repository at this point in the history
* Refactor logic checking if tag is supported

* Use AUDIO tag only if supported by format version
  • Loading branch information
Nianna authored Oct 10, 2024
1 parent f1205c4 commit ec16919
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private boolean createSong() {
song.setTagValue(TagKey.ARTIST, result.getArtist());
song.setTagValue(TagKey.TITLE, result.getTitle());
song.formatSpecificationVersion()
.filter(format -> format.supports(TagKey.AUDIO))
.ifPresent(ignored -> song.setTagValue(TagKey.AUDIO, result.getAudioFilename()));
song.setTagValue(TagKey.MP3, result.getAudioFilename());
song.setTagValue(TagKey.COVER, result.getCoverFilename());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void buildSubCommands() {
result.getArtist()));
addSubCommand(new ChangeTagValueCommand(song, TagKey.TITLE, result.getTitle()));
addSubCommand(new ChangeTagValueCommand(song, TagKey.MP3, result.getAudioFilename()));
if (song.hasTag(TagKey.AUDIO) || song.formatSpecificationVersion().isPresent()) {
if (song.hasTag(TagKey.AUDIO) || formatSupportsAudio(song)) {
addSubCommand(new ChangeTagValueCommand(song, TagKey.AUDIO, result.getAudioFilename()));
}
addSubCommand(new ChangeTagValueCommand(song, TagKey.COVER, result.getCoverFilename()));
Expand All @@ -76,4 +76,10 @@ protected void buildSubCommands() {
};
}

private static boolean formatSupportsAudio(Song song) {
return song.formatSpecificationVersion()
.filter(format -> format.supports(TagKey.AUDIO))
.isPresent();
}

}
6 changes: 5 additions & 1 deletion src/main/java/com/github/nianna/karedi/song/Song.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public Optional<String> getTagValue(TagKey key) {
}

public Optional<String> getMainAudioTagValue() {
return getTagValue(TagKey.AUDIO).or(() -> getTagValue(TagKey.MP3));
TagKey mainAudioTagKey = formatSpecificationVersion()
.filter(format -> format.supports(TagKey.AUDIO))
.map(ignored -> TagKey.AUDIO)
.orElse(TagKey.MP3);
return getTagValue(mainAudioTagKey);
}

public Optional<String> getTagValue(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public static Optional<FormatSpecification> tryParse(String version) {
.findFirst();
}

public boolean supports(String tagKey) {
return TagKey.optionalValueOf(tagKey)
.filter(this::supports)
.isPresent();
}

public boolean supports(TagKey tagKey) {
return FormatSpecificationSupportedTags.isSupported(this, tagKey);
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.github.nianna.karedi.song.tag;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

class FormatSpecificationSupportedTags {

private static final List<TagKey> V_1_0_0_SUPPORTED_KEYS = List.of(
TagKey.VERSION,
TagKey.TITLE,
TagKey.ARTIST,
TagKey.MP3,
TagKey.BPM,
TagKey.GAP,
TagKey.COVER,
TagKey.BACKGROUND,
TagKey.VIDEO,
TagKey.VIDEOGAP,
TagKey.GENRE,
TagKey.EDITION,
TagKey.CREATOR,
TagKey.LANGUAGE,
TagKey.YEAR,
TagKey.START,
TagKey.END,
TagKey.PREVIEWSTART,
TagKey.MEDLEYSTARTBEAT,
TagKey.MEDLEYENDBEAT,
TagKey.CALCMEDLEY,
TagKey.P1,
TagKey.P2,
TagKey.COMMENT
);

private static final List<TagKey> V_1_1_0_SUPPORTED_KEYS = Stream.concat(
V_1_0_0_SUPPORTED_KEYS.stream(),
Stream.of(TagKey.AUDIO, TagKey.VOCALS, TagKey.INSTRUMENTAL, TagKey.TAGS, TagKey.PROVIDEDBY)
).toList();

private static final Map<FormatSpecification, List<TagKey>> SUPPORTED_KEYS = Map.of(
FormatSpecification.V_1_0_0, V_1_0_0_SUPPORTED_KEYS,
FormatSpecification.V_1_1_0, V_1_1_0_SUPPORTED_KEYS
);

private FormatSpecificationSupportedTags() {

}

static boolean isSupported(FormatSpecification formatSpecification, TagKey tagKey) {
return SUPPORTED_KEYS.get(formatSpecification).contains(tagKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,21 @@
import com.github.nianna.karedi.problem.TagProblem;
import com.github.nianna.karedi.problem.UnsupportedTagProblem;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

public abstract class TagKeyValidator {

private static final List<TagKey> V_1_0_0_SUPPORTED_KEYS = List.of(
TagKey.VERSION,
TagKey.TITLE,
TagKey.ARTIST,
TagKey.MP3,
TagKey.BPM,
TagKey.GAP,
TagKey.COVER,
TagKey.BACKGROUND,
TagKey.VIDEO,
TagKey.VIDEOGAP,
TagKey.GENRE,
TagKey.EDITION,
TagKey.CREATOR,
TagKey.LANGUAGE,
TagKey.YEAR,
TagKey.START,
TagKey.END,
TagKey.PREVIEWSTART,
TagKey.MEDLEYSTARTBEAT,
TagKey.MEDLEYENDBEAT,
TagKey.CALCMEDLEY,
TagKey.P1,
TagKey.P2,
TagKey.COMMENT
);

private static final List<TagKey> V_1_1_0_SUPPORTED_KEYS = Stream.concat(
V_1_0_0_SUPPORTED_KEYS.stream(),
Stream.of(TagKey.AUDIO, TagKey.VOCALS, TagKey.INSTRUMENTAL, TagKey.TAGS, TagKey.PROVIDEDBY)
).toList();

private static final Map<FormatSpecification, List<TagKey>> SUPPORTED_KEYS = Map.of(
FormatSpecification.V_1_0_0, V_1_0_0_SUPPORTED_KEYS,
FormatSpecification.V_1_1_0, V_1_1_0_SUPPORTED_KEYS
);

private TagKeyValidator() {

}

public static Optional<TagProblem> validate(String tag, FormatSpecification formatSpecification) {
if (SUPPORTED_KEYS.get(formatSpecification) == null) {
public static Optional<TagProblem> validate(String tagKey, FormatSpecification formatSpecification) {
if (formatSpecification == null) {
return Optional.empty();
}

boolean isKeySupported = TagKey.optionalValueOf(tag)
.map(SUPPORTED_KEYS.get(formatSpecification)::contains)
.orElse(false);

if (!isKeySupported) {
return Optional.of(new UnsupportedTagProblem(tag));
if (!formatSpecification.supports(tagKey)) {
return Optional.of(new UnsupportedTagProblem(tagKey));
}
return Optional.empty();
}
Expand Down

0 comments on commit ec16919

Please sign in to comment.