summaryrefslogtreecommitdiffstats
path: root/configgen/src/main/scala/com/yahoo/config/codegen/ConfigGenerator.scala
blob: 38306a035752cdf3cfa07ea8fbddc1bc8773bede (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.codegen


import com.yahoo.config.codegen.BuilderGenerator.getBuilder
import com.yahoo.config.codegen.JavaClassBuilder.Indentation
import com.yahoo.config.codegen.LeafCNode._
import com.yahoo.config.codegen.ReservedWords.{INTERNAL_PREFIX => InternalPrefix}

/**
 * @author gjoranv
 * @author tonytv
 */
// TODO: don't take indent as method param - the caller should indent
object ConfigGenerator {

  def generateContent(indent: String, node: InnerCNode, isOuter: Boolean = true): String = {
    val children: Array[CNode] = node.getChildren

    def generateCodeForChildren: String = {
      (children collect {
        case enum:  EnumLeaf    => getEnumCode(enum, "") + "\n"
        case inner: InnerCNode  => getInnerDefinition(inner, indent) + "\n"
      } ).mkString("\n")
    }

    def getInnerDefinition(inner: InnerCNode,  indent: String) = {
      <code>
        |{getClassDoc(inner, indent)}
        |{getClassDeclaration(inner)}
        |{generateContent(indent, inner, false)}
      </code>.text.stripMargin.trim + "\n}"
    }

    def getClassDeclaration(node: CNode): String = {
      "public final static class " + nodeClass(node)+ " extends InnerNode { " + "\n"
    }

    def getFieldDefinition(node: CNode): String = {
      node.getCommentBlock("//") + "private final " +
        (node match {
          case _: LeafCNode if node.isArray =>
            "LeafNodeVector<%s, %s> %s;".format(boxedDataType(node), nodeClass(node), node.getName)
          case _: InnerCNode if node.isArray =>
            "InnerNodeVector<%s> %s;".format(nodeClass(node), node.getName)
          case _ if node.isMap =>
            "Map<String, %s> %s;".format(nodeClass(node), node.getName)
          case _ =>
            "%s %s;".format(nodeClass(node), node.getName)
        })
    }

    def getStaticMethods = {
      if (node.isArray) getStaticMethodsForInnerArray(node) + "\n\n"
      else if (node.isMap) getStaticMethodsForInnerMap(node) + "\n\n"
      else ""
    }

    def getContainsFieldsFlaggedWithRestart(node: CNode): String = {
      if (isOuter) {
        """
          |private static boolean containsFieldsFlaggedWithRestart() {
          |  return %b;
          |}
        """.stripMargin.trim.format(node.needRestart) + "\n\n"
      } else ""
    }

    indentCode(indent,
      getBuilder(node) + "\n\n" +
        children.map(getFieldDefinition).mkString("\n") + "\n\n" +
        getConstructors(node) + "\n\n" +
        getAccessors(children) + "\n\n" +
        getGetChangesRequiringRestart(node) + "\n\n" +
        getContainsFieldsFlaggedWithRestart(node) +
        getStaticMethods +
        generateCodeForChildren
    )
  }

  private def getGetChangesRequiringRestart(node: InnerCNode): String = {
    def quotedComment(node: CNode): String = {
      node.getComment.replace("\n", "\\n").replace("\"", "\\\"")
    }

    def getComparison(node: CNode): String = node match {
      case inner: InnerCNode if inner.isArray =>
        <code>
          |  changes.compareArray(this.{inner.getName}, newConfig.{inner.getName}, "{inner.getName}", "{quotedComment(inner)}",
          |                      (a,b) -> (({nodeClass(inner)})a).getChangesRequiringRestart(({nodeClass(inner)})b));
        </code>.text.stripMargin.trim
      case inner: InnerCNode if inner.isMap =>
        <code>
          |  changes.compareMap(this.{inner.getName}, newConfig.{inner.getName}, "{inner.getName}", "{quotedComment(inner)}",
          |                    (a,b) -> (({nodeClass(inner)})a).getChangesRequiringRestart(({nodeClass(inner)})b));
        </code>.text.stripMargin.trim
      case inner: InnerCNode =>
        <code>
          |  changes.mergeChanges("{inner.getName}", this.{inner.getName}.getChangesRequiringRestart(newConfig.{inner.getName}));
        </code>.text.stripMargin.trim
      case node: CNode if node.isArray =>
        <code>
          |  changes.compareArray(this.{node.getName}, newConfig.{node.getName}, "{node.getName}", "{quotedComment(node)}",
          |                      (a,b) -> new ChangesRequiringRestart("{node.getName}").compare(a,b,"","{quotedComment(node)}"));
        </code>.text.stripMargin.trim
      case node: CNode if node.isMap =>
        <code>
          |  changes.compareMap(this.{node.getName}, newConfig.{node.getName}, "{node.getName}", "{quotedComment(node)}",
          |                    (a,b) -> new ChangesRequiringRestart("{node.getName}").compare(a,b,"","{quotedComment(node)}"));
        </code>.text.stripMargin.trim
      case node: CNode =>
        <code>
          |  changes.compare(this.{node.getName}, newConfig.{node.getName}, "{node.getName}", "{quotedComment(node)}");
        </code>.text.stripMargin.trim
    }

    val comparisons =
      for {
        c <- node.getChildren if c.needRestart
      } yield "\n  " + getComparison(c)

    <code>
      |private ChangesRequiringRestart getChangesRequiringRestart({nodeClass(node)} newConfig) {{
      |  ChangesRequiringRestart changes = new ChangesRequiringRestart("{node.getName}");{comparisons.mkString("")}
      |  return changes;
      |}}
    </code>.text.stripMargin.trim
  }


  private def scalarDefault(scalar: LeafCNode): String = {
    scalar match {
      case _ if scalar.getDefaultValue == null => ""
      case enumWithNullDefault: EnumLeaf if enumWithNullDefault.getDefaultValue.getValue == null => ""
      case enum: EnumLeaf => nodeClass(enum) + "." + enum.getDefaultValue.getStringRepresentation
      case long: LongLeaf => long.getDefaultValue.getStringRepresentation + "L"
      case double: DoubleLeaf => double.getDefaultValue.getStringRepresentation + "D"
      case _ => scalar.getDefaultValue.getStringRepresentation
    }
  }

  private def getConstructors(inner: InnerCNode) = {

    def assignFromBuilder(child: CNode) = {
      val name = child.getName
      val className = nodeClass(child)
      val dataType = boxedDataType(child)
      val isArray = child.isArray
      val isMap = child.isMap

      def assignIfInitialized(leaf: LeafCNode) = {
        <code>
          |{name} = (builder.{name} == null) ?
          |    new {className}({scalarDefault(leaf)}) : new {className}(builder.{name});
        </code>.text.stripMargin.trim
      }

      child match {
        case fileArray: FileLeaf if isArray =>
          name + " = LeafNodeVector.createFileNodeVector(builder."+ name +");"
        case pathArray: PathLeaf if isArray =>
          name + " = LeafNodeVector.createPathNodeVector(builder."+ name +");"
        case leafArray: LeafCNode if isArray =>
          name + " = new LeafNodeVector<>(builder."+ name +", new " + className + "());"
        case fileMap: LeafCNode if isMap && child.isInstanceOf[FileLeaf] =>
          name + " = LeafNodeMaps.asFileNodeMap(builder."+ name +");"
        case pathMap: LeafCNode if isMap && child.isInstanceOf[PathLeaf] =>
          name + " = LeafNodeMaps.asPathNodeMap(builder."+ name +");"
        case leafMap: LeafCNode if isMap =>
          name + " = LeafNodeMaps.asNodeMap(builder."+ name +", new " + className + "());"
        case innerArray: InnerCNode if isArray =>
          name + " = " + className + ".createVector(builder." + name + ");"
        case innerMap: InnerCNode if isMap =>
          name + " = " + className + ".createMap(builder." + name + ");"
        case struct: InnerCNode =>
          name + " = new " +  className + "(builder." + name + ", throwIfUninitialized);"
        case leaf: LeafCNode =>
          assignIfInitialized(leaf)
      }
    }

    // TODO: merge these two constructors into one when the config library uses builders to set values from payload.
    <code>
      |public {nodeClass(inner)}(Builder builder) {{
      |  this(builder, true);
      |}}
      |
      |private {nodeClass(inner)}(Builder builder, boolean throwIfUninitialized) {{
      |  if (throwIfUninitialized &amp;&amp; ! builder.{InternalPrefix}uninitialized.isEmpty())
      |    throw new IllegalArgumentException("The following builder parameters for " +
      |        "{inner.getFullName} must be initialized: " + builder.{InternalPrefix}uninitialized);
      |
      |{indentCode(Indentation, inner.getChildren.map(assignFromBuilder).mkString("\n"))}
      |}}
    </code>.text.stripMargin.trim
  }

  private def getAccessors(children: Array[CNode]): String = {

    def getAccessorCode(indent: String, node: CNode): String = {
      indentCode(indent,
        if (node.isArray)
          accessorsForArray(node)
        else if (node.isMap)
          accessorsForMap(node)
        else
          accessorForStructOrScalar(node))
    }

    def valueAccessor(node: CNode) = node match {
      case leaf: LeafCNode => ".value()"
      case inner           => ""
    }

    def listAccessor(node: CNode) = node match {
      case leaf: LeafCNode => "%s.asList()".format(leaf.getName)
      case inner           => inner.getName
    }

    def mapAccessor(node: CNode) = node match {
       case leaf: LeafCNode => "LeafNodeMaps.asValueMap(%s)".format(leaf.getName)
       case inner           => "Collections.unmodifiableMap(%s)".format(inner.getName)
     }

    def accessorsForArray(node: CNode): String = {
      val name = node.getName
      val fullName = node.getFullName
      <code>
        |/**
        | * @return {fullName}
        | */
        |public List&lt;{boxedDataType(node)}&gt; {name}() {{
        |  return {listAccessor(node)};
        |}}
        |
        |/**
        | * @param i the index of the value to return
        | * @return {fullName}
        | */
        |public {userDataType(node)} {name}(int i) {{
        |  return {name}.get(i){valueAccessor(node)};
        |}}
      </code>.text.stripMargin.trim
    }

    def accessorsForMap(node: CNode): String = {
      val name = node.getName
      val fullName = node.getFullName
      <code>
        |/**
        | * @return {fullName}
        | */
        |public Map&lt;String, {boxedDataType(node)}&gt; {name}() {{
        |  return {mapAccessor(node)};
        |}}
        |
        |/**
        | * @param key the key of the value to return
        | * @return {fullName}
        | */
        |public {userDataType(node)} {name}(String key) {{
        |  return {name}.get(key){valueAccessor(node)};
        |}}
      </code>.text.stripMargin.trim
    }

    def accessorForStructOrScalar(node: CNode): String = {
      <code>
        |/**
        | * @return {node.getFullName}
        | */
        |public {userDataType(node)} {node.getName}() {{
        |  return {node.getName}{valueAccessor(node)};
        |}}
      </code>.text.stripMargin.trim
    }

    val accessors =
      for {
        c <- children
        accessor = getAccessorCode("", c)
        if (accessor.length > 0)
      } yield (accessor + "\n")
    accessors.mkString("\n").trim
  }

  private def getStaticMethodsForInnerArray(inner: InnerCNode) = {
    """
      |private static InnerNodeVector<%s> createVector(List<Builder> builders) {
      |    List<%s> elems = new ArrayList<>();
      |    for (Builder b : builders) {
      |        elems.add(new %s(b));
      |    }
      |    return new InnerNodeVector<%s>(elems);
      |}
    """.stripMargin.format(List.fill(5)(nodeClass(inner)): _*).trim
  }

  private def getStaticMethodsForInnerMap(inner: InnerCNode) = {
    """
      |private static Map<String, %s> createMap(Map<String, Builder> builders) {
      |  Map<String, %s> ret = new LinkedHashMap<>();
      |  for(String key : builders.keySet()) {
      |    ret.put(key, new %s(builders.get(key)));
      |  }
      |  return Collections.unmodifiableMap(ret);
      |}
    """.stripMargin.format(List.fill(3)(nodeClass(inner)): _*).trim
  }

  private def getEnumCode(enum: EnumLeaf, indent: String): String = {

    def getEnumValues(enum: EnumLeaf): String = {
      val enumValues =
        for (value <- enum.getLegalValues) yield
          """  public final static Enum %s = Enum.%s;""".format(value, value)
      enumValues.mkString("\n")
    }

    // TODO: try to rewrite to xml
    val code =
      """
        |%s
        |public final static class %s extends EnumNode<%s> {

        |  public %s(){
        |    this.value = null;
        |  }

        |  public %s(Enum enumValue) {
        |    super(enumValue != null);
        |    this.value = enumValue;
        |  }

        |  public enum Enum {%s}
        |%s

        |  @Override
        |  protected boolean doSetValue(@NonNull String name) {
        |    try {
        |      value = Enum.valueOf(name);
        |      return true;
        |    } catch (IllegalArgumentException e) {
        |    }
        |    return false;
        |  }
        |}
        |"""
        .stripMargin.format(getClassDoc(enum, indent),
                            nodeClass(enum),
                            nodeClass(enum)+".Enum",
                            nodeClass(enum),
                            nodeClass(enum),
                            enum.getLegalValues.mkString(", "),
                            getEnumValues(enum))

    indentCode(indent, code).trim
  }

  def getClassDoc(node: CNode, indent: String): String = {
    val header = "/**\n" + " * This class represents " + node.getFullName
    val nodeComment = node.getCommentBlock(" *") match {
      case "" => ""
      case s => "\n *\n" + s.stripLineEnd   // TODO: strip trailing \n in CNode.getCommentBlock
    }
    header + nodeComment + "\n */"
  }

  def indentCode(indent: String, code: String): String = {
    val indentedLines =
      for (s <- code.split("\n", -1)) yield
        if (s.length() > 0) (indent + s) else s
    indentedLines.mkString("\n")
  }

  /**
   * @return the name of the class that is generated by this node.
   */
  def nodeClass(node: CNode): String = {
    node match {
      case emptyName: CNode if node.getName.length == 0 =>
        throw new CodegenRuntimeException("Node with empty name, under parent " + emptyName.getParent.getName)
      case root: InnerCNode if root.getParent == null => ConfiggenUtil.createClassName(root.getName)
      case b: BooleanLeaf   => "BooleanNode"
      case d: DoubleLeaf    => "DoubleNode"
      case f: FileLeaf      => "FileNode"
      case p: PathLeaf      => "PathNode"
      case i: IntegerLeaf   => "IntegerNode"
      case l: LongLeaf      => "LongNode"
      case r: ReferenceLeaf => "ReferenceNode"
      case s: StringLeaf    => "StringNode"
      case _ => node.getName.capitalize
    }
  }

  def userDataType(node: CNode): String = {
    node match {
      case inner: InnerCNode => nodeClass(node)
      case enum: EnumLeaf    => nodeClass(enum) + ".Enum"
      case b: BooleanLeaf    => "boolean"
      case d: DoubleLeaf     => "double"
      case f: FileLeaf       => "FileReference"
      case p: PathLeaf       => "Path"
      case i: IntegerLeaf    => "int"
      case l: LongLeaf       => "long"
      case s: StringLeaf     => "String"
    }
  }

  /**
   * @return the boxed java data type, e.g. Integer for int
   */
  def boxedDataType(node: CNode): String = {
    val rawType = userDataType(node)

    rawType match {
      case "int" => "Integer"
      case _ if rawType == rawType.toLowerCase => rawType.capitalize
      case _ => rawType
    }
  }

}