Java Code Examples for com.typesafe.config.ConfigValue#valueType()

The following examples show how to use com.typesafe.config.ConfigValue#valueType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComponentConfigurator.java    From streams with Apache License 2.0 6 votes vote down vote up
/**
 * returns a version of a config with only values - no nested objects.
 *
 * @param config config
 * @return result
 */
private Config valuesOnly(Config config) {
  for (String key : config.root().keySet()) {
    LOGGER.debug("key: ", key);
    try {
      ConfigValue value = config.getValue(key);
      LOGGER.debug("child type: ", value.valueType());
      if (value.valueType() == ConfigValueType.OBJECT) {
        config = config.withoutPath(key);
      }
    } catch(ConfigException configExceptions) {
      LOGGER.debug("error processing");
      config = config.withoutPath("\""+key+"\"");
    }
  }
  return config;
}
 
Example 2
Source File: SwiftConfigSchema.java    From swift-k with Apache License 2.0 6 votes vote down vote up
private Object checkValue(String k, ConfigValue value, ConfigPropertyType<?> t) {
    Object v = value.unwrapped();
    switch (value.valueType()) {
        case STRING:
            // allow auto-conversion from string
            return t.check(k, value.unwrapped(), value.origin());
        case NUMBER:
            if (t.getBaseType() != ConfigPropertyType.INT && t.getBaseType() != ConfigPropertyType.FLOAT) {
                throw invalidValue(value, k, v, t.getBaseType());
            }
            if (t.getBaseType() == ConfigPropertyType.INT) {
                Number n = (Number) value.unwrapped();
                if (n.intValue() != n.doubleValue()) {
                    throw invalidValue(value, k, v, t.getBaseType());
                }
            }
            return t.check(k, v, null);
        case BOOLEAN:
            if (t.getBaseType() != ConfigPropertyType.BOOLEAN) {
                throw invalidValue(value, k, v, t.getBaseType());
            }
            return value.unwrapped();
        default:
            return t.check(k, v, value.origin());
    }
}
 
Example 3
Source File: VcapServicesStringParser.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static ConfigObject getAsConfigObject(final ConfigList configList) {
    final Map<String, ConfigValue> flattenedConfigValues = new HashMap<>(configList.size());

    for (int i = 0; i < configList.size(); i++) {
        final ConfigValue configValue = configList.get(i);
        final String configPath;
        if (ConfigValueType.OBJECT == configValue.valueType()) {
            configPath = getName((ConfigObject) configValue);
        } else {
            configPath = String.valueOf(i);
        }
        flattenedConfigValues.put(configPath, configValue);
    }

    return ConfigValueFactory.fromMap(flattenedConfigValues);
}
 
Example 4
Source File: FlattenJsonBolt.java    From cognition with Apache License 2.0 6 votes vote down vote up
void parseJson(String jsonString, LogRecord logRecord) {
  String cleanedJsonString = IngestUtilities.removeUnprintableCharacters(jsonString);
  Config cfg = ConfigFactory.parseString(cleanedJsonString);
  for (Map.Entry<String, ConfigValue> entry : cfg.entrySet()) {
    String key = entry.getKey();
    ConfigValue value = entry.getValue();
    switch (value.valueType()) {
      case BOOLEAN:
      case NUMBER:
      case OBJECT:
      case STRING:
        logRecord.setValue(key, ObjectUtils.toString(value.unwrapped()));
        break;
      case LIST:
        ConfigList list = (ConfigList) value;
        Gson gson = new Gson();
        String json = gson.toJson(list.unwrapped());
        logRecord.setValue(key, json);
        break;
      case NULL:
      default:
        // skip
    }
  }
}
 
Example 5
Source File: DefaultScopedConfig.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private int tryToGetIntValue(final String path) {
    try {
        return config.getInt(path);
    } catch (final ConfigException.WrongType e) {
        final ConfigValue configValue = config.getValue(path);
        if (ConfigValueType.STRING == configValue.valueType()) {
            return Integer.parseInt(String.valueOf(configValue.unwrapped()));
        }
        throw e;
    }
}
 
Example 6
Source File: HoconTreeTraversingParser.java    From jackson-dataformat-hocon with Apache License 2.0 5 votes vote down vote up
protected ConfigValue currentNumericNode()
    throws JsonParseException
{
	ConfigValue n = currentNode();
    if (n == null || n.valueType() != ConfigValueType.NUMBER) {
        JsonToken t = (n == null) ? null : asJsonToken(n);
        throw _constructError("Current token ("+t+") not numeric, can not use numeric value accessors");
    }
    return n;
}
 
Example 7
Source File: TemporaryJobs.java    From helios with Apache License 2.0 5 votes vote down vote up
private static List<String> getListByKey(final String key, final Config config) {
  final ConfigList endpointList = config.getList(key);
  final List<String> stringList = Lists.newArrayList();
  for (final ConfigValue v : endpointList) {
    if (v.valueType() != ConfigValueType.STRING) {
      throw new RuntimeException("Item in " + key + " list [" + v + "] is not a string");
    }
    stringList.add((String) v.unwrapped());
  }
  return stringList;
}
 
Example 8
Source File: TypesafeConfigModule.java    From typesafeconfig-guice with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
private Object getConfigValue(Class<?> paramClass, Type paramType, String path) {
	Optional<Object> extractedValue = ConfigExtractors.extractConfigValue(config, paramClass, path);
	if (extractedValue.isPresent()) {
		return extractedValue.get();
	}

	ConfigValue configValue = config.getValue(path);
	ConfigValueType valueType = configValue.valueType();
	if (valueType.equals(ConfigValueType.OBJECT) && Map.class.isAssignableFrom(paramClass)) {
		ConfigObject object = config.getObject(path);
           return object.unwrapped();
	} else if (valueType.equals(ConfigValueType.OBJECT)) {
		Object bean = ConfigBeanFactory.create(config.getConfig(path), paramClass);
		return bean;
	} else if (valueType.equals(ConfigValueType.LIST) && List.class.isAssignableFrom(paramClass)) {
		Type listType = ((ParameterizedType) paramType).getActualTypeArguments()[0];
		
		Optional<List<?>> extractedListValue = 
			ListExtractors.extractConfigListValue(config, listType, path);
		
		if (extractedListValue.isPresent()) {
			return extractedListValue.get();
		} else {
			List<? extends Config> configList = config.getConfigList(path);
			return configList.stream()
				.map(cfg -> {
					Object created = ConfigBeanFactory.create(cfg, (Class) listType);
					return created;
				})
				.collect(Collectors.toList());
		}
	}
	
	throw new RuntimeException("Cannot obtain config value for " + paramType + " at path: " + path);
}
 
Example 9
Source File: BaseMessageTemplates.java    From UHC with MIT License 5 votes vote down vote up
@Override
public List<String> getRawStrings(String path) {
    final ConfigValue value = config.getValue(path);

    if (value.valueType() == ConfigValueType.LIST) {
        return config.getStringList(path);
    }

    return Lists.newArrayList(config.getString(path));
}
 
Example 10
Source File: Runner.java    From envelope with Apache License 2.0 5 votes vote down vote up
void initializeUDFs(Config config) {
  if (!config.hasPath(UDFS_SECTION_CONFIG)) return;

  ConfigList udfList = config.getList(UDFS_SECTION_CONFIG);

  for (ConfigValue udfValue : udfList) {
    ConfigValueType udfValueType = udfValue.valueType();
    if (!udfValueType.equals(ConfigValueType.OBJECT)) {
      throw new RuntimeException("UDF list must contain UDF objects");
    }

    Config udfConfig = ((ConfigObject)udfValue).toConfig();

    for (String path : Lists.newArrayList(UDFS_NAME, UDFS_CLASS)) {
      if (!udfConfig.hasPath(path)) {
        throw new RuntimeException("UDF entries must provide '" + path + "'");
      }
    }

    String name = udfConfig.getString(UDFS_NAME);
    String className = udfConfig.getString(UDFS_CLASS);

    // null third argument means that registerJava will infer the return type
    Contexts.getSparkSession().udf().registerJava(name, className, null);

    LOG.info("Registered Spark SQL UDF: " + name);
  }
}
 
Example 11
Source File: ConfigCenterTest.java    From mpush with Apache License 2.0 5 votes vote down vote up
public static void print2(String s, ConfigValue configValue, String p, int level) {
    int l = level + 1;
    switch (configValue.valueType()) {
        case OBJECT:
            printTab(level);
            System.out.printf("interface %s {%n", s.replace('-', '_'));
            printTab(level);
            System.out.printf("    Config cfg = %s.cfg.getObject(\"%s\").toConfig();%n%n", p, s);
            ((ConfigObject) configValue).forEach((s1, configValue2) -> print2(s1, configValue2, s, l));
            printTab(level);
            System.out.printf("}%n%n");
            break;
        case STRING:
            printTab(level);
            System.out.printf("  String %s = cfg.getString(\"%s\");%n%n", s.replace('-', '_'), s);
            break;
        case NUMBER:
            printTab(level);
            System.out.printf("  Number %s = cfg.getNumber(\"%s\");%n%n", s.replace('-', '_'), s);
            break;
        case BOOLEAN:
            printTab(level);
            System.out.printf("  Boolean %s = cfg.getBoolean(\"%s\");%n%n", s.replace('-', '_'), s);
            break;
        case LIST:
            printTab(level);
            System.out.printf("  List<Object> %s = cfg.getList(\"%s\").unwrapped();%n%n", s.replace('-', '_'), s);
            break;
    }
}
 
Example 12
Source File: SabotConfigIterable.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public OptionValue next() {
  final Entry<String, ConfigValue> e = entries.next();
  final ConfigValue cv = e.getValue();
  final String name = e.getKey();
  OptionValue optionValue = null;
  switch(cv.valueType()) {
  case BOOLEAN:
    optionValue = OptionValue.createBoolean(OptionType.BOOT, name, (Boolean) cv.unwrapped());
    break;

  case LIST:
  case OBJECT:
  case STRING:
    optionValue = OptionValue.createString(OptionType.BOOT, name, cv.render());
    break;

  case NUMBER:
    optionValue = OptionValue.createLong(OptionType.BOOT, name, ((Number) cv.unwrapped()).longValue());
    break;

  case NULL:
    throw new IllegalStateException("Config value \"" + name + "\" has NULL type");
  }

  return optionValue;
}
 
Example 13
Source File: DefaultScopedConfig.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private double tryToGetDoubleValue(final String path) {
    try {
        return config.getDouble(path);
    } catch (final ConfigException.WrongType e) {
        final ConfigValue configValue = config.getValue(path);
        if (ConfigValueType.STRING == configValue.valueType()) {
            return Double.parseDouble(String.valueOf(configValue.unwrapped()));
        }
        throw e;
    }
}
 
Example 14
Source File: DefaultScopedConfig.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private long tryToGetLongValue(final String path) {
    try {
        return config.getLong(path);
    } catch (final ConfigException.WrongType e) {
        final ConfigValue configValue = config.getValue(path);
        if (ConfigValueType.STRING == configValue.valueType()) {
            return Long.parseLong(String.valueOf(configValue.unwrapped()));
        }
        throw e;
    }
}
 
Example 15
Source File: DrillConfigIterator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public OptionValue next() {
  final Entry<String, ConfigValue> e = entries.next();
  final ConfigValue cv = e.getValue();
  final String name = e.getKey();
  OptionValue optionValue = null;
  switch(cv.valueType()) {
  case BOOLEAN:
    optionValue = OptionValue.create(AccessibleScopes.BOOT, name, (Boolean) cv.unwrapped(), OptionScope.BOOT);
    break;

  case LIST:
  case OBJECT:
  case STRING:
    optionValue = OptionValue.create(AccessibleScopes.BOOT, name, cv.render(),OptionScope.BOOT);
    break;

  case NUMBER:
    optionValue = OptionValue.create(OptionValue.AccessibleScopes.BOOT, name, ((Number) cv.unwrapped()).longValue(),OptionScope.BOOT);
    break;

  case NULL:
    throw new IllegalStateException("Config value \"" + name + "\" has NULL type");

  default:
    throw new IllegalStateException("Unknown type: " + cv.valueType());
  }

  return optionValue;
}
 
Example 16
Source File: SwiftConfig.java    From swift-k with Apache License 2.0 4 votes vote down vote up
private void checkType(String name, ConfigValue value, ConfigValueType type) {
    if (!type.equals(value.valueType())) {
        throw new SwiftConfigException(value.origin(), 
            "'" + name + "': wrong type (" + value.valueType() + "). Must be a " + type);
    }
}
 
Example 17
Source File: VcapServicesStringParser.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private static ConfigValue convertConfigListToConfigObject(final ConfigValue configValue) {
    if (ConfigValueType.LIST == configValue.valueType()) {
        return getAsConfigObject((ConfigList) configValue);
    }
    return configValue;
}
 
Example 18
Source File: HoconNodeCursor.java    From jackson-dataformat-hocon with Apache License 2.0 4 votes vote down vote up
protected static boolean isArray(ConfigValue value) {
	return value.valueType() == ConfigValueType.LIST;
}
 
Example 19
Source File: HoconNodeCursor.java    From jackson-dataformat-hocon with Apache License 2.0 4 votes vote down vote up
protected static boolean isObject(ConfigValue value) {
	return value.valueType() == ConfigValueType.OBJECT;
}