aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/processing/ValidateStructTypeInheritance.java
blob: 9bf0c534f1929042f8c89a20a52bda9039786b55 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.schema.Schema;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.schema.RankProfileRegistry;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import com.yahoo.document.Field;
import com.yahoo.schema.document.SDDocumentType;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * @author arnej
 */
public class ValidateStructTypeInheritance extends Processor {

    public ValidateStructTypeInheritance(Schema schema, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(schema, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process(boolean validate, boolean documentsOnly) {
        if (!validate) return;
        verifyNoRedeclarations(schema.getDocument());
    }

    void fail(Field field, String message) {
        throw newProcessException(schema, field, message);
    }

    void verifyNoRedeclarations(SDDocumentType docType) {
        for (SDDocumentType type : docType.allTypes().values()) {
            if (type.isStruct()) {
                var inheritedTypes = new ArrayList<SDDocumentType>(type.getInheritedTypes());
                for (int i = 0; i < inheritedTypes.size(); i++) {
                    SDDocumentType inherit = inheritedTypes.get(i);
                    for (var extra : inherit.getInheritedTypes()) {
                        if (! inheritedTypes.contains(extra)) {
                            inheritedTypes.add(extra);
                        }
                    }
                }
                if (inheritedTypes.isEmpty()) continue;
                var seenFieldNames = new HashSet<>();
                for (var field : type.getDocumentType().contentStruct().getFieldsThisTypeOnly()) {
                    if (seenFieldNames.contains(field.getName())) {
                        // cannot happen?
                        fail(field, "struct "+type.getName()+" has multiple fields with same name: "+field.getName());
                    }
                    seenFieldNames.add(field.getName());
                }
                for (SDDocumentType inherit : inheritedTypes) {
                    if (inherit.isStruct()) {
                        for (var field : inherit.getDocumentType().contentStruct().getFieldsThisTypeOnly()) {
                            if (seenFieldNames.contains(field.getName())) {
                                fail(field, "struct "+type.getName()+" cannot inherit from "+inherit.getName()+" and redeclare field "+field.getName());
                            }
                            seenFieldNames.add(field.getName());
                        }
                    } else {
                        fail(new Field("no field"), "struct cannot inherit from non-struct "+inherit.getName()+" class "+inherit.getClass());
                    }
                }
            }
        }
    }

}