summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/FieldOperationApplierForStructs.java
blob: 16bf37902f50bde8f579bdcdb145050bc5fb49cc (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.document.SDField;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Einar M R Rosenvinge
 */
public class FieldOperationApplierForStructs extends FieldOperationApplier {

    @Override
    public void process(SDDocumentType sdoc) {
        for (SDDocumentType type : sdoc.getAllTypes()) {
            if (type.isStruct()) {
                apply(type);
                copyFields(type, sdoc);
            }
        }
    }

    @SuppressWarnings("deprecation")
    private void copyFields(SDDocumentType structType, SDDocumentType sdoc) {
        //find all fields in OTHER types that have this type:
        List<SDDocumentType> list = new ArrayList<>();
        list.add(sdoc);
        list.addAll(sdoc.getTypes());
        for (SDDocumentType anyType : list) {
            Iterator<Field> fields = anyType.fieldIterator();
            while (fields.hasNext()) {
                SDField field = (SDField) fields.next();
                DataType structUsedByField = field.getFirstStructRecursive();
                if (structUsedByField == null) {
                    continue;
                }
                if (structUsedByField.getName().equals(structType.getName())) {
                    //this field is using this type!!
                    field.populateWithStructFields(sdoc, field.getName(), field.getDataType(), 0);
                    field.populateWithStructMatching(sdoc, field.getDataType(), field.getMatching());
                }
            }
        }
    }

}