Skip to content

fix!: handle sql/driver.Valuer types properly in slogjson #219

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

Merged
merged 3 commits into from
Jul 3, 2025

Conversation

ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Jul 3, 2025

Currently, if a field like sql.NullInt32 has Valid: False, sloghuman will export it's value as <nil>, regardless of it's String.
This is because it checks (driver.Valuer).Value().

However, slogjson currently sets the value to the json string of the raw struct:

{
  "fields": {
      "Code": "{Int32:0 Valid:false}",
      "ValidCode": "{Int32:12 Valid:true}"
  }
}

This PR handles this case by first checking if the type implements sql/driver.Valuer. If Valid is false then a JSON null value is produced:

{
  "fields": {
      "Code": null,
      "ValidCode": 12
  }
}

This matches the behaviour of sloghuman.

This is technically a breaking change, as these types are now T | null instead of String, where T is the corresponding JSON type of sql.Null<V>

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@ethanndickson ethanndickson marked this pull request as ready for review July 3, 2025 05:56
@coveralls
Copy link

coveralls commented Jul 3, 2025

Pull Request Test Coverage Report for Build bba9d76dbd5596a5a6dde53f28ff7ec985d4b3fa-PR-219

Details

  • 4 of 6 (66.67%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 96.822%

Changes Missing Coverage Covered Lines Changed/Added Lines %
map.go 4 6 66.67%
Totals Coverage Status
Change from base Build 0ec81e6e67bb1fe88d4d068a6c7325f449008201: -0.2%
Covered Lines: 792
Relevant Lines: 818

💛 - Coveralls

@ethanndickson ethanndickson changed the title fix!: handle sql/driver.Valuer types properly in slogjson fix!: handle sql/driver.Valuer types properly in slogjson Jul 3, 2025
@ethanndickson ethanndickson merged commit 9df5e0a into main Jul 3, 2025
2 checks passed
@ethanndickson ethanndickson deleted the ethan/handle-sql-types-slogjson branch July 3, 2025 07:42
ethanndickson added a commit to coder/coder that referenced this pull request Jul 7, 2025
This bumps `slog` to incorporate coder/slog#219.

Before:
```json
{
  "fields": {
      "Code": "{Int32:0 Valid:false}",
      "ValidCode": "{Int32:12 Valid:true}"
  }
}
```

After:
```json
{
  "fields": {
      "Code": null,
      "ValidCode": 12
  }
}
```
stirby pushed a commit to coder/coder that referenced this pull request Jul 7, 2025
This bumps `slog` to incorporate coder/slog#219.

Before:
```json
{
  "fields": {
      "Code": "{Int32:0 Valid:false}",
      "ValidCode": "{Int32:12 Valid:true}"
  }
}
```

After:
```json
{
  "fields": {
      "Code": null,
      "ValidCode": 12
  }
}
```
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.

4 participants