Skip to content

Commit 1433a40

Browse files
authored
Merge pull request #370 from kondapally1989/master
add constructDateTime to snakeYaml CustomConstructor
2 parents 4918869 + c08bdf0 commit 1433a40

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

util/src/main/java/io/kubernetes/client/util/Yaml.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
*/
1313
package io.kubernetes.client.util;
1414

1515
import com.google.common.reflect.ClassPath;
@@ -31,6 +31,8 @@
3131
import okio.ByteString;
3232
import org.apache.commons.lang3.tuple.MutablePair;
3333
import org.apache.commons.lang3.tuple.Pair;
34+
import org.joda.time.DateTime;
35+
import org.joda.time.DateTimeZone;
3436
import org.slf4j.Logger;
3537
import org.slf4j.LoggerFactory;
3638
import org.yaml.snakeyaml.DumperOptions;
@@ -324,6 +326,11 @@ protected Object constructObject(Node node) {
324326
if (node.getType() == byte[].class) {
325327
return constructByteArray((ScalarNode) node);
326328
}
329+
330+
if (node.getType() == org.joda.time.DateTime.class) {
331+
return constructDateTime((ScalarNode) node);
332+
}
333+
327334
return super.constructObject(node);
328335
}
329336

@@ -338,6 +345,10 @@ private IntOrString constructIntOrString(ScalarNode node) {
338345
private byte[] constructByteArray(ScalarNode node) {
339346
return ByteString.decodeBase64(node.getValue()).toByteArray();
340347
}
348+
349+
private Object constructDateTime(ScalarNode node) {
350+
return new DateTime(((ScalarNode) node).getValue(), DateTimeZone.UTC);
351+
}
341352
}
342353

343354
public static class CustomRepresenter extends Representer {

util/src/test/java/io/kubernetes/client/util/YamlTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
*/
1313
package io.kubernetes.client.util;
1414

1515
import static org.junit.Assert.*;
1616

1717
import com.google.common.io.Resources;
1818
import io.kubernetes.client.models.AppsV1beta1Deployment;
1919
import io.kubernetes.client.models.V1ObjectMeta;
20+
import io.kubernetes.client.models.V1Pod;
2021
import io.kubernetes.client.models.V1Secret;
2122
import io.kubernetes.client.models.V1Service;
2223
import io.kubernetes.client.models.V1ServicePort;
@@ -181,4 +182,27 @@ public void testLoadBytes() {
181182
assertNull("Unexpected exception: " + ex.toString(), ex);
182183
}
183184
}
185+
186+
@Test
187+
public void testDateTime() {
188+
try {
189+
String strInput =
190+
"apiVersion: v1\n"
191+
+ "kind: Pod\n"
192+
+ "metadata:\n"
193+
+ " creationTimestamp: 2018-09-06T15:12:24Z";
194+
195+
V1Pod pod = Yaml.loadAs(strInput, V1Pod.class);
196+
197+
assertEquals(
198+
"Incorrect value loaded for creationTimestamp",
199+
"2018-09-06T15:12:24.000Z",
200+
new String(
201+
pod.getMetadata().getCreationTimestamp().toString().getBytes(),
202+
StandardCharsets.UTF_8));
203+
204+
} catch (Exception ex) {
205+
assertNull("Unexpected exception: " + ex.toString(), ex);
206+
}
207+
}
184208
}

0 commit comments

Comments
 (0)