public void RowIdentifierFieldId_Is_Null_If_Metadata_Is_Null() { var resourceMetadata = new ResourceMetadata(mockClient); Assert.Null(resourceMetadata.Metadata); Assert.Null(resourceMetadata.RowIdentifierFieldId); }
public void New_Gets_Metadata_Identifier() { var metadata = new ResourceMetadata(mockClient) { Identifier = "identifier" }; var resource = new Resource<object>(metadata); Assert.AreSame(metadata.Identifier, resource.Identifier); }
public void New_Gets_Metadata_Host() { var metadata = new ResourceMetadata(mockClient); var resource = new Resource<object>(metadata); Assert.AreEqual(metadata.Host, resource.Host); }
public void New_Gets_Metadata_Client() { var metadata = new ResourceMetadata(mockClient); var resource = new Resource<object>(metadata); Assert.AreSame(metadata.Client, resource.Client); }
protected ScimUser() { /* 3.3.1.Resource Types * When adding a resource to a specific endpoint, the meta attribute * "resourceType" SHALL be set by the HTTP service provider to the * corresponding resource type for the endpoint. For example, a POST to * the endpoint "/Users" will set "resourceType" to "User", and * "/Groups" will set "resourceType" to "Group". */ Meta = new ResourceMetadata(ScimConstants.ResourceTypes.User); }
public ResourceMetadata Discover(ResourceModel resource) { var metadata = new ResourceMetadata {Uris = resource.Uris}; var filteredHandlers = resource.Handlers.Where(x => !x.Type.StaticType.IsAbstract); foreach (var handler in filteredHandlers) { IndexHandler(resource, handler, metadata); } return metadata; }
private bool ResourceInCache <TResource>(ResourceMetadata metadata, out TResource futureResource) where TResource : class, IResource, new() { if (Model.TryGetResource <TResource>(metadata, out var resource)) { futureResource = resource as TResource; return(true); } futureResource = null; return(false); }
public void RowIdentifierFieldId_Metadata_Can_Be_Read_If_Present() { long rowIdentifier = 12345; var resourceMetadata = new ResourceMetadata(mockClient) { Metadata = new Dictionary<string, dynamic>() { { "rowIdentifier", rowIdentifier } } }; Assert.That(resourceMetadata.RowIdentifierFieldId.HasValue); Assert.AreEqual(rowIdentifier, resourceMetadata.RowIdentifierFieldId.Value); }
public void ContactEmail_Can_Be_Read_If_Present() { string contactEmail = "*****@*****.**"; var resourceMetadata = new ResourceMetadata(mockClient) { PrivateMetadata = new Dictionary <string, dynamic>() { { "contactEmail", contactEmail } } }; Assert.AreEqual(contactEmail, resourceMetadata.ContactEmail); }
public bool TryGetMetadata(Guid id, out ResourceMetadata metadata) { var metadataPathname = $"{MakeLocalRootName(id)}.json"; if (!File.Exists(metadataPathname)) { metadata = null; return(false); } var json = File.ReadAllText(metadataPathname); metadata = JsonConvert.DeserializeObject <ResourceMetadata>(json); return(true); }
public void New_Gets_Metadata_Columns() { var metadata = new ResourceMetadata(mockClient) { Columns = new[] { new ResourceColumn() { Name = "column1" }, new ResourceColumn() { Name = "column2" }, new ResourceColumn() { Name = "column3" } } }; var resource = new Resource<object>(metadata); Assert.AreSame(metadata.Columns, resource.Columns); }
public void RowIdentifierFieldId_Metadata_Can_Be_Read_If_Present() { long rowIdentifier = 12345; var resourceMetadata = new ResourceMetadata(mockClient) { Metadata = new Dictionary <string, dynamic>() { { "rowIdentifier", rowIdentifier } } }; Assert.That(resourceMetadata.RowIdentifierFieldId.HasValue); Assert.AreEqual(rowIdentifier, resourceMetadata.RowIdentifierFieldId.Value); }
public async Task <MetadataResponse> GetMetadata(IEnumerable <string> metaNames) { try { ResourceMetadata metadata = new ResourceMetadata(this, metaNames); return(await metadata.Get()); } catch (Exception e) { return(new MetadataResponse() { Url = Url, StatusCode = HttpStatusCode.BadRequest, Metadata = null, ErrorMessage = e.Message }); } }
private void UpdateCraftRecipeIcons(RectTransform recipePanel, ObjectMetadata item) { //ObjectMetadata objectData = metadataManager.GetObjectMetadataWithObjectId(item.objectId); RecipeMetadata selectedRecipe = MetadataLoader.Instance.GetRecipeMetadataById(item.recipeId); Image[] recipeIcons = recipePanel.GetComponentsInChildren <Image>(); for (int i = 0; i < recipeIcons.Length; i++) { ResourceMetadata resource = metadataManager.GetResourceMetadataByType(selectedRecipe.resources[i]); if (resource != null) { recipeIcons[i].sprite = resource.icon; } } }
public async Task <HttpResponseMessage> UpsertResourceMetadata(ResourceMetadata metadata) { try { await GraphManager.UpsertResourceMetadataAsync(metadata); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (Exception ex) { Trace.TraceWarning("Failed to upsert resource metadata."); Trace.TraceError(ex.Message); Trace.TraceError(ex.StackTrace); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public void Null_UnixDates_Return_Null_DateTimes() { var resourceMetadata = new ResourceMetadata(mockClient); Assert.Null(resourceMetadata.CreationDateUnix); Assert.Null(resourceMetadata.CreationDate); Assert.Null(resourceMetadata.PublishedDateUnix); Assert.Null(resourceMetadata.PublishedDate); Assert.Null(resourceMetadata.RowsLastUpdatedUnix); Assert.Null(resourceMetadata.RowsLastUpdated); Assert.Null(resourceMetadata.SchemaLastUpdatedUnix); Assert.Null(resourceMetadata.SchemaLastUpdated); }
public void Null_UnixDates_Return_Null_DateTimes() { var resourceMetadata = new ResourceMetadata(mockClient); Assert.Null(resourceMetadata.CreationDateUnix); Assert.Null(resourceMetadata.CreationDate); Assert.Null(resourceMetadata.PublishedDateUnix); Assert.Null(resourceMetadata.PublishedDate); Assert.Null(resourceMetadata.RowsLastUpdatedUnix); Assert.Null(resourceMetadata.RowsLastUpdated); Assert.Null(resourceMetadata.SchemaLastUpdatedUnix); Assert.Null(resourceMetadata.SchemaLastUpdated); }
public async Task <HttpResponseMessage> GetResourceMetadata(string resourceUriString) { try { ResourceMetadata metadata = await GraphManager.GetResourceMetadataAsync(resourceUriString); return(Request.CreateResponse <ResourceMetadata>(HttpStatusCode.OK, metadata)); } catch (Exception ex) { Trace.TraceWarning("Failed to get resource metadata."); Trace.TraceError(ex.Message); Trace.TraceError(ex.StackTrace); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public IEnumerable <LanguageExpression> GetResourceNameSegments(ResourceMetadata resource) { // TODO move this into az extension var typeReference = resource.TypeReference; var ancestors = this.context.SemanticModel.ResourceAncestors.GetAncestors(resource); var nameSyntax = resource.NameSyntax; var nameExpression = ConvertExpression(nameSyntax); var typesAfterProvider = typeReference.TypeSegments.Skip(1).ToImmutableArray(); if (ancestors.Length > 0) { var firstAncestorNameLength = typesAfterProvider.Length - ancestors.Length; var resourceName = ConvertExpression(resource.NameSyntax); var parentNames = ancestors.SelectMany((x, i) => { var nameSyntax = x.Resource.NameSyntax; var nameExpression = CreateConverterForIndexReplacement(nameSyntax, x.IndexExpression, x.Resource.Symbol.NameSyntax) .ConvertExpression(nameSyntax); if (i == 0 && firstAncestorNameLength > 1) { return(Enumerable.Range(0, firstAncestorNameLength).Select( (_, i) => AppendProperties( CreateFunction("split", nameExpression, new JTokenExpression("/")), new JTokenExpression(i)))); } return(nameExpression.AsEnumerable()); }); return(parentNames.Concat(resourceName.AsEnumerable())); } if (typesAfterProvider.Length == 1) { return(nameExpression.AsEnumerable()); } return(typesAfterProvider.Select( (type, i) => AppendProperties( CreateFunction("split", nameExpression, new JTokenExpression("/")), new JTokenExpression(i)))); }
internal static byte[] LoadImageBytes(string path, ResourceMetadata metadata) { byte[] data; var ext = Path.GetExtension(path)?.ToLowerInvariant(); switch (ext) { case ".png": case ".bmp": case ".jpg": case ".jpeg": switch (metadata.Compress) { case PsbCompressType.RL: data = RL.CompressImageFile(path, metadata.PixelFormat); break; case PsbCompressType.ByName when metadata.Name != null && metadata.Name.EndsWith(ext, true, null): data = File.ReadAllBytes(path); break; case PsbCompressType.Tlg: //TODO: TLG encode default: data = RL.GetPixelBytesFromImageFile(path, metadata.PixelFormat); break; } break; case ".rl": data = metadata.Compress == PsbCompressType.RL ? File.ReadAllBytes(path) : RL.Uncompress(File.ReadAllBytes(path)); break; case ".raw": data = metadata.Compress == PsbCompressType.RL ? RL.Compress(File.ReadAllBytes(path)) : File.ReadAllBytes(path); break; case ".tlg": //TODO: tlg encode default: //For `.bin`, you have to handle by yourself data = File.ReadAllBytes(path); break; } return(data); }
private LanguageExpression?ConvertResourcePropertyAccess(ResourceMetadata resource, SyntaxBase?indexExpression, string propertyName) { // special cases for certain resource property access. if we recurse normally, we'll end up // generating statements like reference(resourceId(...)).id which are not accepted by ARM switch ((propertyName, context.Settings.EnableSymbolicNames)) { case ("id", true): case ("name", true): case ("type", true): case ("apiVersion", true): var symbolExpression = GenerateSymbolicReference(resource.Symbol.Name, indexExpression); return(AppendProperties( CreateFunction("resourceInfo", symbolExpression), new JTokenExpression(propertyName))); case ("id", false): // the ID is dependent on the name expression which could involve locals in case of a resource collection return(GetFullyQualifiedResourceId(resource)); case ("name", false): // the name is dependent on the name expression which could involve locals in case of a resource collection // Note that we don't want to return the fully-qualified resource name in the case of name property access. // we should return whatever the user has set as the value of the 'name' property for a predictable user experience. return(ConvertExpression(resource.NameSyntax)); case ("type", false): return(new JTokenExpression(resource.TypeReference.FullyQualifiedType)); case ("apiVersion", false): return(new JTokenExpression(resource.TypeReference.ApiVersion)); case ("properties", _): // use the reference() overload without "full" to generate a shorter expression // this is dependent on the name expression which could involve locals in case of a resource collection return(GetReferenceExpression(resource, indexExpression, false)); default: return(null); } }
private IResource MapResource(IParser reader, Func <IParser, Type, object> nestedObjectDeserializer, CommentMetadata comment) { IResource result = null; string kind = null; ResourceMetadata metadata = null; if (reader.Accept <MappingStart>()) { reader.MoveNext(); while (!reader.Accept <MappingEnd>()) { // Read kind var propertyName = reader.Allow <Scalar>().Value; if (propertyName == "kind") { kind = reader.Allow <Scalar>().Value; } else if (propertyName == "metadata") { if (!TryMetadata(reader, nestedObjectDeserializer, out metadata)) { reader.SkipThisAndNestedEvents(); } } else if (propertyName == "spec" && kind != null) { if (!TryResource(kind, reader, nestedObjectDeserializer, metadata, comment, out IResource resource)) { reader.SkipThisAndNestedEvents(); } result = resource; } else { reader.SkipThisAndNestedEvents(); } } reader.MoveNext(); } return(result); }
public FunctionExpression GetReferenceExpression(ResourceMetadata resource, SyntaxBase?indexExpression, bool full) { var referenceExpression = context.Settings.EnableSymbolicNames ? GenerateSymbolicReference(resource.Symbol.Name, indexExpression) : GetFullyQualifiedResourceId(resource); if (!resource.IsAzResource) { // For an extensible resource, always generate a 'reference' statement. // User-defined properties appear inside "properties", so use a non-full reference. return(CreateFunction( "reference", referenceExpression)); } // full gives access to top-level resource properties, but generates a longer statement if (full) { var apiVersion = resource.TypeReference.ApiVersion ?? throw new InvalidOperationException($"Expected resource type {resource.TypeReference.FormatName()} to contain version"); return(CreateFunction( "reference", referenceExpression, new JTokenExpression(apiVersion), new JTokenExpression("full"))); } if (resource.IsExistingResource && !context.Settings.EnableSymbolicNames) { var apiVersion = resource.TypeReference.ApiVersion ?? throw new InvalidOperationException($"Expected resource type {resource.TypeReference.FormatName()} to contain version"); // we must include an API version for an existing resource, because it cannot be inferred from any deployed template resource return(CreateFunction( "reference", referenceExpression, new JTokenExpression(apiVersion))); } return(CreateFunction( "reference", referenceExpression)); }
public SqlBulkCopyDataWrapper CreateSqlBulkCopyDataWrapper(ImportResource resource) { var resourceMetadata = new ResourceMetadata( resource.Resource.CompartmentIndices, resource.Resource.SearchIndices?.ToLookup(e => _searchParameterTypeMap.GetSearchValueType(e)), resource.Resource.LastModifiedClaims); short resourceTypeId = _model.GetResourceTypeId(resource.Resource.ResourceTypeName); resource.CompressedStream.Seek(0, 0); return(new SqlBulkCopyDataWrapper() { Metadata = resourceMetadata, ResourceTypeId = resourceTypeId, Resource = resource.Resource, ResourceSurrogateId = resource.Id, Index = resource.Index, BulkImportResource = resource.ExtractBulkImportResourceTypeV1Row(resourceTypeId), }); }
public List <ResourceMetadata> GetResourceMetadata() { if (resourceList != null) { return(resourceList); } resourceList = new List <ResourceMetadata>(); SqliteCommand cmd = sqlHelper.CreateTextCommand("SELECT * FROM resource_metadata"); SqliteDataReader reader = sqlHelper.ExecuteCommand(cmd); while (reader.Read()) { ResourceMetadata row = new ResourceMetadata(reader); resourceList.Add(row); } sqlHelper.CloseResultReader(reader); return(resourceList); }
private IResource MapResource(IParser reader, Func <IParser, Type, object> nestedObjectDeserializer, CommentMetadata comment) { IResource result = null; string apiVersion = null; string kind = null; ResourceMetadata metadata = null; if (reader.TryConsume <MappingStart>(out _)) { while (reader.TryConsume(out Scalar scalar)) { // Read apiVersion if (TryApiVersion(reader, scalar, out string apiVersionValue)) { apiVersion = apiVersionValue; } // Read kind else if (TryKind(reader, scalar, out string kindValue)) { kind = kindValue; } // Read metadata else if (TryMetadata(reader, scalar, nestedObjectDeserializer, out ResourceMetadata metadataValue)) { metadata = metadataValue; } // Read spec else if (apiVersion != null && kind != null && TrySpec(reader, scalar, apiVersion, kind, nestedObjectDeserializer, metadata, comment, out IResource resource)) { result = resource; } else { reader.SkipThisAndNestedEvents(); } } reader.Require <MappingEnd>(); reader.MoveNext(); } return(result); }
private void IndexHandler(ResourceModel resource, HandlerModel handler, ResourceMetadata metadata) { var exclusions = new List<string> { "ToString", "GetType", "GetHashCode", "Equals" }; foreach (var uri in metadata.Uris) { var candidateMethods = handler.Type.StaticType.GetMethods() .Where(x => x.IsPublic && !exclusions.Contains(x.Name) && !x.IsSpecialName); foreach (var publicMethod in candidateMethods) { var operation = new OperationMetadata(uri, resource.ResourceKey as IType); if (DiscoveryRules.All(x => x.Discover(publicMethod, operation))) { metadata.Add(operation); operation.Group = _grouper.Group(resource, uri, operation); } } } }
public virtual IEnumerable <TRow> GenerateRows(IReadOnlyList <ResourceWrapper> input) { EnsureInitialized(); for (var index = 0; index < input.Count; index++) { ResourceWrapper resource = input[index]; var resourceMetadata = new ResourceMetadata( resource.CompartmentIndices, resource.SearchIndices?.ToLookup(e => _searchParameterTypeMap.GetSearchValueType(e)), resource.LastModifiedClaims); foreach (SearchIndexEntry v in resourceMetadata.GetSearchIndexEntriesByType(typeof(TSearchValue))) { short searchParamId = Model.GetSearchParamId(v.SearchParameter.Url); if (!_isConvertSearchValueOverridden) { // save an array allocation if (TryGenerateRow(index, searchParamId, (TSearchValue)v.Value, out TRow row)) { yield return(row); } } else { foreach (var searchValue in ConvertSearchValue(v)) { if (TryGenerateRow(index, searchParamId, searchValue, out TRow row)) { yield return(row); } } } } } }
public IEnumerable <BulkResourceWriteClaimTableTypeV1Row> GenerateRows(IReadOnlyList <ResourceWrapper> resources) { for (var index = 0; index < resources.Count; index++) { ResourceWrapper resource = resources[index]; var resourceMetadata = new ResourceMetadata( resource.CompartmentIndices, resource.SearchIndices?.ToLookup(e => _searchParameterTypeMap.GetSearchValueType(e)), resource.LastModifiedClaims); IReadOnlyCollection <KeyValuePair <string, string> > writeClaims = resourceMetadata.WriteClaims; if (writeClaims == null) { yield break; } foreach (var claim in writeClaims) { yield return(new BulkResourceWriteClaimTableTypeV1Row(index, _model.GetClaimTypeId(claim.Key), claim.Value)); } } }
public void UnixDates_Can_Convert_To_Local_DateTimes() { DateTime now = DateTime.Now; double unixTimestamp = (now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; var resourceMetadata = new ResourceMetadata(mockClient) { CreationDateUnix = unixTimestamp, PublishedDateUnix = unixTimestamp, RowsLastUpdatedUnix = unixTimestamp, SchemaLastUpdatedUnix = unixTimestamp }; Assert.That(resourceMetadata.CreationDate.HasValue); Assert.That(resourceMetadata.PublishedDate.HasValue); Assert.That(resourceMetadata.RowsLastUpdated.HasValue); Assert.That(resourceMetadata.SchemaLastUpdated.HasValue); //allow a 1 millisecond buffer Assert.That(resourceMetadata.CreationDate.Value, Is.EqualTo(now).Within(1).Milliseconds); Assert.That(resourceMetadata.PublishedDate.Value, Is.EqualTo(now).Within(1).Milliseconds); Assert.That(resourceMetadata.RowsLastUpdated.Value, Is.EqualTo(now).Within(1).Milliseconds); Assert.That(resourceMetadata.SchemaLastUpdated.Value, Is.EqualTo(now).Within(1).Milliseconds); }
public void UnixDates_Can_Convert_To_Local_DateTimes() { DateTime now = DateTime.Now; double unixTimestamp = (now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; var resourceMetadata = new ResourceMetadata(mockClient) { CreationDateUnix = unixTimestamp, PublishedDateUnix = unixTimestamp, RowsLastUpdatedUnix = unixTimestamp, SchemaLastUpdatedUnix = unixTimestamp }; Assert.That(resourceMetadata.CreationDate.HasValue); Assert.That(resourceMetadata.PublishedDate.HasValue); Assert.That(resourceMetadata.RowsLastUpdated.HasValue); Assert.That(resourceMetadata.SchemaLastUpdated.HasValue); //allow a 1 millisecond buffer Assert.That(resourceMetadata.CreationDate.Value, Is.EqualTo(now).Within(1).Milliseconds); Assert.That(resourceMetadata.PublishedDate.Value, Is.EqualTo(now).Within(1).Milliseconds); Assert.That(resourceMetadata.RowsLastUpdated.Value, Is.EqualTo(now).Within(1).Milliseconds); Assert.That(resourceMetadata.SchemaLastUpdated.Value, Is.EqualTo(now).Within(1).Milliseconds); }
private static IEnumerable <ResourceAncestor> GetAncestorsYoungestToOldest(ImmutableDictionary <ResourceMetadata, ResourceAncestor> hierarchy, ResourceMetadata resource) { var visited = new HashSet <ResourceMetadata>(); while (hierarchy.TryGetValue(resource, out var ancestor) && !visited.Contains(ancestor.Resource)) { visited.Add(ancestor.Resource); yield return(ancestor); resource = ancestor.Resource; } }
public ResourceAncestor(ResourceAncestorType ancestorType, ResourceMetadata resource, SyntaxBase?indexExpression) { AncestorType = ancestorType; Resource = resource; IndexExpression = indexExpression; }
public void TestSetup() { mockClient = new SodaClient(StringMocks.Host, StringMocks.NonEmptyInput); mockMetadata = new ResourceMetadata(mockClient); }
public void ContactEmail_Is_Null_If_PrivateMetadata_Is_Null() { var resourceMetadata = new ResourceMetadata(mockClient); Assert.Null(resourceMetadata.PrivateMetadata); Assert.Null(resourceMetadata.ContactEmail); }
private bool TryResource(string name, IParser reader, Func <IParser, Type, object> nestedObjectDeserializer, ResourceMetadata metadata, CommentMetadata comment, out IResource spec) { spec = null; if (_Factory.TryDescriptor(name, out ISpecDescriptor descriptor) && reader.Accept <MappingStart>()) { if (!_Next.Deserialize(reader, descriptor.SpecType, nestedObjectDeserializer, out object value)) { return(false); } spec = descriptor.CreateInstance(RunspaceContext.CurrentThread.Source.File, metadata, comment, value); return(true); } return(false); }
public Tenant() { Meta = new ResourceMetadata("Tenant"); }
/// <summary> /// Link /// </summary> /// <param name="psb"></param> /// <param name="resPaths">resource paths</param> /// <param name="baseDir"></param> /// <param name="order">how to arrange images</param> /// <param name="isExternal">Whether this is an external texture PSB</param> public static void Link(this PSB psb, IList <string> resPaths, string baseDir = null, PsbLinkOrderBy order = PsbLinkOrderBy.Convention, bool isExternal = false) { if (isExternal) { psb.MotionResourceInstrument(); } var resList = psb.CollectResources(); var context = FreeMount.CreateContext(); if (order == PsbLinkOrderBy.Order) { for (int i = 0; i < resList.Count; i++) { var resMd = resList[i]; var fullPath = Path.Combine(baseDir ?? "", resPaths[i]); byte[] data = LoadImageBytes(fullPath, resMd, context); resMd.Resource.Data = data; } return; } if (order == PsbLinkOrderBy.Name) { if (psb.Platform == PsbSpec.krkr) { throw new InvalidOperationException( $"Can not link by file name for krkr PSB. Please consider using {PsbLinkOrderBy.Convention}"); } resList.Sort((md1, md2) => (int)(md1.TextureIndex ?? 0) - (int)(md2.TextureIndex ?? 0)); } for (var i = 0; i < resPaths.Count; i++) { var resPath = resPaths[i]; var resName = Path.GetFileNameWithoutExtension(resPath); //var resMd = uint.TryParse(resName, out uint rid) // ? resList.FirstOrDefault(r => r.Index == rid) // : resList.FirstOrDefault(r => // resName == $"{r.Part}{PsbResCollector.ResourceNameDelimiter}{r.Name}"); //Scan for Resource ResourceMetadata resMd = null; if (order == PsbLinkOrderBy.Name) { if (resName == null) { continue; } if (resList.Count == 1 && resPaths.Count == 1) { //If there is only one resource and one texture, we won't care about file name. resMd = resList[0]; } else { var texIdx = ResourceMetadata.GetTextureIndex(resName); if (texIdx == null) { Console.WriteLine($"[WARN]{resPath} is not used since the file name cannot be recognized."); continue; } if (resList.Count <= texIdx.Value) { Console.WriteLine($"[WARN]{resPath} is not used since the tex No. is too large."); continue; } resMd = resList[(int)texIdx.Value]; } } else //if (order == PsbLinkOrderBy.Convention) { resMd = resList.FirstOrDefault(r => resName == $"{r.Part}{PsbResCollector.ResourceNameDelimiter}{r.Name}"); if (resMd == null && uint.TryParse(resName, out uint rid)) { resMd = resList.FirstOrDefault(r => r.Index == rid); } if (resMd == null && psb.Type == PsbType.Pimg) { resMd = resList.FirstOrDefault(r => resName == Path.GetFileNameWithoutExtension(r.Name)); } } if (resMd == null) { Console.WriteLine($"[WARN]{resPath} is not used."); continue; } var fullPath = Path.Combine(baseDir ?? "", resPath.Replace('/', '\\')); byte[] data = LoadImageBytes(fullPath, resMd, context); resMd.Resource.Data = data; } }
internal static byte[] LoadImageBytes(string path, ResourceMetadata metadata, FreeMountContext context) { byte[] data; Bitmap image = null; var ext = Path.GetExtension(path)?.ToLowerInvariant(); if (metadata.Compress == PsbCompressType.ByName && ext != null && metadata.Name != null && metadata.Name.EndsWith(ext, true, null)) { return(File.ReadAllBytes(path)); } switch (ext) { //tlg case ".tlg" when metadata.Compress == PsbCompressType.Tlg: return(File.ReadAllBytes(path)); case ".tlg": image = context.ResourceToBitmap(".tlg", File.ReadAllBytes(path)); break; //rl case ".rl" when metadata.Compress == PsbCompressType.RL: return(File.ReadAllBytes(path)); case ".rl" when metadata.Compress == PsbCompressType.None: return(RL.Decompress(File.ReadAllBytes(path))); case ".rl": image = RL.DecompressToImage(File.ReadAllBytes(path), metadata.Height, metadata.Width, metadata.PixelFormat); break; //raw case ".raw" when metadata.Compress == PsbCompressType.None: return(File.ReadAllBytes(path)); case ".raw" when metadata.Compress == PsbCompressType.RL: return(RL.Compress(File.ReadAllBytes(path))); case ".raw": image = RL.ConvertToImage(File.ReadAllBytes(path), metadata.Height, metadata.Width, metadata.PixelFormat); break; //bin case ".bin": return(File.ReadAllBytes(path)); //image default: if (SupportedImageExt.Contains(ext)) { image = new Bitmap(path); } else if (context.SupportImageExt(ext)) { image = context.ResourceToBitmap(ext, File.ReadAllBytes(path)); } else { //MARK: No longer try to read files we don't know //return File.ReadAllBytes(path); return(null); } break; } switch (metadata.Compress) { case PsbCompressType.RL: data = RL.CompressImage(image, metadata.PixelFormat); break; case PsbCompressType.Tlg: data = context.BitmapToResource(".tlg", image); if (data == null) { var tlgPath = Path.ChangeExtension(path, ".tlg"); if (File.Exists(tlgPath)) { Console.WriteLine($"[WARN] Can not encode TLG, using {tlgPath}"); data = File.ReadAllBytes(tlgPath); } else { Console.WriteLine($"[WARN] Can not convert image to TLG: {path}"); data = File.ReadAllBytes(path); } } break; case PsbCompressType.ByName: var imgExt = Path.GetExtension(metadata.Name); if (context.SupportImageExt(imgExt)) { data = context.BitmapToResource(imgExt, image); } else { Console.WriteLine($"[WARN] Unsupported image: {path}"); data = File.ReadAllBytes(path); } break; case PsbCompressType.None: default: data = RL.GetPixelBytesFromImage(image, metadata.PixelFormat); break; } return(data); }
private void EmitResource(JsonTextWriter jsonWriter, ResourceMetadata resource, ExpressionEmitter emitter) { jsonWriter.WriteStartObject(); // Note: conditions STACK with nesting. // // Children inherit the conditions of their parents, etc. This avoids a problem // where we emit a dependsOn to something that's not in the template, or not // being evaulated i the template. var conditions = new List <SyntaxBase>(); var loops = new List <(string name, ForSyntax @for, SyntaxBase?input)>(); var ancestors = this.context.SemanticModel.ResourceAncestors.GetAncestors(resource); foreach (var ancestor in ancestors) { if (ancestor.AncestorType == ResourceAncestorGraph.ResourceAncestorType.Nested && ancestor.Resource.Symbol.DeclaringResource.Value is IfConditionSyntax ifCondition) { conditions.Add(ifCondition.ConditionExpression); } if (ancestor.AncestorType == ResourceAncestorGraph.ResourceAncestorType.Nested && ancestor.Resource.Symbol.DeclaringResource.Value is ForSyntax @for) { loops.Add((ancestor.Resource.Symbol.Name, @for, null)); } } // Unwrap the 'real' resource body if there's a condition var body = resource.Symbol.DeclaringResource.Value; switch (body) { case IfConditionSyntax ifCondition: body = ifCondition.Body; conditions.Add(ifCondition.ConditionExpression); break; case ForSyntax @for: loops.Add((resource.Symbol.Name, @for, null)); if (@for.Body is IfConditionSyntax loopFilter) { body = loopFilter.Body; conditions.Add(loopFilter.ConditionExpression); } else { body = @for.Body; } break; } if (conditions.Count == 1) { emitter.EmitProperty("condition", conditions[0]); } else if (conditions.Count > 1) { var @operator = new BinaryOperationSyntax( conditions[0], SyntaxFactory.CreateToken(TokenType.LogicalAnd), conditions[1]); for (var i = 2; i < conditions.Count; i++) { @operator = new BinaryOperationSyntax( @operator, SyntaxFactory.CreateToken(TokenType.LogicalAnd), conditions[i]); } emitter.EmitProperty("condition", @operator); } if (loops.Count == 1) { var batchSize = GetBatchSize(resource.Symbol.DeclaringResource); emitter.EmitProperty("copy", () => emitter.EmitCopyObject(loops[0].name, loops[0].@for, loops[0].input, batchSize: batchSize)); } else if (loops.Count > 1) { throw new InvalidOperationException("nested loops are not supported"); } if (context.Settings.EnableSymbolicNames && resource.IsExistingResource) { jsonWriter.WritePropertyName("existing"); jsonWriter.WriteValue(true); } var importSymbol = context.SemanticModel.Root.ImportDeclarations.FirstOrDefault(i => resource.Type.DeclaringNamespace.AliasNameEquals(i.Name)); if (importSymbol is not null) { emitter.EmitProperty("import", importSymbol.Name); } if (resource.IsAzResource) { emitter.EmitProperty("type", resource.TypeReference.FormatType()); if (resource.TypeReference.ApiVersion is not null) { emitter.EmitProperty("apiVersion", resource.TypeReference.ApiVersion); } } else { emitter.EmitProperty("type", resource.TypeReference.FormatName()); } if (context.SemanticModel.EmitLimitationInfo.ResourceScopeData.TryGetValue(resource, out var scopeData)) { ScopeHelper.EmitResourceScopeProperties(context.SemanticModel, scopeData, emitter, body); } if (resource.IsAzResource) { emitter.EmitProperty(AzResourceTypeProvider.ResourceNamePropertyName, emitter.GetFullyQualifiedResourceName(resource)); emitter.EmitObjectProperties((ObjectSyntax)body, ResourcePropertiesToOmit.Add(AzResourceTypeProvider.ResourceNamePropertyName)); } else { jsonWriter.WritePropertyName("properties"); jsonWriter.WriteStartObject(); emitter.EmitObjectProperties((ObjectSyntax)body, ResourcePropertiesToOmit); jsonWriter.WriteEndObject(); } this.EmitDependsOn(jsonWriter, resource.Symbol, emitter, body); // Since we don't want to be mutating the body of the original ObjectSyntax, we create an placeholder body in place // and emit its properties to merge decorator properties. foreach (var(property, val) in AddDecoratorsToBody( resource.Symbol.DeclaringResource, SyntaxFactory.CreateObject(Enumerable.Empty <ObjectPropertySyntax>()), resource.Symbol.Type).ToNamedPropertyValueDictionary()) { emitter.EmitProperty(property, val); } jsonWriter.WriteEndObject(); }
public void RowIdentifierField_Metadata_Can_Be_Read_If_Present() { long rowIdentifierFieldId = 12345; string rowIdentifierField = "rowIdentifierField"; var resourceMetadataWithNumericRowIdentifier = new ResourceMetadata(mockClient) { Columns = new[] { new ResourceColumn() { Id = rowIdentifierFieldId, SodaFieldName = rowIdentifierField } }, Metadata = new Dictionary<string, dynamic>() { { "rowIdentifier", rowIdentifierFieldId } } }; Assert.AreEqual(rowIdentifierField, resourceMetadataWithNumericRowIdentifier.RowIdentifierField); var resourceMetadataWithTextRowIdentifier = new ResourceMetadata(mockClient) { Metadata = new Dictionary<string, dynamic>() { { "rowIdentifier", rowIdentifierField } } }; Assert.AreEqual(rowIdentifierField, resourceMetadataWithTextRowIdentifier.RowIdentifierField); }
public void ContactEmail_Can_Be_Read_If_Present() { string contactEmail = "*****@*****.**"; var resourceMetadata = new ResourceMetadata(mockClient) { PrivateMetadata = new Dictionary<string, dynamic>() { { "contactEmail", contactEmail } } }; Assert.AreEqual(contactEmail, resourceMetadata.ContactEmail); }
public CompetencyResourceSummaryViewModel(ResourceMetadata resource) { Resource = resource; }
public void RowIdentifierField_Is_Socrata_Id_If_Metadata_Is_Null() { var resourceMetadata = new ResourceMetadata(mockClient); Assert.Null(resourceMetadata.Metadata); Assert.AreEqual(":id", resourceMetadata.RowIdentifierField); }
private bool TryMetadata(IParser reader, Func <IParser, Type, object> nestedObjectDeserializer, out ResourceMetadata metadata) { metadata = null; if (reader.Accept <MappingStart>()) { if (!_Next.Deserialize(reader, typeof(ResourceMetadata), nestedObjectDeserializer, out object value)) { return(false); } metadata = (ResourceMetadata)value; return(true); } return(false); }
public ResourceType() { Meta = new ResourceMetadata(ScimConstants.ResourceTypes.ResourceType); }
public void TestSetup() { mockClient = new SodaClient(StringMocks.Host, StringMocks.NonEmptyInput); mockMetadata = new ResourceMetadata(mockClient); }
public void RowIdentifierField_Is_Socrata_Id_If_RowIdentifierFieldId_Has_Value_With_No_Matching_Columns() { string expected = ":id"; var resourceMetadata = new ResourceMetadata(mockClient) { Metadata = new Dictionary<string, dynamic>() { { "rowIdentifier", 0 } } }; Assert.Null(resourceMetadata.Columns); Assert.AreEqual(expected, resourceMetadata.RowIdentifierField); resourceMetadata.Columns = new List<ResourceColumn>(); Assert.IsNotNull(resourceMetadata.Columns); Assert.AreEqual(expected, resourceMetadata.RowIdentifierField); resourceMetadata.Columns = new List<ResourceColumn>() { new ResourceColumn() { Id = 12345, SodaFieldName = "something" } }; Assert.IsNotEmpty(resourceMetadata.Columns); Assert.AreEqual(expected, resourceMetadata.RowIdentifierField); }