Esempio n. 1
0
        /// <summary>
        /// Converts the serializable named value into a normal named value
        /// </summary>
        /// <param name="serializableNamedValue">The serializable named value</param>
        /// <returns>The value, but in types common to the rest of Taupo</returns>
        public NamedValue Convert(SerializableNamedValue serializableNamedValue)
        {
            ExceptionUtilities.CheckArgumentNotNull(serializableNamedValue, "serializableNamedValue");
            var value = serializableNamedValue.Value;

#if WIN8
            var dateTimeOffsetValue = value as Microsoft.Test.Taupo.Astoria.Contracts.WebServices.DataOracleService.Win8.DateTimeOffset;
            if (dateTimeOffsetValue != null)
            {
                value = new System.DateTimeOffset(dateTimeOffsetValue.DateTime.Year,
                                                  dateTimeOffsetValue.DateTime.Month,
                                                  dateTimeOffsetValue.DateTime.Day,
                                                  dateTimeOffsetValue.DateTime.Hour,
                                                  dateTimeOffsetValue.DateTime.Minute,
                                                  dateTimeOffsetValue.DateTime.Second,
                                                  TimeSpan.FromMinutes(dateTimeOffsetValue.OffsetMinutes));
            }
#endif
            var spatialValue = value as SerializableSpatialData;
            if (spatialValue != null)
            {
                ExceptionUtilities.CheckObjectNotNull(this.SpatialFormatter, "Cannot convert spatial data without SpatialFormatter dependency being set");

                var spatialTypeKind = SpatialUtilities.InferSpatialTypeKind(spatialValue.BaseTypeName);
                value = this.SpatialFormatter.Parse(spatialTypeKind, spatialValue.WellKnownTextRepresentation);
            }
            else if (value is SerializableEmptyData)
            {
                value = EmptyData.Value;
            }

            return(new NamedValue(serializableNamedValue.Name, value));
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to convert the value if it is spatial. Preserves any OData-specific fields.
        /// </summary>
        /// <param name="jsonObject">The json object that might be spatial.</param>
        /// <param name="value">The converted spatial value.</param>
        /// <returns>Whether the object was spatial and could be converted</returns>
        public bool TryConvertIfSpatial(JsonObject jsonObject, out PrimitiveValue value)
        {
            ExceptionUtilities.CheckArgumentNotNull(jsonObject, "jsonObject");

            string          edmTypeName;
            SpatialTypeKind?expectedType = null;

            if (TryExtractMetadataTypeName(jsonObject, out edmTypeName))
            {
                ExceptionUtilities.CheckObjectNotNull(this.PrimitiveDataTypeConverter, "Cannot infer clr type from edm type without converter");
                var clrType = this.PrimitiveDataTypeConverter.ToClrType(edmTypeName);
                if (clrType == null)
                {
                    // must not be primitive, let alone spatial
                    value = null;
                    return(false);
                }

                SpatialUtilities.TryInferSpatialTypeKind(clrType, out expectedType);
            }

            var dictionary = this.DictionaryConverter.Convert(jsonObject);

            object spatialInstance;

            if (this.GeoJsonFormatter.TryParse(dictionary, expectedType, out spatialInstance))
            {
                value = new PrimitiveValue(edmTypeName, spatialInstance);
                return(true);
            }

            value = null;
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Handles spatial primitives before delegating to the base type
        /// </summary>
        /// <param name="value">The primitive value to serialize</param>
        /// <param name="capitalizeIdentifiers">if set to <c>true</c> then identifiers for decimal, double, etc should be capitalized.</param>
        /// <returns>
        /// The wire representation of the primitive
        /// </returns>
        public string SerializePrimitive(object value, bool capitalizeIdentifiers)
        {
            if (value == null)
            {
                return(this.SerializeNull());
            }

            string wellKnownText = null;

            if (this.SpatialFormatter.IfValid(false, f => this.SpatialFormatter.TryConvert(value, out wellKnownText)))
            {
                var    kind = SpatialUtilities.InferSpatialTypeKind(value.GetType());
                string prefix;
                ExceptionUtilities.Assert(spatialLiteralPrefixMap.TryGetValue(kind, out prefix), "Could not find prefix for spatial type kind '{0}'", kind);
                return(prefix + '\'' + wellKnownText + '\'');
            }

            bool oldCapitalizeValue = this.shouldCapitalizeIdentifiers;

            try
            {
                this.shouldCapitalizeIdentifiers = capitalizeIdentifiers;
                return(base.SerializePrimitive(value));
            }
            finally
            {
                this.shouldCapitalizeIdentifiers = oldCapitalizeValue;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Returns whether the given type is a primitive type
        /// </summary>
        /// <param name="type">The type to check</param>
        /// <returns>True if it is a primitive type, false otherwise</returns>
        public override bool IsPrimitiveType(Type type)
        {
            SpatialTypeKind?kind;

            if (SpatialUtilities.TryInferSpatialTypeKind(type, out kind))
            {
                return(true);
            }

            return(base.IsPrimitiveType(type));
        }
            private static bool IsSpatialValue(PrimitiveValue primitiveValue)
            {
                // if the metadata is provided and is known to be spatial, expect an error
                var dataTypeAnnotation = primitiveValue.Annotations.OfType <DataTypeAnnotation>().SingleOrDefault();

                if (dataTypeAnnotation != null && dataTypeAnnotation.DataType is SpatialDataType)
                {
                    return(true);
                }

                // if the object is non-null and a spatial value, expect an error
                SpatialTypeKind?kind;

                if (primitiveValue.ClrValue.IfValid(false, o => SpatialUtilities.TryInferSpatialTypeKind(o.GetType(), out kind)))
                {
                    return(true);
                }

                return(false);
            }
Esempio n. 6
0
        /// <summary>
        /// Converts the serializable named value into a normal named value
        /// </summary>
        /// <param name="serializableNamedValue">The serializable named value</param>
        /// <returns>The value, but in types common to the rest of Taupo</returns>
        public NamedValue Convert(SerializableNamedValue serializableNamedValue)
        {
            ExceptionUtilities.CheckArgumentNotNull(serializableNamedValue, "serializableNamedValue");
            var value        = serializableNamedValue.Value;
            var spatialValue = value as SerializableSpatialData;

            if (spatialValue != null)
            {
                ExceptionUtilities.CheckObjectNotNull(this.SpatialFormatter, "Cannot convert spatial data without SpatialFormatter dependency being set");

                var spatialTypeKind = SpatialUtilities.InferSpatialTypeKind(spatialValue.BaseTypeName);
                value = this.SpatialFormatter.Parse(spatialTypeKind, spatialValue.WellKnownTextRepresentation);
            }
            else if (value is SerializableEmptyData)
            {
                value = EmptyData.Value;
            }

            return(new NamedValue(serializableNamedValue.Name, value));
        }
Esempio n. 7
0
        /// <summary>
        /// Determines whether the value is a geometry instanct that must be converted to geography.
        /// </summary>
        /// <param name="value">The value which might need to be converted.</param>
        /// <param name="expectedType">The expected type.</param>
        /// <returns>True if the value must be converted, otherwise false</returns>
        internal static bool MustConvertGeometryToGeography(object value, Type expectedType)
        {
            if (value == null)
            {
                return(false);
            }

            SpatialTypeKind?actualKind;

            if (!SpatialUtilities.TryInferSpatialTypeKind(value.GetType(), out actualKind))
            {
                return(false);
            }

            SpatialTypeKind?expectedKind;

            if (!SpatialUtilities.TryInferSpatialTypeKind(expectedType, out expectedKind))
            {
                return(false);
            }

            return(expectedKind == SpatialTypeKind.Geography && actualKind == SpatialTypeKind.Geometry);
        }
        /// <summary>
        /// Deserializes the element as either a complex, a primitive, or a null property, based on the content
        /// </summary>
        /// <param name="property">The xml to deserialize</param>
        /// <param name="typeNameFallback">TypeName to use instead of the one from the XElement[type] attribute</param>
        /// <returns>A property representing the given xml</returns>
        private PropertyInstance DeserializeProperty(XElement property, string typeNameFallback)
        {
            string propertyName = property.Name.LocalName;

            // get the type name
            string     typeNameFromPayload = null;
            XAttribute typeAttribute       = property.Attribute(MetadataType);

            if (typeAttribute != null)
            {
                typeNameFromPayload = typeAttribute.Value;
            }

            // set type to be fallback when typeattribute does not exist
            var typeNameForClrTypeLookup = typeNameFromPayload;

            if (typeNameForClrTypeLookup == null && !string.IsNullOrEmpty(typeNameFallback))
            {
                typeNameForClrTypeLookup = typeNameFallback;
            }

            // try to infer the clr type
            Type clrType = null;

            if (!string.IsNullOrEmpty(typeNameForClrTypeLookup))
            {
                ExceptionUtilities.CheckObjectNotNull(this.PrimitiveDataTypeConverter, "Cannot infer clr type from edm type without converter");
                clrType = this.PrimitiveDataTypeConverter.ToClrType(typeNameForClrTypeLookup);
            }

            PropertyInstance result;

            if (property.HasElements)
            {
                // must be complex, a multivalue, or spatial
                ExceptionUtilities.CheckObjectNotNull(this.SpatialFormatter, "Cannot safely deserialize element with children without spatial formatter.");

                // try to infer which spatial type hierarchy it is from the type name in the payload
                SpatialTypeKind?kind = null;
                if (clrType != null)
                {
                    SpatialUtilities.TryInferSpatialTypeKind(clrType, out kind);
                }

                object spatialInstance;
                if (this.SpatialFormatter.TryParse(property.Elements().First(), kind, out spatialInstance))
                {
                    ExceptionUtilities.Assert(property.Elements().Count() == 1, "Spatial property had more than 1 sub-element");
                    result = new PrimitiveProperty(propertyName, typeNameFromPayload, spatialInstance);
                }
                else if (property.Elements().All(e => e.Name == DataServicesElement))
                {
                    result = this.DeserializeCollectionProperty(property);
                }
                else
                {
                    result = new ComplexProperty(propertyName, this.DeserializeComplexInstance(property));
                }
            }
            else
            {
                // check for the null attribute
                bool       isNull          = false;
                XAttribute isNullAttribute = property.Attribute(MetadataNull);
                if (isNullAttribute != null)
                {
                    isNull = bool.Parse(isNullAttribute.Value);
                }

                // If its null and we can't tell whether it is primitive or complex, then return a null marker
                if (isNull && clrType == null)
                {
                    result = new NullPropertyInstance(propertyName, typeNameFromPayload);
                }
                else if (typeNameFromPayload != null && typeNameFromPayload.StartsWith(ODataConstants.BeginMultiValueTypeIdentifier, StringComparison.Ordinal))
                {
                    ExceptionUtilities.CheckObjectNotNull(this.PrimitiveDataTypeConverter, "Cannot infer clr type from edm type without converter");

                    string elementTypeName = ParseBagElementTypeName(typeNameFromPayload);
                    if (this.PrimitiveDataTypeConverter.ToClrType(elementTypeName) != null)
                    {
                        result = new PrimitiveMultiValueProperty(propertyName, new PrimitiveMultiValue(typeNameFromPayload, isNull));
                    }
                    else
                    {
                        result = new ComplexMultiValueProperty(propertyName, new ComplexMultiValue(typeNameFromPayload, isNull));
                    }
                }
                else
                {
                    object value;
                    if (isNull)
                    {
                        value = null;
                    }
                    else if (clrType != null)
                    {
                        ExceptionUtilities.CheckObjectNotNull(this.PrimitiveConverter, "PrimitiveConverter has not been set.");
                        value = this.PrimitiveConverter.DeserializePrimitive(property.Value, clrType);
                    }
                    else
                    {
                        value = property.Value;
                    }

                    result = new PrimitiveProperty(propertyName, typeNameFromPayload, value);
                }
            }

            AddXmlBaseAnnotation(result, property);

            return(result);
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            // Get storage account
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.Default.StorageConnectionString);

            // Create the clients
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

            BlobClient  = storageAccount.CreateCloudBlobClient();
            TableClient = storageAccount.CreateCloudTableClient();

            // Retrieve a reference to a queue
            WorkQueue = queueClient.GetQueueReference(Settings.Default.Queue);

            // Create the queue if it doesn't already exist
            WorkQueue.CreateIfNotExists();

            CloudOptions opt;

            try
            {
                opt = CliParser.Parse <CloudOptions>(args);

                if (opt.ForceCubical)
                {
                    int longestGridSide = Math.Max(Math.Max(opt.XSize, opt.YSize), opt.ZSize);
                    opt.XSize = opt.YSize = opt.ZSize = longestGridSide;

                    Console.WriteLine("Due to -ForceCubical grid size is now {0},{0},{0}", longestGridSide);
                }

                var options = new SlicingOptions
                {
                    OverrideMtl       = opt.MtlOverride,
                    GenerateEbo       = opt.Ebo,
                    GenerateOpenCtm   = opt.OpenCtm,
                    Debug             = opt.Debug,
                    GenerateObj       = opt.Obj,
                    Texture           = Path.GetFileName(opt.Texture),
                    Obj               = Path.GetFileName(opt.Input.First()),
                    WriteMtl          = opt.WriteMtl,
                    TextureScale      = opt.ScaleTexture,
                    TextureSliceX     = opt.TextureXSize,
                    TextureSliceY     = opt.TextureYSize,
                    ForceCubicalCubes = opt.ForceCubical,
                    CubeGrid          = new Vector3 {
                        X = opt.XSize, Y = opt.YSize, Z = opt.ZSize
                    }
                };

                string objPath;
                if (opt.Input.First().StartsWith("http"))
                {
                    objPath = opt.Input.First();
                }
                else
                {
                    objPath = StorageUtilities.UploadBlob(BlobClient, opt.Input.First(), Guid.NewGuid().ToString(), "processingdata");
                }


                string texPath;
                if (opt.Texture.StartsWith("http"))
                {
                    texPath = opt.Texture;
                }
                else
                {
                    texPath = StorageUtilities.UploadBlob(BlobClient, opt.Texture, Guid.NewGuid().ToString(), "processingdata");
                }

                options.CloudObjPath         = objPath;
                options.CloudTexturePath     = texPath;
                options.CloudResultContainer = opt.OutputContainer;
                options.CloudResultPath      = opt.OutputPath;

                // Get texture set size
                Vector2 setSize;
                if (!string.IsNullOrEmpty(options.Texture) && (options.TextureSliceX + options.TextureSliceY) > 2)
                {
                    setSize = new Vector2(options.TextureSliceX, options.TextureSliceY);
                }
                else
                {
                    setSize = new Vector2(1, 1);
                }

                // Queue work
                var setEntity = new SetEntity("Set", DateTime.UtcNow)
                {
                    ResultPath      = options.CloudResultPath,
                    ResultContainer = options.CloudResultContainer,
                    TextureTilesX   = setSize.X,
                    TextureTilesY   = setSize.Y
                };

                options.SetKey = setEntity.RowKey;

                StorageUtilities.InsertSetMetadata(TableClient, setEntity);

                SpatialUtilities.EnumerateSpace(setSize, (x, y) =>
                {
                    options.TextureTile = new Vector2(x, y);
                    string message      = JsonConvert.SerializeObject(options);
                    WorkQueue.AddMessage(new CloudQueueMessage(message));
                });
            }
            catch (ParserExit)
            {
                return;
            }
            catch (ParseException)
            {
                Console.WriteLine("usage: PyriteCli --help");
            }
        }