public <T> T get(String key, T def) { Object value = get(key); if (value instanceof Config && ((Config) value).isEmpty() && !(def instanceof Config)) return def; return (T) value; }
@Override public Object get(Object key) { Object o = proxy.get(key); if (o instanceof Config) return createProxyConfig(this, (String) key, this, (Config) o); else return o; }
public <T> T get(String key, T def) { Object value = get(key); if (value instanceof Config && ((Config) value).isEmpty() && !(def instanceof Config)) return def; return (T) value; }
@Override public Object get(Object key) { Object o = proxy.get(key); if (o instanceof Config) return createProxyConfig(this, (String) key, this, (Config) o); else return o; }
public <T> T get(String key, Supplier<T> def) { Object value = get(key); if (value instanceof Config && ((Config) value).isEmpty()) return def.supply(); return (T) value; }
public <T> T get(String key, Supplier<T> def) { Object value = get(key); if (value instanceof Config && ((Config) value).isEmpty()) return def.supply(); return (T) value; }
@Test public void test() throws IOException { server.setEnvironment(Environment.TEST); server.start(); Assert.assertEquals("value", server.getConfig().get("key")); Assert.assertEquals("c", ((Map) server.getConfig().get("a")).get("b")); Assert.assertEquals(1, ((Map) server.getConfig().get("a")).get("c")); Assert.assertTrue(((Map) server.getConfig().get("environments")).isEmpty()); }
@Test public void test_closures_2() { String s = "a = 1\n" + "s = \"|${a}|\"\n" + "c = {a}\n" + "a=2"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(Integer.valueOf(2), config.get("a", 0)); Assert.assertEquals("|1|", config.get("s").toString()); }
@Test public void test_closures() { String s = "a = 1\n" + "s = \"|${{->a}}|\"\n" + "c = {a}\n" + "a=2"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(Integer.valueOf(2), config.get("a", 0)); Assert.assertEquals("|2|", config.get("s").toString()); }
@Override public <T> Dependency<T> resolve(ConfigValue annotation, Class<T> clazz) { Config config = Holders.getConfig(); String[] path = annotation.value().split("\\."); for (int i = 0; i < path.length - 1; i++) { config = config.config(path[i]); } return new SingletonDependency<>((T) config.get(path[path.length - 1])); } });
@Test public void test_5() { String s = "a { b = 'value'; c = 'value 2' }"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", ((Map) config.get("a")).get("b")); Assert.assertEquals("value 2", ((Map) config.get("a")).get("c")); }
@Test public void test_comment_4() { String s = "b = 'this // is not a comment'\n" + "c = '/*also just a string*/'"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("this // is not a comment", config.get("b", "")); Assert.assertEquals("/*also just a string*/", config.get("c", "")); }
@Test public void test_9() { String s = "a.b {\n" + "foo = 'bar'\n" + "}"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("bar", ((Map) ((Map) config.get("a")).get("b")).get("foo")); }
@Test public void test_imports() { String s = "import com.wizzardo.tools.evaluation.ConfigTest\n" + "a = new ConfigTest.TestImport()\n" + "a.i = 1" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertTrue(config.get("a") instanceof TestImport); Assert.assertEquals(1, config.get("a", new TestImport()).i); }
@Test public void test_profiles_5() throws IOException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InterruptedException { server.setEnvironment(Environment.TEST); server.start(); Assert.assertEquals("bar", server.getConfig().config("foo").get("v")); Assert.assertTrue(server.getConfig().get("bar") instanceof Config); Assert.assertTrue(((Config) server.getConfig().get("bar")).isEmpty()); }
@Test public void test_profiles_6() throws IOException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException, InterruptedException { server.setEnvironment(Environment.DEVELOPMENT); server.start(); Assert.assertEquals("foobar", server.getConfig().config("foo").get("v")); Assert.assertEquals("foobar", server.getConfig().config("bar").get("v").toString()); }
@Test public void test_4() { String s = "a { b { c = 'value'} }"; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals(1, config.size()); Assert.assertEquals("value", ((Map) ((Map) config.get("a")).get("b")).get("c")); }
@Test public void test_closure() { String s = "" + "closure = {'bar'}\n" + "foo = closure()" + ""; Expression expression = EvalTools.prepare(s); Config config = new Config(); expression.get(config); Assert.assertEquals("bar", config.get("foo")); }