use of com.typesafe.config.ConfigOrigin in project apollo by spotify.
the class TypesafeConfigSerializer method serialize.
@Override
public void serialize(ConfigValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value.valueType() == ConfigValueType.OBJECT) {
final ConfigObject object = (ConfigObject) value;
jgen.writeStartObject();
for (Map.Entry<String, ConfigValue> valueEntry : object.entrySet()) {
if (withOrigins) {
final ConfigOrigin origin = valueEntry.getValue().origin();
jgen.writeStringField(valueEntry.getKey() + "__origin", origin.description() + (origin.filename() != null ? ", " + origin.filename() : ""));
}
jgen.writeObjectField(valueEntry.getKey(), valueEntry.getValue());
}
jgen.writeEndObject();
} else if (value.valueType() == ConfigValueType.LIST) {
final ConfigList list = (ConfigList) value;
jgen.writeStartArray();
for (ConfigValue configValue : list) {
jgen.writeObject(configValue);
}
jgen.writeEndArray();
} else {
jgen.writeObject(value.unwrapped());
}
}
use of com.typesafe.config.ConfigOrigin in project config by typesafehub.
the class SimpleConfigList method concatenate.
final SimpleConfigList concatenate(SimpleConfigList other) {
ConfigOrigin combinedOrigin = SimpleConfigOrigin.mergeOrigins(origin(), other.origin());
List<AbstractConfigValue> combined = new ArrayList<AbstractConfigValue>(value.size() + other.value.size());
combined.addAll(value);
combined.addAll(other.value);
return new SimpleConfigList(combinedOrigin, combined);
}
use of com.typesafe.config.ConfigOrigin in project config by typesafehub.
the class SimpleConfigOrigin method mergeOrigins.
static ConfigOrigin mergeOrigins(Collection<? extends ConfigOrigin> stack) {
if (stack.isEmpty()) {
throw new ConfigException.BugOrBroken("can't merge empty list of origins");
} else if (stack.size() == 1) {
return stack.iterator().next();
} else if (stack.size() == 2) {
Iterator<? extends ConfigOrigin> i = stack.iterator();
return mergeTwo((SimpleConfigOrigin) i.next(), (SimpleConfigOrigin) i.next());
} else {
List<SimpleConfigOrigin> remaining = new ArrayList<SimpleConfigOrigin>(stack.size());
for (ConfigOrigin o : stack) {
remaining.add((SimpleConfigOrigin) o);
}
while (remaining.size() > 2) {
SimpleConfigOrigin c = remaining.get(remaining.size() - 1);
remaining.remove(remaining.size() - 1);
SimpleConfigOrigin b = remaining.get(remaining.size() - 1);
remaining.remove(remaining.size() - 1);
SimpleConfigOrigin a = remaining.get(remaining.size() - 1);
remaining.remove(remaining.size() - 1);
SimpleConfigOrigin merged = mergeThree(a, b, c);
remaining.add(merged);
}
// should be down to either 1 or 2
return mergeOrigins(remaining);
}
}
use of com.typesafe.config.ConfigOrigin in project config by typesafehub.
the class ConfigConcatenation method join.
/**
* Add left and right, or their merger, to builder.
*/
private static void join(ArrayList<AbstractConfigValue> builder, AbstractConfigValue origRight) {
AbstractConfigValue left = builder.get(builder.size() - 1);
AbstractConfigValue right = origRight;
// (this will be an object with numeric keys, like foo.0, foo.1)
if (left instanceof ConfigObject && right instanceof SimpleConfigList) {
left = DefaultTransformer.transform(left, ConfigValueType.LIST);
} else if (left instanceof SimpleConfigList && right instanceof ConfigObject) {
right = DefaultTransformer.transform(right, ConfigValueType.LIST);
}
// Since this depends on the type of two instances, I couldn't think
// of much alternative to an instanceof chain. Visitors are sometimes
// used for multiple dispatch but seems like overkill.
AbstractConfigValue joined = null;
if (left instanceof ConfigObject && right instanceof ConfigObject) {
joined = right.withFallback(left);
} else if (left instanceof SimpleConfigList && right instanceof SimpleConfigList) {
joined = ((SimpleConfigList) left).concatenate((SimpleConfigList) right);
} else if ((left instanceof SimpleConfigList || left instanceof ConfigObject) && isIgnoredWhitespace(right)) {
joined = left;
// it should be impossible that left is whitespace and right is a list or object
} else if (left instanceof ConfigConcatenation || right instanceof ConfigConcatenation) {
throw new ConfigException.BugOrBroken("unflattened ConfigConcatenation");
} else if (left instanceof Unmergeable || right instanceof Unmergeable) {
// leave joined=null, cannot join
} else {
// handle primitive type or primitive type mixed with object or list
String s1 = left.transformToString();
String s2 = right.transformToString();
if (s1 == null || s2 == null) {
throw new ConfigException.WrongType(left.origin(), "Cannot concatenate object or list with a non-object-or-list, " + left + " and " + right + " are not compatible");
} else {
ConfigOrigin joinedOrigin = SimpleConfigOrigin.mergeOrigins(left.origin(), right.origin());
joined = new ConfigString.Quoted(joinedOrigin, s1 + s2);
}
}
if (joined == null) {
builder.add(right);
} else {
builder.remove(builder.size() - 1);
builder.add(joined);
}
}
use of com.typesafe.config.ConfigOrigin in project config by typesafehub.
the class AbstractConfigObject method mergeOrigins.
static ConfigOrigin mergeOrigins(Collection<? extends AbstractConfigValue> stack) {
if (stack.isEmpty())
throw new ConfigException.BugOrBroken("can't merge origins on empty list");
List<ConfigOrigin> origins = new ArrayList<ConfigOrigin>();
ConfigOrigin firstOrigin = null;
int numMerged = 0;
for (AbstractConfigValue v : stack) {
if (firstOrigin == null)
firstOrigin = v.origin();
if (v instanceof AbstractConfigObject && ((AbstractConfigObject) v).resolveStatus() == ResolveStatus.RESOLVED && ((ConfigObject) v).isEmpty()) {
// don't include empty files or the .empty()
// config in the description, since they are
// likely to be "implementation details"
} else {
origins.add(v.origin());
numMerged += 1;
}
}
if (numMerged == 0) {
// the configs were all empty, so just use the first one
origins.add(firstOrigin);
}
return SimpleConfigOrigin.mergeOrigins(origins);
}
Aggregations