All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.bamboo.specs.codegen.emitters.plan.PlanLabelsEmitter Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
package com.atlassian.bamboo.specs.codegen.emitters.plan;

import com.atlassian.bamboo.specs.api.codegen.CodeEmitter;
import com.atlassian.bamboo.specs.api.codegen.CodeGenerationContext;
import com.atlassian.bamboo.specs.api.codegen.CodeGenerationException;
import com.atlassian.bamboo.specs.api.model.label.EmptyLabelsListProperties;
import com.atlassian.bamboo.specs.api.model.label.LabelProperties;
import com.atlassian.bamboo.specs.codegen.emitters.CodeGenerationUtils;
import com.atlassian.bamboo.specs.codegen.emitters.value.ValueEmitterFactory;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class PlanLabelsEmitter implements CodeEmitter> {
    @NotNull
    @Override
    public String emitCode(@NotNull CodeGenerationContext context, @NotNull Iterable value) throws CodeGenerationException {
        final List labelProperties = StreamSupport.stream(value.spliterator(), false).collect(Collectors.toList());

        if (labelProperties.size() == 1 && labelProperties.get(0) instanceof EmptyLabelsListProperties) {
            return ".noLabels()";
        } else {
            final StringBuilder result = new StringBuilder(".labels(");
            final List valuesAndFails = new ArrayList<>();
            final Set failed = new HashSet<>();

            int count = 0;
            for (LabelProperties labelProperty : labelProperties) {
                final String label = labelProperty.getName();
                try {
                    final CodeEmitter emitter = ValueEmitterFactory.emitterFor(label);
                    final String code = emitter.emitCode(context, label);
                    valuesAndFails.add(code);
                } catch (CodeGenerationException e) {
                    valuesAndFails.add(e.getMessage());
                    failed.add(count);
                }
                count++;
            }

            context.incIndentation();
            CodeGenerationUtils.appendCommaSeparatedList(context, result, valuesAndFails, failed);
            context.decIndentation();

            return result.append(")").toString();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy