|
13 | 13 | package io.kubernetes.client.util;
|
14 | 14 |
|
15 | 15 | import com.google.common.reflect.ClassPath;
|
| 16 | +import io.kubernetes.client.custom.IntOrString; |
16 | 17 | import java.io.File;
|
17 | 18 | import java.io.FileReader;
|
18 | 19 | import java.io.IOException;
|
|
23 | 24 | import java.util.Set;
|
24 | 25 | import org.slf4j.Logger;
|
25 | 26 | import org.slf4j.LoggerFactory;
|
| 27 | +import org.yaml.snakeyaml.constructor.Constructor; |
| 28 | +import org.yaml.snakeyaml.nodes.Node; |
| 29 | +import org.yaml.snakeyaml.nodes.ScalarNode; |
26 | 30 |
|
27 | 31 | public class Yaml {
|
28 |
| - private static org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(); |
| 32 | + private static org.yaml.snakeyaml.Yaml yaml = |
| 33 | + new org.yaml.snakeyaml.Yaml(new CustomConstructor()); |
29 | 34 | private static Map<String, Class<?>> classes = new HashMap<>();
|
30 | 35 |
|
31 | 36 | static final Logger logger = LoggerFactory.getLogger(Yaml.class);
|
@@ -185,4 +190,23 @@ public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
|
185 | 190 | public static <T> T loadAs(Reader reader, Class<T> clazz) {
|
186 | 191 | return yaml.loadAs(reader, clazz);
|
187 | 192 | }
|
| 193 | + |
| 194 | + /** Defines constructor logic for custom types in this library. */ |
| 195 | + public static class CustomConstructor extends Constructor { |
| 196 | + @Override |
| 197 | + protected Object constructObject(Node node) { |
| 198 | + if (node.getType() == IntOrString.class) { |
| 199 | + return constructIntOrString((ScalarNode) node); |
| 200 | + } |
| 201 | + return super.constructObject(node); |
| 202 | + } |
| 203 | + |
| 204 | + private IntOrString constructIntOrString(ScalarNode node) { |
| 205 | + try { |
| 206 | + return new IntOrString(Integer.parseInt(node.getValue())); |
| 207 | + } catch (NumberFormatException err) { |
| 208 | + return new IntOrString(node.getValue()); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
188 | 212 | }
|
0 commit comments