use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.
the class Attribute method describe.
public CNode describe(Owner instance, ConfigurationContext context) throws ConfiguratorException {
final Configurator c = context.lookup(type);
if (c == null) {
return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": No configurator found for type " + type);
}
try {
Object o = getValue(instance);
if (o == null) {
return null;
}
// In Export we sensitive only those values which do not get rendered as secrets
boolean shouldBeMasked = isSecret(instance);
if (multiple) {
Sequence seq = new Sequence();
if (o.getClass().isArray())
o = Arrays.asList((Object[]) o);
if (o instanceof Iterable) {
for (Object value : (Iterable) o) {
seq.add(_describe(c, context, value, shouldBeMasked));
}
} else {
LOGGER.log(Level.FINE, o.getClass() + " is not iterable");
}
return seq;
}
return _describe(c, context, o, shouldBeMasked);
} catch (Exception | /* Jenkins.getDescriptorOrDie */
AssertionError e) {
// Don't fail the whole export, prefer logging this error
LOGGER.log(Level.WARNING, "Failed to export", e);
return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": " + printThrowable(e));
}
}
use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method toYaml.
@CheckForNull
// for testing only
@Restricted(NoExternalUse.class)
public Node toYaml(CNode config) throws ConfiguratorException {
if (config == null)
return null;
switch(config.getType()) {
case MAPPING:
final Mapping mapping = config.asMapping();
final List<NodeTuple> tuples = new ArrayList<>();
final List<Map.Entry<String, CNode>> entries = new ArrayList<>(mapping.entrySet());
entries.sort(Map.Entry.comparingByKey());
for (Map.Entry<String, CNode> entry : entries) {
final Node valueNode = toYaml(entry.getValue());
if (valueNode == null)
continue;
tuples.add(new NodeTuple(new ScalarNode(Tag.STR, entry.getKey(), null, null, PLAIN), valueNode));
}
if (tuples.isEmpty())
return null;
return new MappingNode(Tag.MAP, tuples, BLOCK);
case SEQUENCE:
final Sequence sequence = config.asSequence();
List<Node> nodes = new ArrayList<>();
for (CNode cNode : sequence) {
final Node valueNode = toYaml(cNode);
if (valueNode == null)
continue;
nodes.add(valueNode);
}
if (nodes.isEmpty())
return null;
return new SequenceNode(Tag.SEQ, nodes, BLOCK);
case SCALAR:
default:
final Scalar scalar = config.asScalar();
final String value = scalar.getValue();
if (value == null || value.length() == 0)
return null;
final DumperOptions.ScalarStyle style;
if (scalar.getFormat().equals(Format.MULTILINESTRING) && !scalar.isRaw()) {
style = LITERAL;
} else if (scalar.isRaw()) {
style = PLAIN;
} else {
style = DOUBLE_QUOTED;
}
return new ScalarNode(getTag(scalar.getFormat()), value, null, null, style);
}
}
use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.
the class ModelConstructor method constructMapping2ndStep.
/**
* Enforce Map keys are only Scalars and can be used as {@link String} keys in {@link Mapping}
*/
@Override
protected void constructMapping2ndStep(MappingNode node, final Map mapping) {
((Mapping) mapping).setSource(getSource(node));
super.constructMapping2ndStep(node, new AbstractMapDecorator(mapping) {
@Override
public Object put(Object key, Object value) {
if (!(key instanceof Scalar))
throw new IllegalStateException("We only support scalar map keys");
Object scalar = ObjectUtils.clone(value);
if (scalar instanceof Number)
scalar = new Scalar(scalar.toString());
else if (scalar instanceof Boolean)
scalar = new Scalar(scalar.toString());
return mapping.put(key.toString(), scalar);
}
});
}
use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.
the class PrimitiveConfiguratorTest method _Integer_env.
@Test
public void _Integer_env() throws Exception {
environment.set("ENV_FOR_TEST", "123");
Configurator c = registry.lookupOrFail(Integer.class);
final Object value = c.configure(new Scalar("${ENV_FOR_TEST}"), context);
assertEquals(123, (int) value);
}
use of io.jenkins.plugins.casc.model.Scalar in project configuration-as-code-plugin by jenkinsci.
the class PrimitiveConfiguratorTest method _string_env.
@Test
public void _string_env() throws Exception {
environment.set("ENV_FOR_TEST", "abc");
Configurator c = registry.lookupOrFail(String.class);
final Object value = c.configure(new Scalar("${ENV_FOR_TEST}"), context);
assertEquals("abc", value);
}
Aggregations