Skip to content
Permalink
Browse files

feat: Add support for kebab casing when configuring dekorate via

config files.
  • Loading branch information...
iocanel committed Oct 2, 2019
1 parent a33e0d3 commit e7f912cd909b4b1ecd5f3123542ffdbff5a80b34
@@ -31,6 +31,15 @@ To reference the `httpActionPath` of the complex object `Probe` which is used to

dekorate.kubernetes.readinessProbe.httpActionPath=/some/path

## Casing

Please note that the casings for these properties are:

- camel case
- kebab

For example both of `dekorate.kubernetes.initContainers` and `dekorate.kubernetes.init-containers` are supported.

Below is a list of all the available options

## Kubernetes
@@ -33,6 +33,7 @@
import io.dekorate.kubernetes.config.ApplicationConfiguration;
import io.dekorate.kubernetes.config.Configuration;
import io.dekorate.utils.Generators;
import io.dekorate.utils.Maps;

/**
* The object that holds the state used by all processors.
@@ -121,7 +122,7 @@ public void feed(Map<String, Object> map) {
String newKey = annotationClass.getName();
Generators.populateArrays(annotationClass, (Map<String, Object>) value);
generatorMap.put(newKey, value);
generator.add(generatorMap);
generator.add(Maps.kebabToCamelCase(generatorMap));
}
}
}
@@ -150,6 +150,23 @@ public static void merge(Map<String, Object> existing, Map<String, Object> map)
}
}

/**
* Recursively convert all {@link Map} keys from kebab case to camel case.
* Recursively here means that if a value is a {@link Map} it will also be converted.
* @param The input string.
* @return The camel cased string.
*/
public static Map<String, Object> kebabToCamelCase(Map<String, Object> map) {
Map<String, Object> result = new HashMap<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
String newKey = Strings.kebabToCamelCase(key);
result.put(newKey, value instanceof Map ? kebabToCamelCase((Map<String, Object>) value) : value);
}
return result;
}

private static void unrollArrays(Map<String, Object> result) {
Map<String, Object> copy = new HashMap<>(result);
for (Map.Entry<String, Object> entry : copy.entrySet()) {
@@ -18,7 +18,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import io.dekorate.DekorateException;

@@ -57,12 +60,74 @@ public static String join(final Object[] array, final char separator) {
return buf.toString();
}

/**
* Uppercase the first character of the word and ignore the rest.
* Examples:
* null -> null
* "" -> ""
* "a" -> "A"
* "string" -> "String"
* "myString" -> "MyString"
* @param str The input string.
* @return The input string with the first character upper cased.
*/
public static String uppercaseFirst(String str) {
if (str == null) {
return null;
}

if (str.isEmpty()) {
return str;
}

if (str.length() == 1) {
return str.toUpperCase();
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}

/**
* Lowercase the first character of the word and ignore the rest.
* Examples:
* null -> null
* "" -> ""
* "A" -> "a"
* "String" -> "string"
* "MyString" -> "myString"
* @param str The input string.
* @return The input string with the first character lower cased.
*/

public static String lowerFirst(String str) {
if (str == null) {
return null;
}

if (str.isEmpty()) {
return str;
}

if (str.length() == 1) {
return str.toLowerCase();
}
return str.substring(0, 1).toLowerCase() + str.substring(1);
}

/**
* Convert kebab case to camel case.
* @param The input string.
* @return The camel cased string.
*/
public static String kebabToCamelCase(String str) {
final AtomicBoolean first = new AtomicBoolean(true);
return str != null && str.contains("-") ? Arrays.stream(str.split("-")).map(s -> first.getAndSet(false) ? s : Strings.uppercaseFirst(s)).collect(Collectors.joining()) : str;
}

public static String read(Path path) {
try {
return new String(Files.readAllBytes(path));
} catch (IOException e) {
throw DekorateException.launderThrowable(e);
}
}

}
@@ -40,6 +40,17 @@ public void testMapFromYAML() throws Exception {
}
}

@Test
public void testKebabToCamelCase() throws Exception {
try (InputStream is = MapsTest.class.getClassLoader().getResourceAsStream("kebab.yml")) {
Map<String, Object> map = Maps.fromYaml(is);
Map<String, Object> result = Maps.kebabToCamelCase(map);
Map<String, Object> kubernetes = (Map<String, Object>) result.get("kubernetes");
Map<String, Object> readinesProbe = (Map<String, Object>) kubernetes.get("readinesProbe");
assertEquals(10, readinesProbe.get("periodSeconds"));
}
}

private void checkFlattenMap(Map<String, Object> map) {
assertNotNull(map);
Map kubernetes = (Map) map.get("kubernetes");
@@ -0,0 +1,36 @@
/**
* Copyright 2018 The original authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/

package io.dekorate.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

class StringsTest {

@Test
public void shloudConvertToCamelCase() throws Exception {
assertNull(Strings.kebabToCamelCase(null));
assertEquals("", Strings.kebabToCamelCase(""));
assertEquals("my", Strings.kebabToCamelCase("my"));
assertEquals("myString", Strings.kebabToCamelCase("my-string"));
assertEquals("myOtherString", Strings.kebabToCamelCase("my-other-string"));
}
}

@@ -0,0 +1,4 @@
dekorate:
kubernetes:
readines-probe:
period-seconds: 10

0 comments on commit e7f912c

Please sign in to comment.
You can’t perform that action at this time.