charleskorn / kaml

YAML support for kotlinx.serialization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accumulate duplicated keys

MiSikora opened this issue · comments

In case of multiple key duplicates I would like to see information about all of them and not first one encountered. Would it be possible to make it a configurable behaviour? I can make a PR if it is something you see fitting for the library.

Hi @MiSikora, if you're interested in making a PR, that would be a great enhancement. I'm not sure we need to make it configurable - I'm thinking we can just always report all duplicates. What do you think?

Sure, if you think it would be a good default I'm all for it.

I played around with it for a while today and I have couple of questions.

public open class YamlException(
override val message: String,
public val path: YamlPath,
override val cause: Throwable? = null
) : SerializationException(message, cause) {
public val location: Location = path.endLocation
public val line: Int = location.line
public val column: Int = location.column
override fun toString(): String = "${this::class.qualifiedName} at ${path.toHumanReadableString()} on line $line, column $column: $message"
}

Is toString() override on YamlException necessary? Exceptions have messages and overriding toString() seems a bit overzealous. Also, are path, line and column properties needed? With them present I find it hard to represent a cumulative error result and I don't want to change each property to a collection just for the sake of it.

I could create some other base exception and make YamlException and DuplicateKeysException inherit from it, but I don't know what you think of it (to me it seems a bit contrived compared to removing properties from the base exception). It would be more or less something like this.

// New exception
public open class YamlException(
  override val message: String,
  override val cause: Throwable? = null,
) : SerializationException(message, cause)

// Current YamlException
public open class SinglePathYamlException(
  override val message: String,
  public val path: YamlPath,
  override val cause: Throwable? = null,
) : YamlException(message, cause)

// New exception for multiple duplicate keys, probably remove DuplicateKeyException
public class DuplicateKeysException : YamlException

Great question.

I'd like to preserve the path and location (line and column) information wherever possible - this is really helpful when presenting information about the error to a user, and I know of at least one consumer of the library that uses this information extensively. For example, being able to say "property 'blah' on line 2, column 5 has value 'abc' which is not a valid boolean value" is useful information for a human.

I think this is the first case where we can't definitively provide one path and location as the source of the error (the previous DuplicateKeyException worked around this with the originalPath and duplicatePath properties which was not quite right), so I think the idea of having a base exception class with no paths or locations, and then a class for exceptions with a single path and another for duplicate keys is OK.

commented

This issue has been automatically marked as stale because it has not had any activity in the last 60 days. It will automatically be closed if no further activity occurs in the next seven days to enable maintainers to focus on the most important issues.
If this issue is still affecting you, please comment below within the next seven days.
Thank you for your contributions.

commented

This issue has been automatically closed because it has not had any recent activity.