1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/** @ignore */
19export { Builder, BuilderOptions } from '../builder';
20export { BoolBuilder } from './bool';
21export { NullBuilder } from './null';
22export { DateBuilder, DateDayBuilder, DateMillisecondBuilder } from './date';
23export { DecimalBuilder } from './decimal';
24export { DictionaryBuilder } from './dictionary';
25export { FixedSizeBinaryBuilder } from './fixedsizebinary';
26export { FloatBuilder, Float16Builder, Float32Builder, Float64Builder } from './float';
27export { IntBuilder, Int8Builder, Int16Builder, Int32Builder, Int64Builder, Uint8Builder, Uint16Builder, Uint32Builder, Uint64Builder } from './int';
28export { TimeBuilder, TimeSecondBuilder, TimeMillisecondBuilder, TimeMicrosecondBuilder, TimeNanosecondBuilder } from './time';
29export { TimestampBuilder, TimestampSecondBuilder, TimestampMillisecondBuilder, TimestampMicrosecondBuilder, TimestampNanosecondBuilder } from './timestamp';
30export { IntervalBuilder, IntervalDayTimeBuilder, IntervalYearMonthBuilder } from './interval';
31export { Utf8Builder } from './utf8';
32export { BinaryBuilder } from './binary';
33export { ListBuilder } from './list';
34export { FixedSizeListBuilder } from './fixedsizelist';
35export { MapBuilder } from './map';
36export { StructBuilder } from './struct';
37export { UnionBuilder, SparseUnionBuilder, DenseUnionBuilder } from './union';
38
39import { Type } from '../enum';
40import { Field } from '../schema';
41import { DataType } from '../type';
42import { Utf8Builder } from './utf8';
43import { BuilderType as B } from '../interfaces';
44import { Builder, BuilderOptions } from '../builder';
45import { instance as setVisitor } from '../visitor/set';
46import { instance as getBuilderConstructor } from '../visitor/builderctor';
47
48/** @nocollapse */
49Builder.new = newBuilder;
50
51function newBuilder<T extends DataType = any, TNull = any>(options: BuilderOptions<T, TNull>): B<T, TNull> {
52
53    const type = options.type;
54    const builder = new (getBuilderConstructor.getVisitFn<T>(type)())(options) as Builder<T, TNull>;
55
56    if (type.children && type.children.length > 0) {
57
58        const children = options['children'] || [] as BuilderOptions[];
59        const defaultOptions = { 'nullValues': options['nullValues'] };
60        const getChildOptions = Array.isArray(children)
61            ? ((_: Field, i: number) => children[i] || defaultOptions)
62            : (({ name }: Field) => children[name] || defaultOptions);
63
64        type.children.forEach((field, index) => {
65            const { type } = field;
66            const opts = getChildOptions(field, index);
67            builder.children.push(newBuilder({ ...opts, type }));
68        });
69    }
70
71    return builder as B<T, TNull>;
72}
73
74(Object.keys(Type) as any[])
75    .map((T: any) => Type[T] as any)
76    .filter((T: any): T is Type => typeof T === 'number' && T !== Type.NONE)
77    .forEach((typeId) => {
78        const BuilderCtor = getBuilderConstructor.visit(typeId);
79        BuilderCtor.prototype._setValue = setVisitor.getVisitFn(typeId);
80    });
81
82(Utf8Builder.prototype as any)._setValue = setVisitor.visitBinary;
83