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

get operator for YamlMap #32

Closed
Vampire opened this issue Aug 24, 2020 · 4 comments
Closed

get operator for YamlMap #32

Vampire opened this issue Aug 24, 2020 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Vampire
Copy link
Contributor

Vampire commented Aug 24, 2020

I'd like to suggest a get operator for YamlMap.
It proved useful for me, so you might want to add this to the YamlMap.

operator fun <T : YamlNode> YamlMap.get(key: String) =
    entries
        .asSequence()
        .find { (it.key as? YamlScalar)?.content == key }
        ?.value as T?

It enables things like:

val runsMaybe: YamlMap? = yamlMap["runs"]
val runs: YamlMap = yamlMap["runs"]!!
val using = yamlMap.get<YamlMap>("runs")!!.get<YamlScalar>("using")!!.content
@Vampire
Copy link
Contributor Author

Vampire commented Aug 24, 2020

And this here is also handy if you need to get the key for a map entry:

fun YamlMap.getKey(value: YamlNode) =
    entries
        .asSequence()
        .find { it.value == value }
        ?.key
        ?.let { it as? YamlScalar }
        ?.content

@charleskorn
Copy link
Owner

Hey @Vampire, makes sense to me, thanks for the feedback.

@charleskorn charleskorn added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Aug 25, 2020
@Vampire
Copy link
Contributor Author

Vampire commented Jul 11, 2022

Hi there, did you not like the second one, or just overlooked it?
In the current version it would be

fun YamlMap.getKey(value: YamlNode) =
    entries
        .asSequence()
        .find { it.value == value }
        ?.key
        ?.content

@charleskorn
Copy link
Owner

@Vampire I'm not keen to add a getKey method as it seems like an unusual use case with a simple-enough alternative for those that really need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants