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

Revisit current transformers for cachebility #778

Closed
musketyr opened this issue Jun 29, 2022 · 1 comment · Fixed by #1169
Closed

Revisit current transformers for cachebility #778

musketyr opened this issue Jun 29, 2022 · 1 comment · Fixed by #1169

Comments

@musketyr
Copy link

Shadow Version

7.1.2

Gradle Version

7.4.1

Expected Behavior

Every transformer which is cacheable is marked with @CacheableTransformer.

Actual Behavior

Some of the transformers are missing the annotation but it's not really clear to me if it's really because they are not cacheable or because their authors have forgotten to add the annotation.

It would be also nice to add more example when the transformer is cacheable and when it is not.

I'm especially interested into Log4j2PluginsCacheFileTransformer and my own transformer to join application.yml files.

public class ApplicationYamlTransformer implements Transformer {

    private static final String FILE_NAME = "application.yml";
    private static final String DOCUMENT_SEPARATOR = "---";

    private final List<String> parts = new ArrayList<>();

    @Override
    public boolean canTransformResource(FileTreeElement element) {
        return element.getName().equals(FILE_NAME);
    }

    @Override
    public void transform(TransformerContext context) {
        parts.add(toString(context.getIs()));
    }

    @Override
    public boolean hasTransformedResource() {
        return !parts.isEmpty();
    }

    @Override
    public void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
        try {
            os.putNextEntry(new ZipEntry(FILE_NAME));
            IOUtil.copy(new ByteArrayInputStream(getContent().getBytes(StandardCharsets.UTF_8)), os);
            os.closeEntry();
        } catch (IOException e) {
            throw new IllegalStateException("Cannot write application.yml", e);
        }
    }

    @Override
    public String getName() {
        return getClass().getSimpleName();
    }

    private String toString(InputStream is) {
        String text = new BufferedReader(
            new InputStreamReader(is, StandardCharsets.UTF_8))
            .lines()
            .collect(Collectors.joining("\n"));

        try {
            is.close();
        } catch (IOException e) {
            throw new IllegalStateException("Cannot close input stream", e);
        }

        return text;
    }

    private String getContent() {
        return parts.stream().map(part -> {
           if (!part.startsWith(DOCUMENT_SEPARATOR)) {
               return String.format("%s\n%s", DOCUMENT_SEPARATOR, part);
           }
           return part;
        }).collect(Collectors.joining(String.format("%n")));
    }
}

Myself, I'm not sure if the transformer can be annotated with @CacheableTransformer or not.

@Goooler
Copy link
Member

Goooler commented Jan 18, 2025

You can mark it @CacheableTransformer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants