コード例 #1
0
        public ConvertTransform(IHostEnvironment env, Arguments args, IDataView input)
            : base(env, RegistrationName, env.CheckRef(args, nameof(args)).Column,
                   input, null)
        {
            Host.AssertNonEmpty(Infos);
            Host.Assert(Infos.Length == Utils.Size(args.Column));

            _exes = new ColInfoEx[Infos.Length];
            for (int i = 0; i < _exes.Length; i++)
            {
                DataKind kind;
                KeyRange range;
                var      col = args.Column[i];
                if (col.ResultType != null)
                {
                    kind  = col.ResultType.Value;
                    range = !string.IsNullOrEmpty(col.Range) ? KeyRange.Parse(col.Range) : col.KeyRange;
                }
                else if (col.KeyRange != null || !string.IsNullOrEmpty(col.Range))
                {
                    kind  = Infos[i].TypeSrc.IsKey ? Infos[i].TypeSrc.RawKind : DataKind.U4;
                    range = col.KeyRange ?? KeyRange.Parse(col.Range);
                }
                else if (args.ResultType != null)
                {
                    kind  = args.ResultType.Value;
                    range = !string.IsNullOrEmpty(args.Range) ? KeyRange.Parse(args.Range) : args.KeyRange;
                }
                else if (args.KeyRange != null || !string.IsNullOrEmpty(args.Range))
                {
                    kind  = Infos[i].TypeSrc.IsKey ? Infos[i].TypeSrc.RawKind : DataKind.U4;
                    range = args.KeyRange ?? KeyRange.Parse(args.Range);
                }
                else
                {
                    kind  = DataKind.Num;
                    range = null;
                }
                Host.CheckUserArg(Enum.IsDefined(typeof(DataKind), kind), nameof(args.ResultType));

                PrimitiveType itemType;
                if (!TryCreateEx(Host, Infos[i], kind, range, out itemType, out _exes[i]))
                {
                    throw Host.ExceptUserArg(nameof(args.Column),
                                             "source column '{0}' with item type '{1}' is not compatible with destination type '{2}'",
                                             input.Schema.GetColumnName(Infos[i].Source), Infos[i].TypeSrc.ItemType, itemType);
                }
            }
            SetMetadata();
        }
コード例 #2
0
        /// <summary>
        /// Attempt to parse the string into a data kind and (optionally) a key range. This method does not check whether
        /// the returned <see cref="DataKind"/> can really be made into a key with the specified <paramref name="keyRange"/>.
        /// </summary>
        /// <param name="str">The string to parse.</param>
        /// <param name="dataKind">The parsed data kind.</param>
        /// <param name="keyRange">The parsed key range, or null if there's no key specification.</param>
        /// <returns>Whether the parsing succeeded or not.</returns>
        public static bool TryParseDataKind(string str, out DataKind dataKind, out KeyRange keyRange)
        {
            Contracts.CheckValue(str, nameof(str));
            keyRange = null;
            dataKind = default(DataKind);

            int ich = str.IndexOf('[');

            if (ich >= 0)
            {
                if (str[str.Length - 1] != ']')
                {
                    return(false);
                }
                keyRange = KeyRange.Parse(str.Substring(ich + 1, str.Length - ich - 2));
                if (keyRange == null)
                {
                    return(false);
                }
                if (ich == 0)
                {
                    return(true);
                }
                str = str.Substring(0, ich);
            }

            DataKind kind;

            if (!Enum.TryParse <DataKind>(str, true, out kind))
            {
                return(false);
            }
            dataKind = kind;

            return(true);
        }