-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to specify updates using regular expressions
- Loading branch information
Showing
22 changed files
with
289 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/io/github/yupd/infrastructure/update/ContentUpdateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.yupd.infrastructure.update; | ||
|
||
import io.github.yupd.infrastructure.update.model.ContentUpdateCriteria; | ||
import io.github.yupd.infrastructure.update.model.ContentUpdateType; | ||
import io.github.yupd.infrastructure.update.updator.RegexUpdator; | ||
import io.github.yupd.infrastructure.update.updator.YamlPathUpdator; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@ApplicationScoped | ||
public class ContentUpdateService { | ||
|
||
@Inject | ||
RegexUpdator regexUpdator; | ||
|
||
@Inject | ||
YamlPathUpdator yamlPathUpdator; | ||
|
||
public String update(String content, List<ContentUpdateCriteria> updates) { | ||
return computeRegexUpdates(computeYamlPathUpdates(content, updates), updates); | ||
} | ||
|
||
private String computeYamlPathUpdates(String content, List<ContentUpdateCriteria> updates) { | ||
List<ContentUpdateCriteria> yamlPathUpdates = filterUpdates(updates, ContentUpdateType.YAMLPATH); | ||
if (yamlPathUpdates.isEmpty()) { | ||
return content; | ||
} | ||
return yamlPathUpdator.update(content, yamlPathUpdates); | ||
} | ||
|
||
private String computeRegexUpdates(String content, List<ContentUpdateCriteria> updates) { | ||
List<ContentUpdateCriteria> regexUpdates = filterUpdates(updates, ContentUpdateType.REGEX); | ||
if (regexUpdates.isEmpty()) { | ||
return content; | ||
} | ||
return regexUpdator.update(content, regexUpdates); | ||
} | ||
|
||
private static List<ContentUpdateCriteria> filterUpdates(List<ContentUpdateCriteria> updates, ContentUpdateType type) { | ||
return updates.stream().filter(update -> update.type() == type).collect(Collectors.toList()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/github/yupd/infrastructure/update/model/ContentUpdateCriteria.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.github.yupd.infrastructure.update.model; | ||
|
||
import java.util.Map; | ||
|
||
public record ContentUpdateCriteria(ContentUpdateType type, String key, String value) { | ||
|
||
public ContentUpdateCriteria(String key, String value) { | ||
this(ContentUpdateType.computeType(key), | ||
key.replace(ContentUpdateType.computeType(key).getPrefix(), ""), | ||
value); | ||
} | ||
|
||
public ContentUpdateCriteria(Map.Entry<String, String> entry) { | ||
this(entry.getKey(), entry.getValue()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/io/github/yupd/infrastructure/update/model/ContentUpdateType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.yupd.infrastructure.update.model; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum ContentUpdateType { | ||
YAMLPATH("ypath:", "yamlpath"), | ||
REGEX("regex:", "regex"); | ||
|
||
private String prefix; | ||
private String displayName; | ||
|
||
ContentUpdateType(String prefix, String displayName) { | ||
this.prefix = prefix; | ||
this.displayName = displayName; | ||
} | ||
|
||
public String getDisplayName() { | ||
return displayName; | ||
} | ||
|
||
public String getPrefix() { | ||
return prefix; | ||
} | ||
|
||
public static ContentUpdateType computeType(String key) { | ||
return Arrays.stream(values()).filter(v -> key.startsWith(v.prefix)).findFirst().orElse(YAMLPATH); | ||
} | ||
} |
Oops, something went wrong.