aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/datatype/arraydatatype.cpp
blob: 2097385559e8692aa3c346267891573ff5a33ae1 (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "arraydatatype.h"
#include <vespa/document/fieldvalue/arrayfieldvalue.h>
#include <vespa/vespalib/util/exceptions.h>
#include <ostream>

namespace document {

ArrayDataType::ArrayDataType(const DataType &nestedType, int32_t id)
    : CollectionDataType("Array<" + nestedType.getName() + ">", nestedType, id)
{
}

ArrayDataType::ArrayDataType(const DataType& nestedType)
    : CollectionDataType("Array<" + nestedType.getName() + ">", nestedType)
{
}

ArrayDataType::~ArrayDataType() = default;

FieldValue::UP
ArrayDataType::createFieldValue() const
{
    return std::make_unique<ArrayFieldValue>(*this);
}

void
ArrayDataType::print(std::ostream& out, bool verbose,
                     const std::string& indent) const
{
    out << "ArrayDataType(\n" << indent << "    ";
    getNestedType().print(out, verbose, indent + "    ");
    out << ", id " << getId() << ")";
}

bool
ArrayDataType::equals(const DataType& other) const noexcept
{
    if (this == &other) return true;
    return CollectionDataType::equals(other) && other.isArray();
}

void
ArrayDataType::onBuildFieldPath(FieldPath & path, vespalib::stringref remainFieldName) const
{
    if (remainFieldName[0] == '[') {
        size_t endPos = remainFieldName.find(']');
        if (endPos == vespalib::stringref::npos) {
            throw vespalib::IllegalArgumentException("Array subscript must be closed with ]");
        } else {
            int pos = endPos + 1;
            if (remainFieldName[pos] == '.') {
                pos++;
            }

            getNestedType().buildFieldPath(path, remainFieldName.substr(pos));

            if (remainFieldName[1] == '$') {
                path.insert(path.begin(), std::make_unique<FieldPathEntry>(getNestedType(), remainFieldName.substr(2, endPos - 2)));
            } else {
                // FIXME C++17 range-safe from_chars() instead of atoi()
                path.insert(path.begin(), std::make_unique<FieldPathEntry>(getNestedType(), atoi(remainFieldName.substr(1, endPos - 1).data())));
            }
        }
    } else {
        getNestedType().buildFieldPath(path, remainFieldName);
    }
}

} // document