Skip to content
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

Adding support for json output #191

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

priorax
Copy link

@priorax priorax commented May 21, 2024

At the moment this project only supports output to a table. While fantastic for human readability, it leads something to be desired when it comes to machine readability.

Use:

  • dotnet libyear > Prints using table output in console dotnet libyear -o console > Prints using table output in console
  • dotnet run -o json > Prints indented json
  • dotnet libyear -o json -q > Prints non-indented json

This is currently useful to me as I would like to be able to run this project against several code bases in a single parse and easily collate the results.

Ideal state may also include hiding the progress bar when searching for results, but I was unsure if that would be valuable/helpful at all.

dependabot bot and others added 2 commits April 29, 2024 15:24
Bump xunit from 2.7.0 to 2.7.1

Bumps [xunit](https://github.com/xunit/xunit) from 2.7.0 to 2.7.1.
- [Commits](xunit/xunit@2.7.0...2.7.1)

---
updated-dependencies:
- dependency-name: xunit
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump xunit.runner.visualstudio from 2.5.7 to 2.5.8

Bumps [xunit.runner.visualstudio](https://github.com/xunit/visualstudio.xunit) from 2.5.7 to 2.5.8.
- [Release notes](https://github.com/xunit/visualstudio.xunit/releases)
- [Commits](xunit/visualstudio.xunit@2.5.7...2.5.8)

---
updated-dependencies:
- dependency-name: xunit.runner.visualstudio
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump Spectre.Console.Testing and Spectre.Console.Cli

Bumps [Spectre.Console.Testing](https://github.com/spectreconsole/spectre.console) and [Spectre.Console.Cli](https://github.com/spectreconsole/spectre.console). These dependencies needed to be updated together.

Updates `Spectre.Console.Testing` from 0.48.0 to 0.49.0
- [Release notes](https://github.com/spectreconsole/spectre.console/releases)
- [Commits](spectreconsole/spectre.console@0.48.0...0.49.0)

Updates `Spectre.Console.Cli` from 0.48.0 to 0.49.0
- [Release notes](https://github.com/spectreconsole/spectre.console/releases)
- [Commits](spectreconsole/spectre.console@0.48.0...0.49.0)

---
updated-dependencies:
- dependency-name: Spectre.Console.Testing
  dependency-type: direct:production
  update-type: version-update:semver-minor
- dependency-name: Spectre.Console.Cli
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump Spectre.Console.Cli from 0.48.0 to 0.49.0

Bumps [Spectre.Console.Cli](https://github.com/spectreconsole/spectre.console) from 0.48.0 to 0.49.0.
- [Release notes](https://github.com/spectreconsole/spectre.console/releases)
- [Commits](spectreconsole/spectre.console@0.48.0...0.49.0)

---
updated-dependencies:
- dependency-name: Spectre.Console.Cli
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump Spectre.Console.Cli from 0.49.0 to 0.49.1

Bumps [Spectre.Console.Cli](https://github.com/spectreconsole/spectre.console) from 0.49.0 to 0.49.1.
- [Release notes](https://github.com/spectreconsole/spectre.console/releases)
- [Commits](spectreconsole/spectre.console@0.49.0...0.49.1)

---
updated-dependencies:
- dependency-name: Spectre.Console.Cli
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump Spectre.Console.Testing from 0.49.0 to 0.49.1

Bumps [Spectre.Console.Testing](https://github.com/spectreconsole/spectre.console) from 0.49.0 to 0.49.1.
- [Release notes](https://github.com/spectreconsole/spectre.console/releases)
- [Commits](spectreconsole/spectre.console@0.49.0...0.49.1)

---
updated-dependencies:
- dependency-name: Spectre.Console.Testing
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump xunit from 2.7.1 to 2.8.0

Bumps [xunit](https://github.com/xunit/xunit) from 2.7.1 to 2.8.0.
- [Commits](xunit/xunit@2.7.1...2.8.0)

---
updated-dependencies:
- dependency-name: xunit
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

Bump xunit.runner.visualstudio from 2.5.8 to 2.8.0

Bumps [xunit.runner.visualstudio](https://github.com/xunit/visualstudio.xunit) from 2.5.8 to 2.8.0.
- [Release notes](https://github.com/xunit/visualstudio.xunit/releases)
- [Commits](xunit/visualstudio.xunit@2.5.8...2.8.0)

---
updated-dependencies:
- dependency-name: xunit.runner.visualstudio
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
At the moment this project only supports output to a table.
While fantastic for human readability, it leads something to be desired
when it comes to machine readability.

Use:
`dotnet libyear` > Prints using table output in console
`dotnet libyear -o console` > Prints using table output in console
`dotnet run -o json` > Prints indented json
`dotnet libyear -o json -q` > Prints non-indented json

This is currently useful to me as I would like to be able to run this
project against several code bases in a single parse and easily collate
the results.
Copy link
Member

@SteveDesmond-ca SteveDesmond-ca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Doug, thanks for taking the time to submit this PR!

Something of this size will likely have multiple iterations of review, just letting you know up front so there is a mutual understanding of commitment.

The big note to start off with is test coverage: can you please ensure that all new code branches have a unit test showing their intended correctness?

src/LibYear/App.cs Outdated Show resolved Hide resolved
src/LibYear/App.cs Outdated Show resolved Hide resolved
src/LibYear/App.cs Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/LibYear/App.cs Outdated Show resolved Hide resolved
src/LibYear/Output/PackageResult.cs Outdated Show resolved Hide resolved
src/LibYear/Settings.cs Outdated Show resolved Hide resolved
src/LibYear/Settings.cs Outdated Show resolved Hide resolved
src/LibYear/Output/JsonOutput.cs Outdated Show resolved Hide resolved
src/LibYear/Output/JsonOutput.cs Outdated Show resolved Hide resolved
Copy link
Member

@SteveDesmond-ca SteveDesmond-ca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes, we're getting there! I resolved a bunch of conversations and left the outstanding ones open.

Copy link
Member

@SteveDesmond-ca SteveDesmond-ca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, almost there!

README.md Outdated Show resolved Hide resolved
test/LibYear.Tests/Output/Json/JsonOutputTests.cs Outdated Show resolved Hide resolved
Comment on lines +17 to +18
CurrentVersion = result.Installed is null ? null : new DisplayVersion(result.Installed);
LatestVersion = result.Latest is null ? null : new DisplayVersion(result.Latest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we invert these ternaries (... is not null) to keep them in line with the others?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this one might be moot now if the whole class disappears)


namespace LibYear.Output.Json;

internal sealed class DoubleFormatter : JsonConverter<double>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some tests for this bad boy as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also name it DoubleConverter to match the convention of the base class and DateTimeConverter?

src/LibYear/Output/Json/DateTimeConverter.cs Outdated Show resolved Hide resolved
public string VersionNumber { get; init; } = string.Empty;
[JsonPropertyName("releaseDate")]
public DateTime ReleaseDate { get; init; }
public DisplayVersion(Release release)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use the underlying Release here? Might require a JsonConverter<PackageVersion> that returns its ToString()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to put full release there, but given everything is currently public, everything would get included
That would currently look like

{
  "YearsBehind": 0,
  "DaysBehind": 0,
  "Projects": [
    {
      "Project": "test project 1",
      "YearsBehind": 0,
      "Packages": [
        {
          "PackageName": "test1",
          "CurrentVersion": {
            "versionNumber": "1.2.3",
            "releaseDate": "2024-05-30",
            "Release": {
              "Version": {
                "WildcardType": 0,
                "Version": "1.2.3.0",
                "IsLegacyVersion": false,
                "Revision": 0,
                "IsSemVer2": false,
                "OriginalVersion": null,
                "Major": 1,
                "Minor": 2,
                "Patch": 3,
                "ReleaseLabels": [],
                "Release": "",
                "IsPrerelease": false,
                "HasMetadata": false,
                "Metadata": null
              },
              "Date": "2024-05-30",
              "IsPublished": true
            }
          }
        }
      ]
    }
  ]
}

Is there anything that would to be removed/hidden/etc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, should have been clearer here. Can we just use more of the existing model objects/classes in the JSON above?

  • Packages becomes an array of Result
  • Release (of which Result has 2, Installed and Latest) contains a PackageVersion
  • Could that PackageVersion use a JsonConverter<PackageVersion> that returns just the ToString() interpretation of the version? (I may not have a proper understanding of JsonConverter<T>)

So the JSON from above would look like:

{
  "YearsBehind": 0,
  "Projects": [
    {
      "Project": "test project 1",
      "YearsBehind": 0,
      "Packages": [
        {
          "Name": "test1",
          "Installed": {
            "Version": "1.2.3",
            "Date": "2024-05-30",
            "IsPublished": true
          },
          "Latest": {
            "Version": "1.2.3",
            "Date": "2024-05-30",
            "IsPublished": true
          }
        }
      ]
    }
  ]
}

Copy link
Member

@SteveDesmond-ca SteveDesmond-ca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So close, a big potential cleanup if my understanding is correct 🤞🏻

Comment on lines +11 to +13
Test = new DateTime(2020, 01, 01)
};
private static string ExpectedJson = @"{""Test"":""2020-01-01""}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make sure the month and day are different numbers, just to ensure that a change to yyyy-dd-MM should fail the tests


namespace LibYear.Tests.Output.Json;

public class DoubleConverterTests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this has a bunch of copypasta leftovers from DateTimeConverterTests 😅

Can we also make sure the test object contains a number with more than one decimal, so that we're testing the rounding properly?

Comment on lines +17 to +18
CurrentVersion = result.Installed is null ? null : new DisplayVersion(result.Installed);
LatestVersion = result.Latest is null ? null : new DisplayVersion(result.Latest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this one might be moot now if the whole class disappears)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants