77
88import com .fasterxml .jackson .databind .JsonNode ;
99import com .fasterxml .jackson .databind .ObjectMapper ;
10+ import com .fasterxml .jackson .databind .node .ArrayNode ;
1011import com .fasterxml .jackson .databind .node .ObjectNode ;
1112import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
1213import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
2122 */
2223public abstract class KindWriter {
2324
24- protected final KubernetesWriter writer ;
25+ protected KubernetesWriter writer ;
2526
26- protected final ObjectNode json ;
27+ protected ObjectNode json ;
2728
2829 public KindWriter (String name ) throws Exception {
2930 this .writer = new KubernetesWriter ();
@@ -35,6 +36,11 @@ public KindWriter(String name, String namespace) throws Exception {
3536 this .json = toObjectNode (getTemplate (), new String [] {"#NAME#" , name , "#NAMESPACE#" , namespace });
3637 }
3738
39+ public KindWriter (String name , String namespace , String [] kvs ) throws Exception {
40+ this .writer = new KubernetesWriter ();
41+ this .json = toObjectNode (getTemplate (), kvs );
42+ }
43+
3844 public void stream (PrintStream ps ) throws Exception {
3945 ps .println (new YAMLMapper ().writeValueAsString (json ));
4046 }
@@ -47,6 +53,30 @@ public void yaml(String path) {
4753 writer .writeAsYaml (path , json );
4854 }
4955
56+ public ObjectNode getObjectValue (String key ) {
57+ if (!json .has (key )) {
58+ ObjectNode val = new ObjectMapper ().createObjectNode ();
59+ json .set (key , val );
60+ }
61+ return (ObjectNode ) json .get (key );
62+ }
63+
64+ public ObjectNode getObjectValue (ObjectNode node , String key ) {
65+ if (!node .has (key )) {
66+ ObjectNode val = new ObjectMapper ().createObjectNode ();
67+ node .set (key , val );
68+ }
69+ return (ObjectNode ) node .get (key );
70+ }
71+
72+ public ArrayNode getArrayValue (ObjectNode node , String key ) {
73+ if (!node .has (key )) {
74+ ArrayNode val = new ObjectMapper ().createArrayNode ();
75+ node .set (key , val );
76+ }
77+ return (ArrayNode ) node .get (key );
78+ }
79+
5080 public ObjectNode toObjectNode (String str , String [] list ) throws Exception {
5181 for (int i = 0 ; i < list .length ; i = i + 2 ) {
5282 str = str .replaceAll (list [i ], list [i + 1 ]);
@@ -60,5 +90,15 @@ public ObjectNode toObjectNode(String key, JsonNode json) throws Exception {
6090 return (ObjectNode ) new ObjectMapper (new YAMLFactory ()).readTree (val .toPrettyString ());
6191 }
6292
93+ public ObjectNode toObjectNode (Object obj ) throws Exception {
94+ return (ObjectNode ) new ObjectMapper (new YAMLFactory ()).readTree (
95+ new ObjectMapper ().writeValueAsString (obj ));
96+ }
97+
98+ public ObjectNode toObjectNode (String json ) throws Exception {
99+ return (ObjectNode ) new ObjectMapper (new YAMLFactory ()).readTree (json );
100+ }
101+
63102 public abstract String getTemplate ();
103+
64104}
0 commit comments