Exemplo n.º 1
0
        // More type safe than base class's ParseJson
        // Parse json.
        // [1,2,3]  is a single column table, actually equivalent to:
        // [{Value : 1, Value: 2, Value :3 }]
        internal static TableValue TableFromJsonArray(JsonElement array)
        {
            Contract.Assert(array.ValueKind == JsonValueKind.Array);

            List <RecordValue> records = new List <RecordValue>();

            for (var i = 0; i < array.GetArrayLength(); ++i)
            {
                var element = array[i];
                var val     = GuaranteeRecord(FormulaValue.FromJson(element));

                records.Add(val);
            }

            // Constructor will handle both single-column table
            TableType type;

            if (records.Count == 0)
            {
                type = new TableType();
            }
            else
            {
                type = TableType.FromRecord((RecordType)GuaranteeRecord(records[0]).IRContext.ResultType);
            }
            return(new InMemoryTableValue(IRContext.NotInSource(type), records.Select(r => DValue <RecordValue> .Of(r))));
        }
Exemplo n.º 2
0
 // Used for converting an arbitrary .net enumerable.
 // The types of the elements my be heterogenous even after this call
 private static IEnumerable <RecordValue> TableToRecords(
     System.Collections.IEnumerable values,
     Type elementType)
 {
     foreach (var obj in values)
     {
         var formulaValue = GuaranteeRecord(FormulaValue.New(obj, elementType));
         yield return(formulaValue);
     }
 }
Exemplo n.º 3
0
 private FormulaValue PropagateFieldType(FormulaValue fieldValue, FormulaType fieldType)
 {
     if (fieldValue is RecordValue recordValue)
     {
         return(new InMemoryRecordValue(IRContext.NotInSource(fieldType), recordValue.Fields));
     }
     if (fieldValue is TableValue tableValue)
     {
         return(new InMemoryTableValue(IRContext.NotInSource(fieldType), tableValue.Rows));
     }
     return(fieldValue);
 }
Exemplo n.º 4
0
        private static RecordValue GuaranteeRecord(FormulaValue rawVal)
        {
            if (rawVal is RecordValue record)
            {
                return(record);
            }

            // Handle the single-column-table case.
            var defaultField = new NamedValue("Value", rawVal);

            RecordValue val = RecordFromFields(defaultField);

            return(val);
        }
Exemplo n.º 5
0
        public static TableValue TableFromRecords <T>(IEnumerable <T> values, TableType type)
        {
            var values2 = values.Select(v => GuaranteeRecord(FormulaValue.New(v, typeof(T))));

            return(new InMemoryTableValue(IRContext.NotInSource(type), values2.Select(r => DValue <RecordValue> .Of(r))));
        }
Exemplo n.º 6
0
 public NamedValue(string name, FormulaValue value)
 {
     Name  = name ?? throw new ArgumentNullException(nameof(name));
     Value = value;
 }