public static boolean isAncestorOrSame(ConfigKeyPath descendant, ConfigKeyPath ancestor) { Preconditions.checkNotNull(descendant, "input can not be null"); Preconditions.checkNotNull(ancestor, "input can not be null"); if (descendant.equals(ancestor)) return true; if (descendant.isRootPath()) return false; return isAncestorOrSame(descendant.getParent(), ancestor); }
public List<ConfigKeyPath> getImportsRecursively(ConfigKeyPath configKey, Optional<Config> runtimeConfig) { return new ImportTraverser<>(key -> { if (key.isRootPath()) { return new LinkedList<>(); } List<ConfigKeyPath> imports = Lists.newArrayList(); imports.addAll(Lists.reverse(getOwnImports(key, runtimeConfig))); imports.add(key.getParent()); return imports; }, this.recursiveImportMap).traverseGraphRecursively(configKey); }
@Test public void testSystemPropertyResolution() { ConfigStore mockConfigStore = mock(ConfigStore.class, Mockito.RETURNS_SMART_NULLS); when(mockConfigStore.getCurrentVersion()).thenReturn(version); ConfigStoreTopologyInspector mockTopology = mock(ConfigStoreTopologyInspector.class, Mockito.RETURNS_SMART_NULLS); ConfigStoreBackedValueInspector valueInspector = new ConfigStoreBackedValueInspector(mockConfigStore, version, mockTopology); ConfigKeyPath testConfigKeyPath = SingleLinkedListConfigKeyPath.ROOT.createChild("a"); when(mockConfigStore.getOwnConfig(testConfigKeyPath.getParent(), version)).thenReturn(ConfigFactory.empty()); when(mockConfigStore.getOwnConfig(testConfigKeyPath, version)).thenReturn( ConfigFactory.parseString("configProp = ${?" + VALUE_INSPECTOR_SYS_PROP_KEY + "}")); Assert.assertEquals(valueInspector.getResolvedConfig(testConfigKeyPath).getString("configProp"), VALUE_INSPECTOR_SYS_PROP_VALUE); }
private Config getResolvedConfigRecursive(ConfigKeyPath configKey, Set<String> alreadyLoadedPaths, Optional<Config> runtimeConfig) { if (this.cs instanceof ConfigStoreWithResolution) { return ((ConfigStoreWithResolution) this.cs).getResolvedConfig(configKey, this.version); } if (!alreadyLoadedPaths.add(configKey.getAbsolutePathString())) { return ConfigFactory.empty(); } Config initialConfig = this.getOwnConfig(configKey); if (configKey.isRootPath()) { return initialConfig; } List<ConfigKeyPath> ownImports = this.topology.getOwnImports(configKey, runtimeConfig); // merge with other configs from imports if (ownImports != null) { for (ConfigKeyPath p : ownImports) { initialConfig = initialConfig.withFallback(this.getResolvedConfigRecursive(p, alreadyLoadedPaths, runtimeConfig)); } } // merge with configs from parent for Non root initialConfig = initialConfig .withFallback(this.getResolvedConfigRecursive(configKey.getParent(), alreadyLoadedPaths, runtimeConfig)); return initialConfig; }
@Test public void testResolveConfigOverridingInChild() { ConfigStore mockConfigStore = mock(ConfigStore.class, Mockito.RETURNS_SMART_NULLS); when(mockConfigStore.getCurrentVersion()).thenReturn(version); ConfigStoreTopologyInspector mockTopology = mock(ConfigStoreTopologyInspector.class, Mockito.RETURNS_SMART_NULLS); ConfigStoreBackedValueInspector valueInspector = new ConfigStoreBackedValueInspector(mockConfigStore, version, mockTopology); ConfigKeyPath keyPathA = SingleLinkedListConfigKeyPath.ROOT.createChild("a"); ConfigKeyPath keyPathA_Slash_B = keyPathA.createChild("b"); when(mockConfigStore.getOwnConfig(keyPathA.getParent(), version)).thenReturn(ConfigFactory.empty()); when(mockConfigStore.getOwnConfig(keyPathA, version)).thenReturn( ConfigFactory.parseString("key1 = value1InA \n key2 = ${key1}")); when(mockConfigStore.getOwnConfig(keyPathA_Slash_B, version)).thenReturn( ConfigFactory.parseString("key1 = value1InB")); Assert.assertEquals(valueInspector.getResolvedConfig(keyPathA_Slash_B).getString("key2"), "value1InB"); } }
public static boolean isAncestorOrSame(ConfigKeyPath descendant, ConfigKeyPath ancestor) { Preconditions.checkNotNull(descendant, "input can not be null"); Preconditions.checkNotNull(ancestor, "input can not be null"); if (descendant.equals(ancestor)) return true; if (descendant.isRootPath()) return false; return isAncestorOrSame(descendant.getParent(), ancestor); }
public List<ConfigKeyPath> getImportsRecursively(ConfigKeyPath configKey, Optional<Config> runtimeConfig) { return new ImportTraverser<>(key -> { if (key.isRootPath()) { return new LinkedList<>(); } List<ConfigKeyPath> imports = Lists.newArrayList(); imports.addAll(Lists.reverse(getOwnImports(key, runtimeConfig))); imports.add(key.getParent()); return imports; }, this.recursiveImportMap).traverseGraphRecursively(configKey); }
private Config getResolvedConfigRecursive(ConfigKeyPath configKey, Set<String> alreadyLoadedPaths, Optional<Config> runtimeConfig) { if (this.cs instanceof ConfigStoreWithResolution) { return ((ConfigStoreWithResolution) this.cs).getResolvedConfig(configKey, this.version); } if (!alreadyLoadedPaths.add(configKey.getAbsolutePathString())) { return ConfigFactory.empty(); } Config initialConfig = this.getOwnConfig(configKey); if (configKey.isRootPath()) { return initialConfig; } List<ConfigKeyPath> ownImports = this.topology.getOwnImports(configKey, runtimeConfig); // merge with other configs from imports if (ownImports != null) { for (ConfigKeyPath p : ownImports) { initialConfig = initialConfig.withFallback(this.getResolvedConfigRecursive(p, alreadyLoadedPaths, runtimeConfig)); } } // merge with configs from parent for Non root initialConfig = initialConfig .withFallback(this.getResolvedConfigRecursive(configKey.getParent(), alreadyLoadedPaths, runtimeConfig)); return initialConfig; }