Example #1
0
 public IfErrorFunction()
     : base("IfError", TexlStrings.AboutIfError, FunctionCategories.Logical, DType.Unknown, 0, 2, int.MaxValue)
 {
     ScopeInfo = new FunctionScopeInfo(this,
                                       iteratesOverScope: false,
                                       scopeType: DType.CreateRecord(
                                           new TypedName(ErrorType.ReifiedError(), new DName("FirstError")),
                                           new TypedName(ErrorType.ReifiedErrorTable(), new DName("AllErrors")),
                                           new TypedName(DType.ObjNull, new DName("ErrorResult"))),
                                       appliesToArgument: (i => i > 0 && (i % 2 == 1)));
 }
Example #2
0
        // Get the correct derived type
        internal static FormulaType Build(DType type)
        {
            switch (type.Kind)
            {
            case DKind.ObjNull: return(Blank);

            case DKind.Record: return(new RecordType(type));

            case DKind.Table: return(new TableType(type));

            case DKind.Number: return(Number);

            case DKind.String: return(String);

            case DKind.Boolean: return(Boolean);

            case DKind.Currency: return(Number);    // TODO: validate

            case DKind.Time: return(Time);

            case DKind.Date: return(Date);

            case DKind.DateTime: return(DateTime);

            case DKind.DateTimeNoTimeZone: return(DateTimeNoTimeZone);

            case DKind.OptionSetValue:
                var isBoolean = type.OptionSetInfo?.IsBooleanValued;
                return(isBoolean.HasValue && isBoolean.Value ? Boolean : OptionSetValue);

            // This isn't quite right, but once we're in the IR, an option set acts more like a record with optionsetvalue fields.
            case DKind.OptionSet:
                return(new RecordType(DType.CreateRecord(type.GetAllNames(DPath.Root))));

            default:
                throw new NotImplementedException($"Not implemented type: {type}");
            }
        }
Example #3
0
        public override bool CheckInvocation(TexlNode[] args, DType[] argTypes, IErrorContainer errors, out DType returnType, out Dictionary <TexlNode, DType> nodeToCoercedTypeMap)
        {
            Contracts.AssertValue(args);
            Contracts.AssertAllValues(args);
            Contracts.AssertValue(argTypes);
            Contracts.Assert(args.Length == argTypes.Length);
            Contracts.Assert(args.Length == 1);
            Contracts.AssertValue(errors);

            var reifiedError      = ErrorType.ReifiedError();
            var acceptedFields    = reifiedError.GetNames(DPath.Root);
            var requiredKindField = acceptedFields.Where(tn => tn.Name == "Kind").First();

            Contracts.Assert(requiredKindField.Type.IsEnum);
            var optionalFields = acceptedFields.Where(tn => tn.Name != "Kind");

            returnType           = DType.ObjNull;
            nodeToCoercedTypeMap = null;

            var argument     = args[0];
            var argumentType = argTypes[0];

            if (argumentType.Kind != DKind.Record && argumentType.Kind != DKind.Table)
            {
                errors.EnsureError(argument, TexlStrings.ErrBadType);
                return(false);
            }

            // We cache the whole name list regardless of path.
            var names = argumentType.GetNames(DPath.Root).ToArray();

            // First handle required fields (currently only 'Kind')
            if (!names.Any(field => field.Name == requiredKindField.Name))
            {
                // Kind is required, point it out to the maker, and specify the enumeration type.
                errors.EnsureError(
                    argument,
                    TexlStrings.ErrBadSchema_ExpectedType,
                    reifiedError.GetKindString());
                errors.Error(
                    argument,
                    TexlStrings.ErrColumnMissing_ColName_ExpectedType,
                    requiredKindField.Name.Value,
                    "ErrorKind");
                return(false);
            }

            var argumentKindType = names.First(tn => tn.Name == requiredKindField.Name).Type;

            if (!argumentKindType.CoercesTo(requiredKindField.Type))
            {
                errors.EnsureError(
                    argument,
                    TexlStrings.ErrBadSchema_ExpectedType,
                    reifiedError.GetKindString());
                errors.Error(
                    argument,
                    TexlStrings.ErrBadRecordFieldType_FieldName_ExpectedType,
                    requiredKindField.Name.Value,
                    "ErrorKind");
                return(false);
            }

            bool valid = true;

            var record = argument.AsRecord();

            foreach (var name in names)
            {
                if (!acceptedFields.Any(field => field.Name == name.Name))
                {
                    // If they have a record literal, we can position the errors for rejected fields.
                    if (record != null)
                    {
                        errors.EnsureError(record.Children.Where((_, i) => record.Ids[i].Name == name.Name).FirstOrDefault() ?? record, TexlStrings.ErrErrorIrrelevantField);
                    }
                    else
                    {
                        errors.EnsureError(argument, TexlStrings.ErrErrorIrrelevantField);
                    }
                    valid = false;
                }
            }

            bool matchedWithCoercion;
            bool typeValid;

            if (argumentType.Kind == DKind.Record)
            {
                // A record with the proper types for the fields that are specified.
                var expectedOptionalFieldsRecord = DType.CreateRecord(
                    acceptedFields.Where(field =>
                                         // Kind has already been handled before
                                         field.Name != "Kind" && names.Any(name => name.Name == field.Name)));

                typeValid = CheckType(argument, argumentType, expectedOptionalFieldsRecord, errors, true, out matchedWithCoercion);
            }
            else
            {
                // A table with the proper types for the fields that are specified.
                var expectedOptionalFieldsTable = DType.CreateTable(
                    acceptedFields.Where(field =>
                                         // Kind has already been handled before
                                         field.Name != "Kind" && names.Any(name => name.Name == field.Name)));
                typeValid = CheckType(argument, argumentType, expectedOptionalFieldsTable, errors, true, out matchedWithCoercion);
            }

            if (!typeValid)
            {
                errors.EnsureError(DocumentErrorSeverity.Severe, argument, TexlStrings.ErrTypeError);
                valid = false;
            }
            else if (matchedWithCoercion && valid)
            {
                var recordOrTableSchema   = acceptedFields.Where(field => names.Any(name => name.Name == field.Name));
                var expectedRecordOrTable = argumentType.Kind == DKind.Record ?
                                            DType.CreateRecord(recordOrTableSchema) :
                                            DType.CreateTable(recordOrTableSchema);
                CollectionUtils.Add(ref nodeToCoercedTypeMap, argument, expectedRecordOrTable);
            }

            return(valid);
        }
Example #4
0
 /// <returns>
 /// The <see cref="DType"/> of an error value
 /// </returns>
 public static DType ReifiedError() => DType.CreateRecord(ReifiedErrorSchema);