public static T Get <T>(this JsonElement?element, string property)
 {
     return(element !.Value.GetProperty(property).ToObject <T>());
 }
Пример #2
0
 internal JsonObject(JsonElement element, JsonNodeOptions?options = null) : this(options)
 {
     Debug.Assert(element.ValueKind == JsonValueKind.Object);
     _jsonElement = element;
 }
Пример #3
0
        internal override void OnMessage(string method, JsonElement?serverParams)
        {
            switch (method)
            {
            case "close":
                Closed?.Invoke(this, EventArgs.Empty);
                break;

            case "crash":
                Crashed?.Invoke(this, EventArgs.Empty);
                break;

            case "domcontentloaded":
                DOMContentLoaded?.Invoke(this, EventArgs.Empty);
                break;

            case "load":
                Load?.Invoke(this, EventArgs.Empty);
                break;

            case "bindingCall":
                BindingCall?.Invoke(
                    this,
                    new BindingCallEventArgs
                {
                    BidingCall = serverParams?.GetProperty("binding").ToObject <BindingCallChannel>(Connection.GetDefaultJsonSerializerOptions()).Object,
                });
                break;

            case "route":
                Route?.Invoke(
                    this,
                    new RouteEventArgs
                {
                    Route   = serverParams?.GetProperty("route").ToObject <RouteChannel>(Connection.GetDefaultJsonSerializerOptions()).Object,
                    Request = serverParams?.GetProperty("request").ToObject <RequestChannel>(Connection.GetDefaultJsonSerializerOptions()).Object,
                });
                break;

            case "popup":
                Popup?.Invoke(this, new PageChannelPopupEventArgs
                {
                    Page = serverParams?.GetProperty("page").ToObject <PageChannel>(Connection.GetDefaultJsonSerializerOptions()).Object,
                });
                break;

            case "pageError":
                PageError?.Invoke(this, serverParams?.GetProperty("error").GetProperty("error").ToObject <PageErrorEventArgs>(Connection.GetDefaultJsonSerializerOptions()));
                break;

            case "fileChooser":
                FileChooser?.Invoke(this, serverParams?.ToObject <FileChooserChannelEventArgs>(Connection.GetDefaultJsonSerializerOptions()));
                break;

            case "frameAttached":
                FrameAttached?.Invoke(this, new FrameEventArgs(serverParams?.GetProperty("frame").ToObject <FrameChannel>(Connection.GetDefaultJsonSerializerOptions()).Object));
                break;

            case "frameDetached":
                FrameDetached?.Invoke(this, new FrameEventArgs(serverParams?.GetProperty("frame").ToObject <FrameChannel>(Connection.GetDefaultJsonSerializerOptions()).Object));
                break;

            case "dialog":
                Dialog?.Invoke(this, new DialogEventArgs(serverParams?.GetProperty("dialog").ToObject <DialogChannel>(Connection.GetDefaultJsonSerializerOptions()).Object));
                break;

            case "console":
                Console?.Invoke(this, new ConsoleEventArgs(serverParams?.GetProperty("message").ToObject <ConsoleMessage>(Connection.GetDefaultJsonSerializerOptions())));
                break;

            case "request":
                Request?.Invoke(this, new RequestEventArgs {
                    Request = serverParams?.GetProperty("request").ToObject <RequestChannel>(Connection.GetDefaultJsonSerializerOptions()).Object
                });
                break;

            case "requestFinished":
                RequestFinished?.Invoke(this, new RequestEventArgs {
                    Request = serverParams?.GetProperty("request").ToObject <RequestChannel>(Connection.GetDefaultJsonSerializerOptions()).Object
                });
                break;

            case "requestFailed":
                RequestFailed?.Invoke(this, serverParams?.ToObject <PageChannelRequestFailedEventArgs>(Connection.GetDefaultJsonSerializerOptions()));
                break;

            case "response":
                Response?.Invoke(this, new ResponseEventArgs {
                    Response = serverParams?.GetProperty("response").ToObject <ResponseChannel>(Connection.GetDefaultJsonSerializerOptions()).Object
                });
                break;

            case "download":
                Download?.Invoke(this, new DownloadEventArgs()
                {
                    Download = serverParams?.GetProperty("download").ToObject <DownloadChannel>(Connection.GetDefaultJsonSerializerOptions()).Object
                });
                break;

            case "video":
                Video?.Invoke(this, new VideoEventArgs()
                {
                    RelativePath = serverParams?.GetProperty("relativePath").ToString()
                });
                break;

            case "worker":
                Worker?.Invoke(
                    this,
                    new WorkerChannelEventArgs
                {
                    WorkerChannel = serverParams?.GetProperty("worker").ToObject <WorkerChannel>(Connection.GetDefaultJsonSerializerOptions()),
                });
                break;
            }
        }
Пример #4
0
        public void ShouldNotMap_From_Nullable_JsonElement_To_JArray_Given_Invalid_Element(JsonElement?element)
        {
            var item = new JsonElementA()
            {
                Bar = element
            };
            var result = Mapper.Map <JArrayA>(item);

            A.CallTo(Logger)
            .Where(x => x.Method.Name == nameof(Logger.Log))
            .Where(x => x.Arguments.Get <LogLevel>(0) == LogLevel.Warning)
            .Where(
                x => x.Arguments.Get <IReadOnlyList <KeyValuePair <string, object> > >(2).Any(
                    z => z.Value is string &&
                    ((string)z.Value) == "Tried to convert non array JsonElement? to JArray"
                    )
                )
            .MustHaveHappened();
            result.Bar.Should().BeNull();
        }
Пример #5
0
 public EditArticle(JsonElement?_json)
 {
     LanguageJson = _json;
 }
Пример #6
0
 public Searched(JsonElement?_json)
 {
     LanguageJson = _json;
 }
Пример #7
0
        /// <summary>
        ///     Gets the structured metadata for the npm package
        /// </summary>
        /// <param name="purl"> </param>
        /// <returns> </returns>
        public override async Task <PackageMetadata> GetPackageMetadata(PackageURL purl)
        {
            PackageMetadata metadata = new PackageMetadata();
            string?         content  = await GetMetadata(purl);

            if (string.IsNullOrEmpty(content))
            {
                return(metadata);
            }

            // convert NPM package data to normalized form
            JsonDocument contentJSON = JsonDocument.Parse(content);
            JsonElement  root        = contentJSON.RootElement;

            metadata.Name        = root.GetProperty("name").GetString();
            metadata.Description = root.GetProperty("description").GetString();

            metadata.PackageManagerUri = ENV_NPM_ENDPOINT;
            metadata.Platform          = "NPM";
            metadata.Language          = "JavaScript";
            metadata.Package_Uri       = $"{metadata.PackageManagerUri}/package/{metadata.Name}";

            var versions      = GetVersions(contentJSON);
            var latestVersion = GetLatestVersion(versions);

            if (purl.Version != null)
            {
                // find the version object from the collection
                metadata.PackageVersion = purl.Version;
            }
            else
            {
                metadata.PackageVersion = latestVersion is null ? purl.Version : latestVersion?.ToString();
            }

            // if we found any version at all, get the deets
            if (metadata.PackageVersion is not null)
            {
                Version     versionToGet   = new Version(metadata.PackageVersion);
                JsonElement?versionElement = GetVersionElement(contentJSON, versionToGet);
                if (versionElement is not null)
                {
                    // redo the generic values to version specific values
                    metadata.Package_Uri = $"{ENV_NPM_ENDPOINT}/package/{metadata.Name}";
                    metadata.VersionUri  = $"{ENV_NPM_ENDPOINT}/package/{metadata.Name}/v/{metadata.PackageVersion}";

                    var distElement = Utilities.GetJSONPropertyIfExists(versionElement, "dist");
                    if (distElement?.GetProperty("tarball") is JsonElement tarballElement)
                    {
                        metadata.VersionDownloadUri = tarballElement.ToString() ??
                                                      $"{ENV_NPM_API_ENDPOINT}/{metadata.Name}/-/{metadata.Name}-{metadata.PackageVersion}.tgz";
                    }

                    if (distElement?.GetProperty("integrity") is JsonElement integrityElement &&
                        integrityElement.ToString() is string integrity &&
                        integrity.Split('-') is string[] pair &&
                        pair.Length == 2)
                    {
                        metadata.Signature ??= new List <Digest>();
                        metadata.Signature.Add(new Digest()
                        {
                            Algorithm = pair[0],
                            Signature = pair[1]
                        });
                    }
                    // size
                    if (Utilities.GetJSONPropertyIfExists(distElement, "unpackedSize") is JsonElement sizeElement &&
                        sizeElement.GetInt64() is long size)
                    {
                        metadata.Size = size;
                    }

                    // commit id
                    if (Utilities.GetJSONPropertyStringIfExists(versionElement, "gitHead") is string gitHead &&
                        !string.IsNullOrWhiteSpace(gitHead))
                    {
                        metadata.CommitId = gitHead;
                    }

                    // install scripts
                    {
                        if (Utilities.GetJSONEnumerator(Utilities.GetJSONPropertyIfExists(versionElement, "scripts"))
                            is JsonElement.ArrayEnumerator enumerator &&
                            enumerator.Any())
                        {
                            metadata.Scripts ??= new List <Command>();
                            enumerator.ToList().ForEach((element) => metadata.Scripts.Add(new Command {
                                CommandLine = element.ToString()
                            }));
                        }
                    }

                    // dependencies
                    var dependencies = Utilities.ConvertJSONToList(Utilities.GetJSONPropertyIfExists(versionElement, "dependencies"));
                    if (dependencies is not null && dependencies.Count > 0)
                    {
                        metadata.Dependencies ??= new List <Dependency>();
                        dependencies.ForEach((dependency) => metadata.Dependencies.Add(new Dependency()
                        {
                            Package = dependency
                        }));
                    }

                    // author(s)
                    var  authorElement = Utilities.GetJSONPropertyIfExists(versionElement, "author");
                    User author        = new User();
                    if (authorElement is not null)
                    {
                        author.Name  = Utilities.GetJSONPropertyStringIfExists(authorElement, "name");
                        author.Email = Utilities.GetJSONPropertyStringIfExists(authorElement, "email");
                        author.Url   = Utilities.GetJSONPropertyStringIfExists(authorElement, "url");

                        metadata.Authors ??= new List <User>();
                        metadata.Authors.Add(author);
                    }

                    // maintainers
                    {
                        var maintainersElement = Utilities.GetJSONPropertyIfExists(versionElement, "maintainers");
                        if (maintainersElement?.EnumerateArray() is JsonElement.ArrayEnumerator enumerator)
                        {
                            metadata.Maintainers ??= new List <User>();
                            enumerator.ToList().ForEach((element) =>
                            {
                                metadata.Maintainers.Add(
                                    new User
                                {
                                    Name  = Utilities.GetJSONPropertyStringIfExists(element, "name"),
                                    Email = Utilities.GetJSONPropertyStringIfExists(element, "email"),
                                    Url   = Utilities.GetJSONPropertyStringIfExists(element, "url")
                                });
                            });
                        }
                    }

                    // repository
                    var repoMappings = await SearchRepoUrlsInPackageMetadata(purl, content);

                    foreach (var repoMapping in repoMappings)
                    {
                        Repository repository = new Repository
                        {
                            Rank = repoMapping.Value,
                            Type = repoMapping.Key.Type
                        };
                        await repository.ExtractRepositoryMetadata(repoMapping.Key);

                        metadata.Repository ??= new List <Repository>();
                        metadata.Repository.Add(repository);
                    }

                    // keywords
                    metadata.Keywords = Utilities.ConvertJSONToList(Utilities.GetJSONPropertyIfExists(versionElement, "keywords"));

                    // licenses
                    {
                        if (Utilities.GetJSONEnumerator(Utilities.GetJSONPropertyIfExists(versionElement, "licenses"))
                            is JsonElement.ArrayEnumerator enumeratorElement &&
                            enumeratorElement.ToList() is List <JsonElement> enumerator &&
                            enumerator.Any())
                        {
                            metadata.Licenses ??= new List <License>();
                            // TODO: Convert/append SPIX_ID values?
                            enumerator.ForEach((license) =>
                            {
                                metadata.Licenses.Add(new License()
                                {
                                    Name = Utilities.GetJSONPropertyStringIfExists(license, "type"),
                                    Url  = Utilities.GetJSONPropertyStringIfExists(license, "url")
                                });
                            });
                        }
                    }
                }
            }

            if (latestVersion is not null)
            {
                metadata.LatestPackageVersion = latestVersion.ToString();
            }

            return(metadata);
        }
Пример #8
0
 public static string GetRawText(this JsonElement?jsonElement)
 {
     return(jsonElement?.GetRawText() ?? "{}");
 }
Пример #9
0
 public static string GetString(this JsonElement?jsonElement)
 {
     return(jsonElement?.GetString());
 }
Пример #10
0
 public static string?GetJSONPropertyStringIfExists(JsonElement?element, string keyName)
 {
     return(GetJSONPropertyIfExists(element, keyName)?.ToString());
 }
Пример #11
0
 public static IEnumerable <JsonElement> EnumerateArray(this JsonElement?jsonElement)
 {
     return((jsonElement?.EnumerateArray() as IEnumerable <JsonElement>) ?? Array.Empty <JsonElement>());
 }
        private async Task <CloudEvent[]> DeserializeCloudEventsInternal(string requestContent, bool async, CancellationToken cancellationToken = default)
        {
            List <CloudEventInternal> cloudEventsInternal = new List <CloudEventInternal>();
            List <CloudEvent>         cloudEvents         = new List <CloudEvent>();

            // Deserialize raw JSON string into separate events, deserialize event envelope properties
            JsonDocument requestDocument = await ParseJsonToDocument(requestContent, async, cancellationToken).ConfigureAwait(false);

            foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
            {
                cloudEventsInternal.Add(CloudEventInternal.DeserializeCloudEventInternal(property));
            }

            // Deserialize 'Data' property from JsonElement for each event
            foreach (CloudEventInternal cloudEventInternal in cloudEventsInternal)
            {
                object cloudEventData = null;
                if (cloudEventInternal.DataBase64 != null)
                {
                    cloudEventData = Convert.FromBase64String(cloudEventInternal.DataBase64);
                }
                else
                {
                    JsonElement?dataElement = cloudEventInternal.Data;

                    if (dataElement.HasValue && dataElement.Value.ValueKind != JsonValueKind.Null)
                    {
                        // Reserialize JsonElement to stream
                        MemoryStream dataStream = SerializePayloadToStream(dataElement, cancellationToken);

                        // First, let's attempt to find the mapping for the event type in the custom event mapping.
                        if (_customEventTypeMappings.TryGetValue(cloudEventInternal.Type, out Type typeOfEventData))
                        {
                            if (!TryGetPrimitiveFromJsonElement(dataElement.Value, out cloudEventData))
                            {
                                if (async)
                                {
                                    cloudEventData = await _dataSerializer.DeserializeAsync(dataStream, typeOfEventData, cancellationToken).ConfigureAwait(false);
                                }
                                else
                                {
                                    cloudEventData = _dataSerializer.Deserialize(dataStream, typeOfEventData, cancellationToken);
                                }
                            }
                        }
                        // If a custom mapping doesn't exist, let's attempt to find the mapping for the deserialization function in the system event type mapping.
                        else if (SystemEventTypeMappings.SystemEventDeserializers.TryGetValue(cloudEventInternal.Type, out Func <JsonElement, object> systemDeserializationFunction))
                        {
                            cloudEventData = systemDeserializationFunction(dataElement.Value);
                        }
                        // If no custom mapping was added, either return a primitive/string, or an object wrapped as BinaryData
                        else
                        {
                            // If event data is not a primitive/string, return as BinaryData
                            if (!TryGetPrimitiveFromJsonElement(dataElement.Value, out cloudEventData))
                            {
                                cloudEventData = BinaryData.FromStream(dataStream);
                            }
                        }
                    }
                    else // Event has null data
                    {
                        cloudEventData          = null;
                        cloudEventInternal.Type = "";
                    }
                }

                cloudEvents.Add(new CloudEvent(
                                    cloudEventInternal.Source,
                                    cloudEventInternal.Type)
                {
                    Id              = cloudEventInternal.Id,
                    Data            = cloudEventData,
                    Time            = cloudEventInternal.Time,
                    DataSchema      = cloudEventInternal.Dataschema,
                    DataContentType = cloudEventInternal.Datacontenttype,
                    Subject         = cloudEventInternal.Subject
                });
            }

            return(cloudEvents.ToArray());
        }
Пример #13
0
 public TemplateSpecEntity(ResourceIdentifier id, string name, ResourceType type, JsonElement systemData, string location, JsonElement?tags, string?description, JsonElement?linkedTemplates, JsonElement?metadata, JsonElement mainTemplate, JsonElement?uiFormDefinition)
 {
     this.Id               = id;
     this.Name             = name;
     this.Type             = type;
     this.SystemData       = systemData;
     this.Location         = location;
     this.Tags             = tags;
     this.Description      = description;
     this.LinkedTemplates  = linkedTemplates;
     this.Metadata         = metadata;
     this.MainTemplate     = mainTemplate;
     this.UiFormDefinition = uiFormDefinition;
 }
Пример #14
0
        /// <inheritdoc />
        public override async Task <PackageMetadata?> GetPackageMetadataAsync(PackageURL purl, bool useCache = true)
        {
            PackageMetadata metadata = new();
            string?         content  = await GetMetadataAsync(purl, useCache);

            if (string.IsNullOrEmpty(content))
            {
                return(null);
            }

            // convert NPM package data to normalized form
            JsonDocument contentJSON = JsonDocument.Parse(content);
            JsonElement  root        = contentJSON.RootElement;

            metadata.Name        = root.GetProperty("name").GetString();
            metadata.Description = OssUtilities.GetJSONPropertyStringIfExists(root, "description");

            metadata.PackageManagerUri = ENV_NPM_ENDPOINT;
            metadata.Platform          = "NPM";
            metadata.Language          = "JavaScript";
            metadata.PackageUri        = $"{metadata.PackageManagerUri}/package/{metadata.Name}";
            metadata.ApiPackageUri     = $"{ENV_NPM_API_ENDPOINT}/{metadata.Name}";

            List <Version> versions      = GetVersions(contentJSON);
            Version?       latestVersion = GetLatestVersion(versions);

            if (purl.Version != null)
            {
                // find the version object from the collection
                metadata.PackageVersion = purl.Version;
            }
            else
            {
                metadata.PackageVersion = latestVersion is null ? purl.Version : latestVersion?.ToString();
            }

            // if we found any version at all, get the information
            if (metadata.PackageVersion != null)
            {
                Version     versionToGet   = new(metadata.PackageVersion);
                JsonElement?versionElement = GetVersionElement(contentJSON, versionToGet);

                if (root.TryGetProperty("time", out JsonElement time))
                {
                    string?uploadTime = OssUtilities.GetJSONPropertyStringIfExists(time, metadata.PackageVersion);
                    if (uploadTime != null)
                    {
                        metadata.UploadTime = DateTime.Parse(uploadTime);
                    }
                }

                if (versionElement != null)
                {
                    // redo the generic values to version specific values
                    metadata.PackageUri    = $"{ENV_NPM_ENDPOINT}/package/{metadata.Name}";
                    metadata.VersionUri    = $"{ENV_NPM_ENDPOINT}/package/{metadata.Name}/v/{metadata.PackageVersion}";
                    metadata.ApiVersionUri = $"{ENV_NPM_API_ENDPOINT}/{metadata.Name}/{metadata.PackageVersion}";

                    // prioritize the version level description
                    if (OssUtilities.GetJSONPropertyStringIfExists(versionElement, "description") is string description)
                    {
                        metadata.Description = description;
                    }

                    JsonElement?distElement = OssUtilities.GetJSONPropertyIfExists(versionElement, "dist");
                    if (OssUtilities.GetJSONPropertyIfExists(distElement, "tarball") is JsonElement tarballElement)
                    {
                        metadata.VersionDownloadUri = tarballElement.ToString().IsBlank() ?
                                                      $"{ENV_NPM_API_ENDPOINT}/{metadata.Name}/-/{metadata.Name}-{metadata.PackageVersion}.tgz"
                            : tarballElement.ToString();
                    }

                    if (OssUtilities.GetJSONPropertyIfExists(distElement, "integrity") is JsonElement integrityElement &&
                        integrityElement.ToString() is string integrity &&
                        integrity.Split('-') is string[] pair &&
                        pair.Length == 2)
                    {
                        metadata.Signature ??= new List <Digest>();
                        metadata.Signature.Add(new Digest()
                        {
                            Algorithm = pair[0],
                            Signature = pair[1]
                        });
                    }

                    // size
                    if (OssUtilities.GetJSONPropertyIfExists(distElement, "unpackedSize") is JsonElement sizeElement &&
                        sizeElement.GetInt64() is long size)
                    {
                        metadata.Size = size;
                    }

                    // check for typescript
                    List <string>?devDependencies = OssUtilities.ConvertJSONToList(OssUtilities.GetJSONPropertyIfExists(versionElement, "devDependencies"));
                    if (devDependencies is not null && devDependencies.Count > 0 && devDependencies.Any(stringToCheck => stringToCheck.Contains("\"typescript\":")))
                    {
                        metadata.Language = "TypeScript";
                    }

                    // homepage
                    if (OssUtilities.GetJSONPropertyStringIfExists(versionElement, "homepage") is string homepage &&
                        !string.IsNullOrWhiteSpace(homepage))
                    {
                        metadata.Homepage = homepage;
                    }

                    // commit id
                    if (OssUtilities.GetJSONPropertyStringIfExists(versionElement, "gitHead") is string gitHead &&
                        !string.IsNullOrWhiteSpace(gitHead))
                    {
                        metadata.CommitId = gitHead;
                    }

                    // install scripts
                    List <string>?scripts = OssUtilities.ConvertJSONToList(OssUtilities.GetJSONPropertyIfExists(versionElement, "scripts"));
                    if (scripts is not null && scripts.Count > 0)
                    {
                        metadata.Scripts ??= new List <Command>();
                        scripts.ForEach((element) => metadata.Scripts.Add(new Command {
                            CommandLine = element
                        }));
                    }

                    // dependencies
                    List <string>?dependencies = OssUtilities.ConvertJSONToList(OssUtilities.GetJSONPropertyIfExists(versionElement, "dependencies"));
                    if (dependencies is not null && dependencies.Count > 0)
                    {
                        metadata.Dependencies ??= new List <Dependency>();
                        dependencies.ForEach((dependency) => metadata.Dependencies.Add(new Dependency()
                        {
                            Package = dependency
                        }));
                    }

                    // author(s)
                    JsonElement?authorElement = OssUtilities.GetJSONPropertyIfExists(versionElement, "_npmUser");
                    if (authorElement is not null)
                    {
                        User author = new()
                        {
                            Name  = OssUtilities.GetJSONPropertyStringIfExists(authorElement, "name"),
                            Email = OssUtilities.GetJSONPropertyStringIfExists(authorElement, "email"),
                            Url   = OssUtilities.GetJSONPropertyStringIfExists(authorElement, "url")
                        };

                        metadata.Authors ??= new List <User>();
                        metadata.Authors.Add(author);
                    }

                    // maintainers
                    JsonElement?maintainersElement = OssUtilities.GetJSONPropertyIfExists(versionElement, "maintainers");
                    if (maintainersElement?.EnumerateArray() is JsonElement.ArrayEnumerator maintainerEnumerator)
                    {
                        metadata.Maintainers ??= new List <User>();
                        maintainerEnumerator.ToList().ForEach((element) =>
                        {
                            metadata.Maintainers.Add(
                                new User
                            {
                                Name  = OssUtilities.GetJSONPropertyStringIfExists(element, "name"),
                                Email = OssUtilities.GetJSONPropertyStringIfExists(element, "email"),
                                Url   = OssUtilities.GetJSONPropertyStringIfExists(element, "url")
                            });
                        });
                    }

                    // repository
                    Dictionary <PackageURL, double> repoMappings = await SearchRepoUrlsInPackageMetadata(purl, content);

                    foreach (KeyValuePair <PackageURL, double> repoMapping in repoMappings)
                    {
                        Repository repository = new()
                        {
                            Rank = repoMapping.Value,
                            Type = repoMapping.Key.Type
                        };
                        await repository.ExtractRepositoryMetadata(repoMapping.Key);

                        metadata.Repository ??= new List <Repository>();
                        metadata.Repository.Add(repository);
                    }

                    // keywords
                    metadata.Keywords = OssUtilities.ConvertJSONToList(OssUtilities.GetJSONPropertyIfExists(versionElement, "keywords"));

                    // licenses
                    {
                        if (OssUtilities.GetJSONEnumerator(OssUtilities.GetJSONPropertyIfExists(versionElement, "licenses"))
                            is JsonElement.ArrayEnumerator enumeratorElement &&
                            enumeratorElement.ToList() is List <JsonElement> enumerator &&
                            enumerator.Any())
                        {
                            metadata.Licenses ??= new List <License>();
                            // TODO: Convert/append SPIX_ID values?
                            enumerator.ForEach((license) =>
                            {
                                metadata.Licenses.Add(new License()
                                {
                                    Name = OssUtilities.GetJSONPropertyStringIfExists(license, "type"),
                                    Url  = OssUtilities.GetJSONPropertyStringIfExists(license, "url")
                                });
                            });
                        }
                    }
                }
            }

            if (latestVersion is not null)
            {
                metadata.LatestPackageVersion = latestVersion.ToString();
            }

            return(metadata);
        }
Пример #15
0
        /// <summary>
        ///   Performs a deep copy operation on <paramref name="jsonElement"/> returning corresponding modifiable tree structure of JSON nodes.
        ///   Operations performed on returned <see cref="JsonNode"/> does not modify <paramref name="jsonElement"/>.
        /// </summary>
        /// <param name="jsonElement"><see cref="JsonElement"/> to copy.</param>
        /// <returns><see cref="JsonNode"/>  representing <paramref name="jsonElement"/>.</returns>
        public static JsonNode DeepCopy(JsonElement jsonElement)
        {
            if (!jsonElement.IsImmutable)
            {
                return(GetNode(jsonElement).Clone());
            }

            // Iterative DFS:

            var      currentNodes   = new Stack <KeyValuePair <string?, JsonNode?> >();    // objects/arrays currently being created
            var      recursionStack = new Stack <KeyValuePair <string?, JsonElement?> >(); // null JsonElement represents end of current object/array
            JsonNode?toReturn       = null;

            recursionStack.Push(new KeyValuePair <string?, JsonElement?>(null, jsonElement));

            while (recursionStack.TryPop(out KeyValuePair <string?, JsonElement?> currentPair))
            {
                JsonElement?currentJsonElement = currentPair.Value;

                if (!currentJsonElement.HasValue)
                {
                    // Current object/array is finished and can be added to its parent:

                    KeyValuePair <string?, JsonNode?> nodePair = currentNodes.Pop();

                    Debug.Assert(nodePair.Value is JsonArray || nodePair.Value is JsonObject);

                    AddToParent(nodePair, ref currentNodes, ref toReturn);

                    continue;
                }

                switch (currentJsonElement.Value.ValueKind)
                {
                case JsonValueKind.Object:
                    var jsonObject = new JsonObject();

                    // Add jsonObject to current nodes:
                    currentNodes.Push(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonObject));

                    // Add end of object marker:
                    recursionStack.Push(new KeyValuePair <string?, JsonElement?>(null, null));

                    // Add properties to recursion stack. Reverse enumerate to keep properties order:
                    foreach (JsonProperty property in currentJsonElement.Value.EnumerateObject().Reverse())
                    {
                        recursionStack.Push(new KeyValuePair <string?, JsonElement?>(property.Name, property.Value));
                    }
                    break;

                case JsonValueKind.Array:
                    var jsonArray = new JsonArray();

                    // Add jsonArray to current nodes:
                    currentNodes.Push(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonArray));

                    // Add end of array marker:
                    recursionStack.Push(new KeyValuePair <string?, JsonElement?>(null, null));

                    // Add elements to recursion stack. Reverse enumerate to keep items order:
                    foreach (JsonElement element in currentJsonElement.Value.EnumerateArray().Reverse())
                    {
                        recursionStack.Push(new KeyValuePair <string?, JsonElement?>(null, element));
                    }
                    break;

                case JsonValueKind.Number:
                    var jsonNumber = new JsonNumber(currentJsonElement.Value.GetRawText());
                    AddToParent(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonNumber), ref currentNodes, ref toReturn);
                    break;

                case JsonValueKind.String:
                    string?value = currentJsonElement.Value.GetString();
                    Debug.Assert(value != null);
                    var jsonString = new JsonString(value);
                    AddToParent(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonString), ref currentNodes, ref toReturn);
                    break;

                case JsonValueKind.True:
                    var jsonBooleanTrue = new JsonBoolean(true);
                    AddToParent(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonBooleanTrue), ref currentNodes, ref toReturn);
                    break;

                case JsonValueKind.False:
                    var jsonBooleanFalse = new JsonBoolean(false);
                    AddToParent(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonBooleanFalse), ref currentNodes, ref toReturn);
                    break;

                case JsonValueKind.Null:
                    var jsonNull = JsonNull.Instance;
                    AddToParent(new KeyValuePair <string?, JsonNode?>(currentPair.Key, jsonNull), ref currentNodes, ref toReturn);
                    break;

                default:
                    Debug.Assert(jsonElement.ValueKind == JsonValueKind.Undefined, "No handler for JsonValueKind.{jsonElement.ValueKind}");
                    throw ThrowHelper.GetJsonElementWrongTypeException(JsonValueKind.Undefined, jsonElement.ValueKind);
                }
            }

            Debug.Assert(toReturn != null);

            return(toReturn);
        }
Пример #16
0
        private async Task <ResourceCommandResult> OperationDoWork(Workspace workspace, ResourceOperation operation, string[] addresses, string args)
        {
            var         errors     = new List <string>();
            JsonElement?outputs    = null;
            var         workingDir = workspace.GetPath(_terraformOptions.RootWorkingDirectory);
            var         files      = await _db.GetWorkspaceFiles(workspace, workspace.Directory);

            await workspace.PrepareFileSystem(workingDir, files);

            var initResult = _terraformService.InitializeWorkspace(workspace, null);

            var statePath = string.Empty;

            if (!workspace.IsDefault)
            {
                statePath = workspace.GetStatePath(workingDir, backupState: false);
            }

            if (!initResult.IsError)
            {
                TerraformResult result = null;

                switch (operation)
                {
                case ResourceOperation.taint:
                case ResourceOperation.untaint:
                    foreach (string address in addresses)
                    {
                        TerraformResult taintResult = null;

                        switch (operation)
                        {
                        case ResourceOperation.taint:
                            taintResult = _terraformService.Taint(workspace, address, statePath);
                            break;

                        case ResourceOperation.untaint:
                            taintResult = _terraformService.Untaint(workspace, address, statePath);
                            break;
                        }

                        if (taintResult != null && taintResult.IsError)
                        {
                            errors.Add(taintResult.Output);
                        }
                    }
                    break;

                case ResourceOperation.refresh:
                    result = _terraformService.Refresh(workspace, statePath);
                    break;

                case ResourceOperation.remove:
                    result = _terraformService.RemoveResources(workspace, addresses, statePath);
                    break;

                case ResourceOperation.import:
                    result = _terraformService.Import(workspace, addresses[0], args, statePath);
                    break;

                case ResourceOperation.output:
                    result  = _terraformService.GetOutputs(workspace, statePath);
                    outputs = JsonDocument.Parse(result.Output).RootElement;
                    break;
                }

                if (result != null && result.IsError)
                {
                    errors.Add(result.Output);
                }

                await workspace.RetrieveState(workingDir);

                await _db.SaveChangesAsync();

                workspace.CleanupFileSystem(_terraformOptions.RootWorkingDirectory);
            }

            return(new ResourceCommandResult
            {
                Resources = _mapper.Map <Resource[]>(workspace.GetState().GetResources(), opts => opts.ExcludeMembers(nameof(Resource.Attributes))),
                Errors = errors.ToArray(),
                Outputs = outputs
            });
        }
Пример #17
0
        internal static object ParseEvaluateResult(JsonElement?element, Type t)
        {
            var genericMethod = _parseEvaluateResult.MakeGenericMethod(t);

            return(genericMethod.Invoke(null, new object[] { element }));
        }
Пример #18
0
 public QueryExpressionNode(JsonElement value)
 {
     _value = value;
 }
Пример #19
0
        internal CloudEvent(string id, string source, string type, DateTimeOffset?time, string dataSchema, string dataContentType, string subject, JsonElement?serializedData, byte[] dataBase64)
        {
            Argument.AssertNotNull(id, nameof(id));
            Argument.AssertNotNull(source, nameof(source));
            Argument.AssertNotNull(type, nameof(type));

            Id                  = id;
            Source              = source;
            Type                = type;
            Time                = time;
            DataSchema          = dataSchema;
            DataContentType     = dataContentType;
            Subject             = subject;
            SerializedData      = serializedData;
            DataBase64          = dataBase64;
            ExtensionAttributes = new Dictionary <string, object>();
        }
Пример #20
0
 internal virtual void OnMessage(string method, JsonElement?serverParams)
 {
 }
Пример #21
0
 public static IEnumerable <JsonProperty> EnumerateObjectOrEmpty(this JsonElement?element)
 {
     return(element?.EnumerateObject() as IEnumerable <JsonProperty> ?? Array.Empty <JsonProperty>());
 }
Пример #22
0
        private void FindNodeRecursive <T>(PropertyInfo param, JsonElement parentNode, ref JsonElement?result)
        {
            if (result == null)
            {
                if (parentNode.TryGetProperty(param.Name, out JsonElement element) && param.PropertyType == typeof(T))
                {
                    result = (JsonElement?)element;
                    return;
                }
                else if (!param.PropertyType.IsPrimitive && param.PropertyType != typeof(string))
                {
                    Type subType = param.PropertyType;

                    PropertyInfo[] subparams = subType.GetProperties();
                    foreach (PropertyInfo sparam in subparams)
                    {
                        FindNodeRecursive <T>(sparam, parentNode.GetProperty(sparam.Name), ref result);
                    }
                }
            }
        }
Пример #23
0
 public EditProfile(JsonElement?_json)
 {
     LanguageJson = _json;
 }
Пример #24
0
        public async Task <IActionResult> AddMetric(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, ILogger log)
        {
            var root         = (await JsonDocument.ParseAsync(req.Body)).RootElement;
            var errorList    = new List <string>();
            var currentIndex = -1;
            var totalChanges = 0;

            if (DatabaseConnection == null)
            {
                log.LogError("No database connection initialized.");
                return(JsonError("Database failed to initialize."));
            }

            var transaction = await DatabaseConnection.BeginTransactionAsync();

            try
            {
                foreach (var package in root.EnumerateArray())
                {
                    ++currentIndex;

                    if (!package.TryGetProperty("package_url", out JsonElement packageUrlElt) ||
                        !package.TryGetProperty("key", out JsonElement keyElt) ||
                        !package.TryGetProperty("operation", out JsonElement operationElt))
                    {
                        errorList.Add($"Missing required field (index #{currentIndex})");
                        continue;
                    }
                    var packageUrl = new PackageURL(packageUrlElt.GetString());
                    var key        = keyElt.GetString();

                    var operation = operationElt.GetString();
                    if (operation != "insert" && operation != "replace")
                    {
                        errorList.Add($"Invalid operation specified (index #{currentIndex})");
                        continue;
                    }

                    if (!package.TryGetProperty("values", out JsonElement valuesElt))
                    {
                        errorList.Add($"No values specified (index #{currentIndex})");
                        continue;
                    }

                    if (operation == "replace")
                    {
                        totalChanges += await DoClearMetrics(key, packageUrl, transaction);
                    }

                    foreach (var item in valuesElt.EnumerateArray())
                    {
                        var timestamp = DateTime.Now;
                        if (item.TryGetProperty("timestamp", out JsonElement timestampElement))
                        {
                            timestampElement.TryGetDateTime(out timestamp);
                        }
                        ;
                        if (!item.TryGetProperty("value", out JsonElement valueElt))
                        {
                            continue;
                        }
                        JsonElement?properties = null;
                        if (item.TryGetProperty("properties", out JsonElement propertiesElt))
                        {
                            properties = (JsonElement?)propertiesElt;
                        }

                        var value = valueElt.ToString();
                        try
                        {
                            totalChanges += await InsertMetricAsync(packageUrl, key, value, timestamp, properties, log, transaction);
                        }
                        catch (Exception ex)
                        {
                            errorList.Add($"Error inserting into {key}: {ex.Message}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                errorList.Add($"Error processing payload, rolling back: {ex.Message}");
                transaction.Rollback();
            }

            try
            {
                log.LogInformation("Committing transaction.");
                transaction.Commit();
            }
            catch (Exception)
            {
                // ignore OK
            }

            if (errorList.Any())
            {
                return(JsonError(string.Join(";", errorList)));
            }
            else
            {
                return(JsonSuccess($"Updated {totalChanges} records.", null));
            }
        }
Пример #25
0
    public void ShouldNotMap_From_Nullable_JsonElement_To_JArray_Given_Invalid_Element(JsonElement?element)
    {
        var item = new JsonElementA()
        {
            Bar = element
        };
        Action a = () => Mapper.Map <JArrayA>(item);

        a.Should().Throw <AutoMapperMappingException>();
    }
Пример #26
0
 private static async Task <int> InsertMetricAsync(PackageURL packageUrl, string key, string value, DateTime timestamp, JsonElement?properties, ILogger log, NpgsqlTransaction transaction)
 {
     await using var cmdIns = new NpgsqlCommand("INSERT INTO metrics (package_url, key, properties, value, timestamp) VALUES (@package_url, @key, @properties, @value, @timestamp)", transaction.Connection, transaction);
     cmdIns.Parameters.AddWithValue("@package_url", packageUrl.ToString());
     cmdIns.Parameters.AddWithValue("@key", key);
     if (properties.HasValue)
     {
         cmdIns.Parameters.Add(new NpgsqlParameter("@properties", NpgsqlTypes.NpgsqlDbType.Jsonb)
         {
             Value = properties
         });
     }
     else
     {
         cmdIns.Parameters.AddWithValue("@properties", DBNull.Value);
     }
     cmdIns.Parameters.AddWithValue("@value", value);
     cmdIns.Parameters.AddWithValue("@timestamp", timestamp);
     return(await cmdIns.ExecuteNonQueryAsync());
 }
Пример #27
0
        /// <summary>
        /// This is used to resolve package references by looking up the package IDs in the asset file created
        /// by the NuGet Restore task.
        /// </summary>
        /// <param name="referencesToResolve">The package references to resolve</param>
        /// <returns>An enumerable list of assembly names.</returns>
        /// <remarks>If a package has dependencies, those will be resolved and returned as well</remarks>
        private IEnumerable <string> ResolvePackageReferencesInternal(IEnumerable <string> referencesToResolve)
        {
            HashSet <string> references = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var p in referencesToResolve)
            {
                try
                {
                    JsonElement?match       = null;
                    string      packageName = p;

                    resolvedDependencies.Add(packageName);

                    // If we don't get a match, try it with ".0" on the end.  Sometimes the reference version
                    // leaves it off.
                    if (!packages.Value.Value.TryGetProperty(packageName, out JsonElement m))
                    {
                        packageName += ".0";

                        if (packages.Value.Value.TryGetProperty(packageName, out m))
                        {
                            resolvedDependencies.Add(packageName);
                            match = m;
                        }
                    }
                    else
                    {
                        match = m;
                    }

                    if (match != null)
                    {
                        JsonElement?assemblyInfo = null;

                        if (match.Value.TryGetProperty("compile", out JsonElement c))
                        {
                            assemblyInfo = c;
                        }
                        else
                        {
                            if (match.Value.TryGetProperty("runtime", out JsonElement r))
                            {
                                assemblyInfo = r;
                            }
                        }

                        if (assemblyInfo != null)
                        {
                            foreach (var assemblyName in assemblyInfo.Value.EnumerateObject())
                            {
                                // Ignore mscorlib.dll as it's types will have been redirected elsewhere so we
                                // don't need it.  "_._" occurs in the framework SDK packages and we can ignore
                                // it too.
                                if (!assemblyName.Name.EndsWith("/mscorlib.dll", StringComparison.OrdinalIgnoreCase) &&
                                    !assemblyName.Name.EndsWith("_._", StringComparison.Ordinal))
                                {
                                    references.Add(Path.Combine(packageName, assemblyName.Name));
                                }
                            }
                        }

                        if (match.Value.TryGetProperty("dependencies", out JsonElement dependencies))
                        {
                            var deps = dependencies.EnumerateObject().Select(t => t.Name + "/" + t.Value).Where(
                                d => !resolvedDependencies.Contains(d)).ToList();

                            if (deps.Count != 0)
                            {
                                // Track the ones we've seen to prevent getting stuck due to circular references
                                resolvedDependencies.UnionWith(deps);
                                references.UnionWith(this.ResolvePackageReferencesInternal(deps));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // We won't prevent the build from continuing if there's an error.  We'll just report it.
                    System.Diagnostics.Debug.WriteLine(ex);

                    buildProcess.ReportWarning("BE0011", "Unable to load package reference information for " +
                                               "'{0}' in '{1}'.  Reason: {2}", p, projectFilename, ex.Message);
                }
            }

            return(references);
        }
Пример #28
0
        public override void Run(DataPack pack, JsonElement?config, Output output)
        {
            if (!ValidateConfig(config))
            {
                output.InvalidConfiguration <Identifier>();
                return;
            }

            foreach (var ns in pack.Namespaces)
            {
                string   foundPrefix  = null;
                Function foundPrefixF = null;
                Command  foundPrefixC = null;

                foreach (var function in ns.Functions)
                {
                    foreach (var command in function.CommandsFlat)
                    {
                        if (command.ContentType != Command.Type.Command)
                        {
                            continue;
                        }
                        var(prefix, isNamespace) = FindIdentifierPrefix(command);
                        if (prefix == null)
                        {
                            continue;
                        }
                        else if (prefix == "")
                        {
                            output.Error(command, "Identifier in command does not have a prefix.");
                            continue;
                        }

                        bool configMatch = config != null && (
                            isNamespace && config.Value.GetProperty("namespaces").EnumerateArray().Any(v => v.GetString() == prefix) ||
                            !isNamespace && config.Value.GetProperty("prefixes").EnumerateArray().Any(v => v.GetString() == prefix));
                        bool append = config == null || config.Value.GetProperty("extend").GetBoolean();

                        if (!PrefixAllowed(prefix, isNamespace, foundPrefix, append, configMatch, ns))
                        {
                            if (isNamespace && append)
                            {
                                output.Error(command, $"Identifier namespace does not match function namespace or any of the ones in the config file.");
                            }
                            else if (isNamespace)
                            {
                                output.Error(command, $"Identifier namespace does not match any of the ones in the config file.");
                            }
                            else if (foundPrefix != null)
                            {
                                output.Error(command, $"Identifier prefix does not match {foundPrefix} found on {foundPrefixF.NamespacedIdentifier} line {foundPrefixC.Line} or any of the ones in the config file.");
                            }
                            else
                            {
                                output.Error(command, $"Identifier prefix does not match any of the ones in the config file.");
                            }
                        }
                        else if (!isNamespace && foundPrefix == null && append && !configMatch)
                        {
                            foundPrefix  = prefix;
                            foundPrefixF = function;
                            foundPrefixC = command;
                        }
                    }
                }
            }
        }
Пример #29
0
 internal JsonArray(JsonElement element, JsonNodeOptions?options = null) : base(options)
 {
     Debug.Assert(element.ValueKind == JsonValueKind.Array);
     _jsonElement = element;
 }
Пример #30
0
        /// <summary>
        /// Unwraps data from JsonElement.
        /// </summary>
        /// <param name="propertyInfos">For each property in JsonElement: Key = name of the property, Value = Type of the property.</param>
        /// <param name="jsonRequest">JsonElement to unwrap.</param>
        /// <returns>Dictionary where: Key = name of the property, Value = value unwrapped from JsonElement</returns>
        public Dictionary <string, object> UnwrapJsonRequest(Dictionary <string, Type> propertyInfos, JsonElement jsonElement)
        {
            // Validate keys
            if (propertyInfos == null)
            {
                throw new ArgumentNullException("keys");
            }

            Dictionary <string, object> results = new Dictionary <string, object>();

            // Repeat for every value in JSON
            foreach (KeyValuePair <string, Type> keyValuePair in propertyInfos)
            {
                JsonElement?singleJsonValue = null;
                // Unwrap into separate JSON
                try
                {
                    singleJsonValue = jsonElement.GetProperty(keyValuePair.Key);
                }
                catch (KeyNotFoundException ex)
                {
                    logger.LogError(
                        ex,
                        "Values in jsonElement do NOT correspond with keys.",
                        keyValuePair.Key.ToString(),
                        keyValuePair.Value.ToString()
                        );
                }
                catch (InvalidOperationException ex)
                {
                    logger.LogError(
                        ex,
                        "Values in jsonElement are of a wrong kind.",
                        keyValuePair.Key.ToString(),
                        keyValuePair.Value.ToString()
                        );
                }
                catch (ArgumentNullException ex)
                {
                    logger.LogError(
                        ex,
                        "keyValuePair.Key is null.",
                        keyValuePair.Key.ToString(),
                        keyValuePair.Value.ToString()
                        );
                }

                // If we fail to acquire value, stop everything
                if (singleJsonValue == null)
                {
                    return(null);
                }

                // Prepare call to GetObject method
                MethodInfo method        = GetType().GetMethod("GetObject", BindingFlags.NonPublic | BindingFlags.Instance);
                MethodInfo genericMethod = null;
                // Set generic parameter type for GetObject method
                try
                {
                    genericMethod = method.MakeGenericMethod(keyValuePair.Value);
                }
                catch (ArgumentNullException ex)
                {
                    logger.LogError(ex, "Type passed in Dictionary.Value is null.", keyValuePair.Key.ToString(), keyValuePair.Value.ToString());
                }
                catch (ArgumentException ex)
                {
                    logger.LogError(ex, "Type passed in Dictionary.Value is no correct.", keyValuePair.Key.ToString(), keyValuePair.Value.ToString());
                }

                // If we fail to set correct generic Type, stop everything
                if (genericMethod == null)
                {
                    return(null);
                }

                object result = null;
                // Call the method
                try
                {
                    result = genericMethod.Invoke(this, new object[] { singleJsonValue });
                }
                catch (TargetInvocationException ex)
                {
                    logger.LogError(
                        ex,
                        "Unexpected exception occurred during JSON unwrapping.",
                        keyValuePair.Key.ToString(),
                        keyValuePair.Value.ToString());
                }

                // If we fail to unwrap JSON, stop everything
                if (result == null)
                {
                    return(null);
                }

                // Add returned value to result with its corresponding key
                results.Add(keyValuePair.Key, result);
            }
            return(results);
        }