Skip to main content
Notice removed Draw attention by isic5
Bounty Ended with tyg's answer chosen by isic5
Notice added Draw attention by isic5
Bounty Started worth 100 reputation by isic5
added 6 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

ImI'm not really happy with the below solution. Background is that I am receiving results from a graphql query that includeincludes tags, and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node, I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im. I'm not sure if I like having additional logic in an enum like that. Also I am not happy that my toDto function still contains quite a bit of logic to achieve the final result of assigning a weight to the dto.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.filter { tag -> tag.name == "tier1" || tag.name == "tier2" }
        .map { tag -> Weight.fromTagName(tag.name) }
        .firstOrNull()?.value ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }

Im not really happy with the below solution. Background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that. Also I am not happy that my toDto function still contains quite a bit of logic to achieve the final result of assigning a weight to the dto.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.filter { tag -> tag.name == "tier1" || tag.name == "tier2" }
        .map { tag -> Weight.fromTagName(tag.name) }
        .firstOrNull()?.value ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }

I'm not really happy with the below solution. Background is that I am receiving results from a graphql query that includes tags, and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node, I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this. I'm not sure if I like having additional logic in an enum like that. Also I am not happy that my toDto function still contains quite a bit of logic to achieve the final result of assigning a weight to the dto.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.filter { tag -> tag.name == "tier1" || tag.name == "tier2" }
        .map { tag -> Weight.fromTagName(tag.name) }
        .firstOrNull()?.value ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }
added 202 characters in body
Source Link
isic5
  • 95
  • 6

Im not really happy with the below solution. Background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that. Also I am not happy that my toDto function still contains quite a bit of logic to achieve the final result of assigning a weight to the dto.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.mapfilter { tag -> tag.name == "tier1" || tag.name == "tier2" }
        .map { tag -> Weight.fromTagName(tag.name).value }
        }.firstOrNull()?.value ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }

Im not really happy with the below solution. Background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.map { tag ->
            Weight.fromTagName(tag.name).value
        }.firstOrNull() ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }

Im not really happy with the below solution. Background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that. Also I am not happy that my toDto function still contains quite a bit of logic to achieve the final result of assigning a weight to the dto.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.filter { tag -> tag.name == "tier1" || tag.name == "tier2" }
        .map { tag -> Weight.fromTagName(tag.name) }
        .firstOrNull()?.value ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }
added 125 characters in body
Source Link
isic5
  • 95
  • 6

Im not really happy with the below solution, background. Background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.map { tag ->
            Weight.fromTagName(tag.name).value
        }.firstOrNull() ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }

Im not really happy with the below solution, background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.map { tag ->
            Weight.fromTagName(tag.name).value
        }.firstOrNull() ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }

Im not really happy with the below solution. Background is that I am receiving results from a graphql query that include tags and I want to translate these tags into weights and save those results with the weights in my db. Important to note is that in the case where no matching tag is found in a given node I want to set the default weight.

Currently I put that logic into an enum class that I am then calling from my mapper. I wonder if there is a better way to achieve this, Im not sure if I like having additional logic in an enum like that.

enum class Weight(val value: Int) {
    LEVEL1(3),
    LEVEL2(2),
    DEFAULT(1);

    companion object {
        fun fromTagName(tagName: String): Weight {
            return when (tagName) {
                "level1" -> LEVEL1
                "level2" -> LEVEL2
                else -> DEFAULT
            }
        }
    }
}


fun toDto(node: BaseNode): Result<Dto> =
    Result.catch {
        val weight = node.tags.map { tag ->
            Weight.fromTagName(tag.name).value
        }.firstOrNull() ?: Weight.DEFAULT.value

        Dto(
            name = provider.name,
            weight = weight
        )
    }
Source Link
isic5
  • 95
  • 6
Loading