-
Notifications
You must be signed in to change notification settings - Fork 132
Use DTO for uv.lock parsing #1851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ryan Brandenburg (ryanbrandenburg)
wants to merge
2
commits into
main
Choose a base branch
from
rybrande/uvlock_toml_dto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Uv; | ||
|
|
||
| using System.Runtime.Serialization; | ||
|
|
||
| [DataContract] | ||
| internal class UvDependency | ||
| { | ||
| public required string Name { get; init; } | ||
| [DataMember(Name = "name")] | ||
| public string Name { get; set; } = string.Empty; | ||
|
|
||
| [DataMember(Name = "specifier")] | ||
| public string? Specifier { get; set; } | ||
| } | ||
137 changes: 16 additions & 121 deletions
137
src/Microsoft.ComponentDetection.Detectors/uv/UvLock.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,146 +1,41 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Uv; | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Runtime.Serialization; | ||
| using Tomlyn; | ||
| using Tomlyn.Model; | ||
|
|
||
| [DataContract] | ||
| internal class UvLock | ||
| { | ||
| // a list of packages with their dependencies | ||
| // A list of packages with their dependencies. | ||
| [DataMember(Name = "package")] | ||
| public List<UvPackage> Packages { get; set; } = []; | ||
|
ryanbrandenburg marked this conversation as resolved.
|
||
|
|
||
| // static method to parse the TOML stream into a UvLock model | ||
| public static UvLock Parse(Stream tomlStream) | ||
| { | ||
| using var reader = new StreamReader(tomlStream); | ||
| var tomlContent = reader.ReadToEnd(); | ||
| var model = Toml.ToModel(tomlContent); | ||
| return new UvLock | ||
| var options = new TomlModelOptions | ||
| { | ||
| Packages = ParsePackagesFromModel(model), | ||
| IgnoreMissingProperties = true, | ||
| }; | ||
| } | ||
|
|
||
| internal static List<UvPackage> ParsePackagesFromModel(object? model) | ||
| { | ||
| if (model is not TomlTable table) | ||
| { | ||
| throw new InvalidOperationException("TOML root is not a table"); | ||
| } | ||
|
|
||
| if (!table.TryGetValue("package", out var packagesObj) || packagesObj is not TomlTableArray packages) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| var result = new List<UvPackage>(); | ||
| foreach (var pkg in packages) | ||
| { | ||
| var parsed = ParsePackage(pkg); | ||
| if (parsed is not null) | ||
| { | ||
| result.Add(parsed); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| internal static UvPackage? ParsePackage(object? pkg) | ||
| { | ||
| if (pkg is not TomlTable pkgTable) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (pkgTable.TryGetValue("name", out var nameObj) && nameObj is string name && | ||
| pkgTable.TryGetValue("version", out var versionObj) && versionObj is string version) | ||
| { | ||
| var uvPackage = new UvPackage | ||
| { | ||
| Name = name, | ||
| Version = version, | ||
| Dependencies = [], | ||
| MetadataRequiresDist = [], | ||
| MetadataRequiresDev = [], | ||
| }; | ||
|
|
||
| if (pkgTable.TryGetValue("dependencies", out var depsObj) && depsObj is TomlArray depsArray) | ||
| { | ||
| uvPackage.Dependencies = ParseDependenciesArray(depsArray); | ||
| } | ||
|
|
||
| if (pkgTable.TryGetValue("metadata", out var metadataObj) && metadataObj is TomlTable metadataTable) | ||
| { | ||
| ParseMetadata(metadataTable, uvPackage); | ||
| } | ||
|
|
||
| // Parse source | ||
| if (pkgTable.TryGetValue("source", out var sourceObj) && sourceObj is TomlTable sourceTable) | ||
| { | ||
| var source = new UvSource | ||
| { | ||
| Registry = sourceTable.TryGetValue("registry", out var regObj) && regObj is string reg ? reg : null, | ||
| Virtual = sourceTable.TryGetValue("virtual", out var virtObj) && virtObj is string virt ? virt : null, | ||
| Git = sourceTable.TryGetValue("git", out var gitObj) && gitObj is string git ? git : null, | ||
| }; | ||
| uvPackage.Source = source; | ||
| } | ||
|
|
||
| return uvPackage; | ||
| } | ||
|
|
||
| return null; | ||
| var parsed = Toml.ToModel<UvLock>(tomlContent, options: options); | ||
| parsed.Normalize(); | ||
| return parsed; | ||
| } | ||
|
|
||
| internal static List<UvDependency> ParseDependenciesArray(TomlArray? depsArray) | ||
| private void Normalize() | ||
| { | ||
| var deps = new List<UvDependency>(); | ||
| if (depsArray is null) | ||
| { | ||
| return deps; | ||
| } | ||
|
|
||
| foreach (var dep in depsArray) | ||
| { | ||
| if (dep is TomlTable depTable && | ||
| depTable.TryGetValue("name", out var depNameObj) && depNameObj is string depName) | ||
| { | ||
| var depSpec = depTable.TryGetValue("specifier", out var specObj) && specObj is string s ? s : null; | ||
| deps.Add(new UvDependency | ||
| { | ||
| Name = depName, | ||
| Specifier = depSpec, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return deps; | ||
| } | ||
|
|
||
| internal static void ParseMetadata(TomlTable? metadataTable, UvPackage uvPackage) | ||
| { | ||
| if (metadataTable is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (metadataTable.TryGetValue("requires-dist", out var requiresDistObj) && requiresDistObj is TomlArray requiresDistArr) | ||
| { | ||
| uvPackage.MetadataRequiresDist = ParseDependenciesArray(requiresDistArr); | ||
| } | ||
| this.Packages = this.Packages | ||
| .Where(p => !string.IsNullOrWhiteSpace(p.Name) && !string.IsNullOrWhiteSpace(p.Version)) | ||
| .ToList(); | ||
|
|
||
| if (metadataTable.TryGetValue("requires-dev", out var requiresDevObj) && requiresDevObj is TomlTable requiresDevTable) | ||
| foreach (var package in this.Packages) | ||
| { | ||
| foreach (var kvp in requiresDevTable) | ||
| { | ||
| if (kvp.Value is TomlArray groupArr) | ||
| { | ||
| uvPackage.MetadataRequiresDev.AddRange(ParseDependenciesArray(groupArr)); | ||
| } | ||
| } | ||
| package.Normalize(); | ||
| } | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
src/Microsoft.ComponentDetection.Detectors/uv/UvMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Uv; | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Runtime.Serialization; | ||
|
|
||
| [DataContract] | ||
| internal class UvMetadata | ||
| { | ||
| [DataMember(Name = "requires-dist")] | ||
| public List<UvDependency> RequiresDist { get; set; } = []; | ||
|
|
||
| [DataMember(Name = "requires-dev")] | ||
| public Dictionary<string, List<UvDependency>> RequiresDev { get; set; } = []; | ||
|
ryanbrandenburg marked this conversation as resolved.
|
||
|
|
||
| public void Normalize() | ||
| { | ||
| this.RequiresDist = this.RequiresDist | ||
| .Where(d => !string.IsNullOrWhiteSpace(d.Name)) | ||
| .ToList(); | ||
|
|
||
| var normalizedDev = new Dictionary<string, List<UvDependency>>(this.RequiresDev.Count, this.RequiresDev.Comparer); | ||
| foreach (var (group, dependencies) in this.RequiresDev) | ||
| { | ||
| normalizedDev[group] = dependencies | ||
| .Where(d => !string.IsNullOrWhiteSpace(d.Name)) | ||
| .ToList(); | ||
| } | ||
|
|
||
| this.RequiresDev = normalizedDev; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| namespace Microsoft.ComponentDetection.Detectors.Uv; | ||
|
|
||
| using System.Runtime.Serialization; | ||
|
|
||
| [DataContract] | ||
| internal class UvSource | ||
| { | ||
| [DataMember(Name = "registry")] | ||
| public string? Registry { get; set; } | ||
|
|
||
| [DataMember(Name = "virtual")] | ||
| public string? Virtual { get; set; } | ||
|
|
||
| [DataMember(Name = "git")] | ||
| public string? Git { get; set; } | ||
|
ryanbrandenburg marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.