aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/vespa/config/DefaultValueApplier.java
blob: 224f854a65c88be0be583e7071e5845c68e29f81 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config;

import com.yahoo.config.codegen.CNode;
import com.yahoo.config.codegen.InnerCNode;
import com.yahoo.config.codegen.LeafCNode;
import com.yahoo.slime.*;

/**
 * Applies default values of a given config definition to a slime payload.
 * TODO: Support giving correct type of default values
 *
 * @author Ulf Lilleengen
 */
public class DefaultValueApplier {

    public Slime applyDefaults(Slime slime, InnerCNode def) {
        applyDefaultsRecursive(slime.get(), def);
        return slime;
    }

    private void applyDefaultsRecursive(Cursor cursor, InnerCNode def) {
        if (def.isArray) {
            applyDefaultsToArray(cursor, def);
        } else if (def.isMap) {
            applyDefaultsToMap(cursor, def);
        } else {
            applyDefaultsToObject(cursor, def);
        }
    }

    private void applyDefaultsToMap(final Cursor cursor, final InnerCNode def) {
        cursor.traverse((ObjectTraverser) (name, inspector) -> applyDefaultsToObject(cursor.field(name), def));
    }

    private void applyDefaultsToArray(final Cursor cursor, final InnerCNode def) {
        cursor.traverse((ArrayTraverser) (idx, inspector) -> applyDefaultsToObject(cursor.entry(idx), def));
    }

    private void applyDefaultsToObject(Cursor cursor, InnerCNode def) {
        for (CNode child : def.getChildren()) {
            Cursor childCursor = cursor.field(child.getName());
            if (isLeafNode(child) && canApplyDefault(childCursor, child)) {
                applyDefaultToLeaf(cursor, child);
            } else if (isInnerNode(child)) {
                if (!childCursor.valid()) {
                    if (child.isArray) {
                        childCursor = cursor.setArray(child.getName());
                    } else {
                        childCursor = cursor.setObject(child.getName());
                    }
                }
                applyDefaultsRecursive(childCursor, (InnerCNode) child);
            }
        }
    }

    private boolean isInnerNode(CNode child) {
        return child instanceof InnerCNode;
    }

    private boolean isLeafNode(CNode child) {
        return child instanceof LeafCNode;
    }

    private void applyDefaultToLeaf(Cursor cursor, CNode child) {
        cursor.setString(child.getName(), ((LeafCNode) child).getDefaultValue().getValue());
    }

    private boolean canApplyDefault(Cursor cursor, CNode child) {
        return !cursor.valid() && ((LeafCNode) child).getDefaultValue() != null;
    }
}