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

Codable extension for Logger.MetadataValue #334

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

bwdmr
Copy link

@bwdmr bwdmr commented Nov 8, 2024

This Codable extension for Logger.MetadataValue provides encoding and decoding functionality, mapping MetadataValue cases (string, stringConvertible, dictionary, array) into JSON-compatible formats using the associated type and value keys

  • Container Encoding:
    • Each case of MetadataValue is encoded with an explicit type (ValueType) and corresponding value.
    • stringConvertible is encoded using .description since we need it in a String format.
  • Container Decoding
    • When decoding, ValueType is first extracted to determine the case.
    • For stringConvertible, the String value is decoded and then assigned back to stringConvertible.
    • Dictionary and array cases are decoded as Logger.Metadata and [Logger.MetadataValue] respectively.

… decoding functionality, mapping MetadataValue cases (string, stringConvertible, dictionary, array) into JSON-compatible formats using the associated type and value keys

* Container Encoding:
  * Each case of MetadataValue is encoded with an explicit type (ValueType) and corresponding value.
  * stringConvertible is encoded using .description since we need it in a String format.
* Container Decoding
  * When decoding, ValueType is first extracted to determine the case.
  * For stringConvertible, the String value is decoded and then assigned back to stringConvertible.
  * Dictionary and array cases are decoded as Logger.Metadata and [Logger.MetadataValue] respectively.
@ktoso ktoso added the semver/patch No public API change. label Nov 18, 2024
@ktoso
Copy link
Member

ktoso commented Nov 18, 2024

I think that's fine but I'd like to request a test (can pull in foundation and use JSON), so that we check this is covered.

@FranzBusch
Copy link
Member

I am not yet convinced this is the right thing to do. Encoding enums is always a bit tricky and results in interesting JSON formats. Not saying we shouldn't do this but I would like to understand the motivation a bit more here.

@bwdmr
Copy link
Author

bwdmr commented Nov 18, 2024

generally meant for metadatavalue to be compatible with codable.
it was meant for cases like string

for enums would imply a codable constraint. enforcing a custom implementation each time one would rely on it.
i dont know how excessive this requirement is.

might be too much to ask. what do you guys think

@FranzBusch
Copy link
Member

generally meant for metadatavalue to be compatible with codable. it was meant for cases like string

for enums would imply a codable constraint. enforcing a custom implementation each time one would rely on it. i dont know how excessive this requirement is.

might be too much to ask. what do you guys think

Thanks for explaining your intention. I remain unconvinced that this is the right thing to do. Specifically because the way we encode this is very custom to how the Swift type is structured. I also don't see this as something that is blocking people from writing their own backends that have to emit metadata values as JSON. We have many log handler backends that do this already and they often hand roll the JSON emission for better performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants