Search in sources :

Example 1 with CNode

use of com.yahoo.config.codegen.CNode in project vespa by vespa-engine.

the class ConfigDefinitionBuilder method addNode.

/**
 * @param def a ConfigDefinition object
 * @param node the node to be added to the config definition
 */
private static void addNode(ConfigDefinition def, CNode node) {
    String name = node.getName();
    if (node instanceof LeafCNode) {
        if (node.isArray) {
            // System.out.println("Adding array node " + name);
            String enumValues = null;
            String type = ((LeafCNode) node).getType();
            if ("enum".equals(type)) {
                enumValues = convertToEnumValueCommaSeparated(((LeafCNode.EnumLeaf) node).getLegalValues());
            }
            def.arrayDef(name).setTypeSpec(new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, enumValues, null, null));
        } else if (node.isMap) {
            // System.out.println("Adding leaf map node " + name);
            def.leafMapDef(name).setTypeSpec(new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, null, null, null));
        } else {
            // System.out.println("Adding basic node " + name);
            if (node instanceof LeafCNode.IntegerLeaf) {
                addNode(def, (LeafCNode.IntegerLeaf) node);
            } else if (node instanceof LeafCNode.LongLeaf) {
                addNode(def, (LeafCNode.LongLeaf) node);
            } else if (node instanceof LeafCNode.BooleanLeaf) {
                addNode(def, (LeafCNode.BooleanLeaf) node);
            } else if (node instanceof LeafCNode.DoubleLeaf) {
                addNode(def, (LeafCNode.DoubleLeaf) node);
            // Need to come before StringLeaf, since it is a subclass of StringLeaf
            } else if (node instanceof LeafCNode.ReferenceLeaf) {
                addNode(def, (LeafCNode.ReferenceLeaf) node);
            } else if (node instanceof LeafCNode.FileLeaf) {
                addNode(def, (LeafCNode.FileLeaf) node);
            } else if (node instanceof LeafCNode.PathLeaf) {
                addNode(def, (LeafCNode.PathLeaf) node);
            } else if (node instanceof LeafCNode.StringLeaf) {
                addNode(def, (LeafCNode.StringLeaf) node);
            } else if (node instanceof LeafCNode.EnumLeaf) {
                addNode(def, (LeafCNode.EnumLeaf) node);
            } else {
                System.err.println("Unknown node type for node with name " + name);
            }
        }
    } else {
        ConfigDefinition newDef;
        if (node.isArray) {
            if (node.getChildren() != null && node.getChildren().length > 0) {
                // System.out.println("\tAdding inner array node " + name);
                newDef = def.innerArrayDef(name);
                for (CNode childNode : node.getChildren()) {
                    // System.out.println("\tChild node " + childNode.getName());
                    addNode(newDef, childNode);
                }
            }
        } else if (node.isMap) {
            // System.out.println("Adding struct map node " + name);
            newDef = def.structMapDef(name);
            if (node.getChildren() != null && node.getChildren().length > 0) {
                for (CNode childNode : node.getChildren()) {
                    // System.out.println("\tChild node " + childNode.getName());
                    addNode(newDef, childNode);
                }
            }
        } else {
            // System.out.println("Adding struct node " + name);
            newDef = def.structDef(name);
            if (node.getChildren() != null && node.getChildren().length > 0) {
                for (CNode childNode : node.getChildren()) {
                    // System.out.println("\tChild node " + childNode.getName());
                    addNode(newDef, childNode);
                }
            }
        }
    }
}
Also used : LeafCNode(com.yahoo.config.codegen.LeafCNode) CNode(com.yahoo.config.codegen.CNode) LeafCNode(com.yahoo.config.codegen.LeafCNode)

Example 2 with CNode

use of com.yahoo.config.codegen.CNode in project vespa by vespa-engine.

the class ConfigFileFormat method field.

@Override
public void field(String fieldName, Inspector inspector) {
    try {
        Node parent = nodeStack.peek();
        CNode child = parent.node.getChild(fieldName);
        if (child == null) {
            // Skip this field to optimistic
            return;
        }
        if (!child.isArray && !child.isMap && child instanceof LeafCNode) {
            nodeStack.push(new Node(child));
            encode(inspector, child);
            nodeStack.pop();
        } else {
            encode(inspector, child);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : CNode(com.yahoo.config.codegen.CNode) LeafCNode(com.yahoo.config.codegen.LeafCNode) InnerCNode(com.yahoo.config.codegen.InnerCNode) LeafCNode(com.yahoo.config.codegen.LeafCNode) CNode(com.yahoo.config.codegen.CNode) LeafCNode(com.yahoo.config.codegen.LeafCNode) InnerCNode(com.yahoo.config.codegen.InnerCNode)

Example 3 with CNode

use of com.yahoo.config.codegen.CNode in project vespa by vespa-engine.

the class ConfigFileFormat method encodeObject.

private void encodeObject(Inspector inspector, CNode node) {
    if (!node.isArray && !node.isMap) {
        nodeStack.push(new Node(node));
        inspector.traverse(this);
        nodeStack.pop();
    } else {
        inspector.traverse(this);
    }
}
Also used : CNode(com.yahoo.config.codegen.CNode) LeafCNode(com.yahoo.config.codegen.LeafCNode) InnerCNode(com.yahoo.config.codegen.InnerCNode)

Example 4 with CNode

use of com.yahoo.config.codegen.CNode in project vespa by vespa-engine.

the class ConfigDefinitionBuilderTest method testCreateConfigDefinition.

@Test
public // TODO Test ranges
void testCreateConfigDefinition() throws IOException, InterruptedException {
    File defFile = new File(DEF_NAME);
    DefParser defParser = new DefParser(defFile.getName(), new FileReader(defFile));
    CNode root = defParser.getTree();
    ConfigDefinition def = ConfigDefinitionBuilder.createConfigDefinition(root);
    assertNotNull(def);
    assertThat(def.getBoolDefs().size(), is(2));
    assertNull(def.getBoolDefs().get("bool_val").getDefVal());
    assertThat(def.getBoolDefs().get("bool_with_def").getDefVal(), is(false));
    assertThat(def.getIntDefs().size(), is(2));
    assertNull(def.getIntDefs().get("int_val").getDefVal());
    assertThat(def.getIntDefs().get("int_with_def").getDefVal(), is(-545));
    assertThat(def.getLongDefs().size(), is(2));
    assertNull(def.getLongDefs().get("long_val").getDefVal());
    assertThat(def.getLongDefs().get("long_with_def").getDefVal(), is(-50000000000L));
    assertThat(def.getDoubleDefs().size(), is(2));
    assertNull(def.getDoubleDefs().get("double_val").getDefVal());
    assertThat(def.getDoubleDefs().get("double_with_def").getDefVal(), is(-6.43));
    assertThat(def.getEnumDefs().size(), is(3));
    assertTrue(def.getEnumDefs().containsKey("enum_val"));
    assertThat(def.getEnumDefs().get("enum_val").getVals().size(), is(3));
    assertThat(def.getEnumDefs().get("enum_val").getVals().get(0), is("FOO"));
    assertThat(def.getEnumDefs().get("enum_val").getVals().get(1), is("BAR"));
    assertThat(def.getEnumDefs().get("enum_val").getVals().get(2), is("FOOBAR"));
    assertTrue(def.getEnumDefs().containsKey("enumwithdef"));
    assertThat(def.getEnumDefs().get("enumwithdef").getDefVal(), is("BAR2"));
    assertTrue(def.getEnumDefs().containsKey("onechoice"));
    assertThat(def.getEnumDefs().get("onechoice").getDefVal(), is("ONLYFOO"));
    assertThat(def.getStringDefs().size(), is(2));
    // The return value is a String, so null if no default value
    assertNull(def.getStringDefs().get("string_val").getDefVal());
    assertThat(def.getStringDefs().get("stringwithdef").getDefVal(), is("foobar"));
    assertThat(def.getReferenceDefs().size(), is(2));
    assertNotNull(def.getReferenceDefs().get("refval"));
    assertThat(def.getReferenceDefs().get("refwithdef").getDefVal(), is(":parent:"));
    assertThat(def.getFileDefs().size(), is(1));
    assertNotNull(def.getFileDefs().get("fileVal"));
    assertThat(def.getArrayDefs().size(), is(9));
    assertNotNull(def.getArrayDefs().get("boolarr"));
    assertThat(def.getArrayDefs().get("boolarr").getTypeSpec().getType(), is("bool"));
    assertNotNull(def.getArrayDefs().get("enumarr"));
    assertThat(def.getArrayDefs().get("enumarr").getTypeSpec().getType(), is("enum"));
    assertThat(def.getArrayDefs().get("enumarr").getTypeSpec().getEnumVals().toString(), is("[ARRAY, VALUES]"));
    assertNotNull(def.getArrayDefs().get("refarr"));
    assertThat(def.getArrayDefs().get("refarr").getTypeSpec().getType(), is("reference"));
    assertNotNull(def.getArrayDefs().get("fileArr"));
    assertThat(def.getArrayDefs().get("fileArr").getTypeSpec().getType(), is("file"));
    assertThat(def.getStructDefs().size(), is(2));
    assertNotNull(def.getStructDefs().get("basicStruct"));
    assertThat(def.getStructDefs().get("basicStruct").getStringDefs().size(), is(1));
    assertThat(def.getStructDefs().get("basicStruct").getStringDefs().get("foo").getDefVal(), is("basic"));
    assertThat(def.getStructDefs().get("basicStruct").getIntDefs().size(), is(1));
    assertNull(def.getStructDefs().get("basicStruct").getIntDefs().get("bar").getDefVal());
    assertThat(def.getStructDefs().get("basicStruct").getArrayDefs().size(), is(1));
    assertThat(def.getStructDefs().get("basicStruct").getArrayDefs().get("intArr").getTypeSpec().getType(), is("int"));
    assertNotNull(def.getStructDefs().get("rootStruct"));
    assertNotNull(def.getStructDefs().get("rootStruct").getStructDefs().get("inner0"));
    assertNotNull(def.getStructDefs().get("rootStruct").getStructDefs().get("inner1"));
    assertThat(def.getStructDefs().get("rootStruct").getInnerArrayDefs().size(), is(1));
    assertNotNull(def.getStructDefs().get("rootStruct").getInnerArrayDefs().get("innerArr"));
    assertThat(def.getStructDefs().get("rootStruct").getInnerArrayDefs().get("innerArr").getStringDefs().size(), is(1));
    assertThat(def.getInnerArrayDefs().size(), is(1));
    assertNotNull(def.getInnerArrayDefs().get("myarray"));
    assertThat(def.getInnerArrayDefs().get("myarray").getIntDefs().get("intval").getDefVal(), is(14));
    assertThat(def.getInnerArrayDefs().get("myarray").getArrayDefs().size(), is(1));
    assertNotNull(def.getInnerArrayDefs().get("myarray").getArrayDefs().get("stringval"));
    assertThat(def.getInnerArrayDefs().get("myarray").getArrayDefs().get("stringval").getTypeSpec().getType(), is("string"));
    assertThat(def.getInnerArrayDefs().get("myarray").getEnumDefs().get("enumval").getDefVal(), is("TYPE"));
    assertNull(def.getInnerArrayDefs().get("myarray").getReferenceDefs().get("refval").getDefVal());
    assertThat(def.getInnerArrayDefs().get("myarray").getInnerArrayDefs().size(), is(1));
    assertThat(def.getInnerArrayDefs().get("myarray").getInnerArrayDefs().get("anotherarray").getIntDefs().get("foo").getDefVal(), is(-4));
    assertNull(def.getInnerArrayDefs().get("myarray").getStructDefs().get("myStruct").getIntDefs().get("a").getDefVal());
    assertThat(def.getInnerArrayDefs().get("myarray").getStructDefs().get("myStruct").getIntDefs().get("b").getDefVal(), is(2));
    // Maps
    assertEquals(def.getLeafMapDefs().size(), 4);
    assertEquals(def.getLeafMapDefs().get("intMap").getTypeSpec().getType(), "int");
    assertEquals(def.getLeafMapDefs().get("stringMap").getTypeSpec().getType(), "string");
    assertEquals(def.getStructMapDefs().size(), 1);
    assertEquals(def.getStructMapDefs().get("myStructMap").getIntDefs().get("myInt").getDefVal(), null);
    assertEquals(def.getStructMapDefs().get("myStructMap").getStringDefs().get("myString").getDefVal(), null);
    assertEquals(def.getStructMapDefs().get("myStructMap").getIntDefs().get("myIntDef").getDefVal(), (Integer) 56);
    assertEquals(def.getStructMapDefs().get("myStructMap").getStringDefs().get("myStringDef").getDefVal(), "g");
// Ranges
}
Also used : CNode(com.yahoo.config.codegen.CNode) FileReader(java.io.FileReader) DefParser(com.yahoo.config.codegen.DefParser) File(java.io.File) Test(org.junit.Test)

Aggregations

CNode (com.yahoo.config.codegen.CNode)4 LeafCNode (com.yahoo.config.codegen.LeafCNode)3 InnerCNode (com.yahoo.config.codegen.InnerCNode)2 DefParser (com.yahoo.config.codegen.DefParser)1 File (java.io.File)1 FileReader (java.io.FileReader)1 Test (org.junit.Test)1