|
| 1 | +/* |
| 2 | + * Copyright 2022, TeamDev. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Redistribution and use in source and/or binary forms, with or without |
| 11 | + * modification, must retain the above copyright notice and the following |
| 12 | + * disclaimer. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package io.spine.validation.java |
| 28 | + |
| 29 | +import io.spine.base.FieldPath |
| 30 | +import io.spine.protodata.ast.isList |
| 31 | +import io.spine.protodata.ast.isMap |
| 32 | +import io.spine.protodata.ast.name |
| 33 | +import io.spine.protodata.ast.qualifiedName |
| 34 | +import io.spine.protodata.java.CodeBlock |
| 35 | +import io.spine.protodata.java.Expression |
| 36 | +import io.spine.protodata.java.ReadVar |
| 37 | +import io.spine.protodata.java.StringLiteral |
| 38 | +import io.spine.protodata.java.call |
| 39 | +import io.spine.protodata.java.field |
| 40 | +import io.spine.protodata.java.toBuilder |
| 41 | +import io.spine.validate.ConstraintViolation |
| 42 | +import io.spine.validation.DistinctField |
| 43 | +import io.spine.validation.PATTERN |
| 44 | +import io.spine.validation.java.ErrorPlaceholder.FIELD_DUPLICATES |
| 45 | +import io.spine.validation.java.ErrorPlaceholder.FIELD_PATH |
| 46 | +import io.spine.validation.java.ErrorPlaceholder.FIELD_TYPE |
| 47 | +import io.spine.validation.java.ErrorPlaceholder.FIELD_VALUE |
| 48 | +import io.spine.validation.java.ErrorPlaceholder.PARENT_TYPE |
| 49 | +import io.spine.validation.java.ValidationCodeInjector.MessageScope.message |
| 50 | +import io.spine.validation.java.ValidationCodeInjector.ValidateScope.parentPath |
| 51 | + |
| 52 | +/** |
| 53 | + * The generator for `(distinct)` option. |
| 54 | + * |
| 55 | + * Generates code for a single field represented by the provided [view]. |
| 56 | + */ |
| 57 | +internal class DistinctFieldGenerator(private val view: DistinctField) { |
| 58 | + |
| 59 | + private val field = view.subject |
| 60 | + private val fieldType = field.type |
| 61 | + private val getter = message.field(field).getter<List<*>>() |
| 62 | + private val declaringType = field.declaringType |
| 63 | + |
| 64 | + fun generate(): FieldOptionCode { |
| 65 | + val collection = when { |
| 66 | + fieldType.isList -> getter |
| 67 | + fieldType.isMap -> getter.call("values") |
| 68 | + else -> error("...") |
| 69 | + } |
| 70 | + val set = ImmutableSetClass.call<Set<*>>("copyOf", collection) |
| 71 | + val constraint = CodeBlock( |
| 72 | + """ |
| 73 | + if ($getter.size() != $set.size()) { |
| 74 | + var duplicates = ${calculateDuplicates(collection)}; |
| 75 | + var fieldPath = ${fieldPath(parentPath)}; |
| 76 | + var violation = ${violation(ReadVar("fieldPath"), ReadVar("duplicates"))}; |
| 77 | + violations.add(violation); |
| 78 | + } |
| 79 | + """.trimIndent() |
| 80 | + ) |
| 81 | + return FieldOptionCode(constraint) |
| 82 | + } |
| 83 | + |
| 84 | + // We would like to preserve ordering, so let's use `LinkedHashMultisetClass`. |
| 85 | + private fun calculateDuplicates(list: Expression<List<*>>): Expression<List<*>> = |
| 86 | + Expression( |
| 87 | + """ |
| 88 | + $LinkedHashMultisetClass.create($list) |
| 89 | + .entrySet() |
| 90 | + .stream() |
| 91 | + .filter(e -> e.getCount() > 1) |
| 92 | + .map($MultiSetEntryClass::getElement) |
| 93 | + .collect($CollectorsClass.toList()) |
| 94 | + """.trimIndent() |
| 95 | + ) |
| 96 | + |
| 97 | + private fun fieldPath(parent: Expression<FieldPath>): Expression<FieldPath> = |
| 98 | + parent.toBuilder() |
| 99 | + .chainAdd("field_name", StringLiteral(field.name.value)) |
| 100 | + .chainBuild() |
| 101 | + |
| 102 | + private fun violation( |
| 103 | + fieldPath: Expression<FieldPath>, |
| 104 | + duplicates: Expression<List<*>> |
| 105 | + ): Expression<ConstraintViolation> { |
| 106 | + val qualifiedName = field.qualifiedName |
| 107 | + val placeholders = supportedPlaceholders(fieldPath, duplicates) |
| 108 | + val errorMessage = templateString(view.errorMessage, placeholders, PATTERN, qualifiedName) |
| 109 | + return constraintViolation(errorMessage, declaringType, fieldPath, getter) |
| 110 | + } |
| 111 | + |
| 112 | + private fun supportedPlaceholders( |
| 113 | + fieldPath: Expression<FieldPath>, |
| 114 | + duplicates: Expression<List<*>> |
| 115 | + ): Map<ErrorPlaceholder, Expression<String>> { |
| 116 | + val pathAsString = FieldPathsClass.call<String>("getJoined", fieldPath) |
| 117 | + return mapOf( |
| 118 | + FIELD_PATH to pathAsString, |
| 119 | + FIELD_VALUE to fieldType.stringValueOf(getter), |
| 120 | + FIELD_TYPE to StringLiteral(fieldType.name), |
| 121 | + PARENT_TYPE to StringLiteral(declaringType.qualifiedName), |
| 122 | + FIELD_DUPLICATES to duplicates.call("toString") |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
0 commit comments