Skip to content

Commit

Permalink
added validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ellizio committed Mar 18, 2024
1 parent 61d3167 commit 567ac8e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ class CliDialog(private val model: CliDialogModel) : DialogWrapper(false) {
textField()
.align(AlignX.FILL)
.bindText(model.serviceName)
.validationOnInput(model.validator.serviceNameValidation())
.validationOnApply(model.validator.serviceNameValidation())
}
row("Metadata source:") {
textFieldWithBrowseButton(fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor("xml"))
.align(AlignX.FILL)
.comment("The URI of the metadata document. The value must be set to a valid service document URI or a local file path", Int.MAX_VALUE)
.bindText(model.metadataUri)
.validationOnInput(model.validator.metadataUriValidation())
.validationOnApply(model.validator.metadataUriValidation())
}.bottomGap(BottomGap.SMALL)
row {
cell(tabbedPane)
Expand All @@ -56,12 +60,16 @@ class CliDialog(private val model: CliDialogModel) : DialogWrapper(false) {
.emptyText("Default: Reference.cs")
.comment("The name of the generated file")
.bindText(model.fileName)
.validationOnInput(model.validator.fileNameValidation())
.validationOnApply(model.validator.fileNameValidation())
}
row("--namespace-prefix") {
textField()
.align(AlignX.FILL)
.comment("The namespace of the client code generated")
.bindText(model.namespacePrefix)
.validationOnInput(model.validator.namespacePrefixValidation())
.validationOnApply(model.validator.namespacePrefixValidation())
}
row("--excluded-operation-imports") {
textField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.jetbrains.rider.model.RdCustomLocation
import com.jetbrains.rider.model.RdDependencyFolderDescriptor
import com.jetbrains.rider.model.RdProjectDescriptor
import com.jetbrains.rider.plugins.odatacliui.extensions.entityForAction
import com.jetbrains.rider.plugins.odatacliui.models.validators.CliDialogModelValidator
import com.jetbrains.rider.plugins.odatacliui.terminal.CommandBuilder
import com.jetbrains.rider.projectView.solution
import com.jetbrains.rider.projectView.workspace.ProjectModelEntity
Expand All @@ -16,6 +17,8 @@ import kotlin.io.path.Path
class CliDialogModel(event: AnActionEvent) {
private val connectedServicesDir: String

val validator = CliDialogModelValidator()

val cliVersion: String

init {
Expand Down Expand Up @@ -63,18 +66,18 @@ class CliDialogModel(event: AnActionEvent) {
private fun getOutputDir() = Path(connectedServicesDir, serviceName.get()).toString()

fun buildCommand(): GeneralCommandLine = CommandBuilder("odata-cli", "generate")
.addIfNotEmpty("--metadata-uri", metadataUri.get())
.addIfNotEmpty("--file-name", fileName.get())
.addIfNotEmpty("--custom-headers", customHeaders.get())
.addIfNotEmpty("--proxy", proxy.get())
.addIfNotEmpty("--namespace-prefix", namespacePrefix.get())
.addIfNotEmpty("--excluded-operation-imports", excludedOperationImports.get())
.addIfNotEmpty("--excluded-bound-operations", excludedBoundOperations.get())
.addIfNotEmpty("--excluded-schema-types", excludedSchemaTypes.get())
.addIfNotBlank("--metadata-uri", metadataUri.get())
.addIfNotBlank("--file-name", fileName.get())
.addIfNotBlank("--custom-headers", customHeaders.get())
.addIfNotBlank("--proxy", proxy.get())
.addIfNotBlank("--namespace-prefix", namespacePrefix.get())
.addIfNotBlank("--excluded-operation-imports", excludedOperationImports.get())
.addIfNotBlank("--excluded-bound-operations", excludedBoundOperations.get())
.addIfNotBlank("--excluded-schema-types", excludedSchemaTypes.get())
.addFlag("--upper-camel-case", upperCamelCase.get())
.addFlag("--internal", internal.get())
.addFlag("--multiple-files", multipleFiles.get())
.addFlag("--ignore-unexpected-elements", ignoreUnexpectedElements.get())
.addIfNotEmpty("--outputdir", getOutputDir())
.addIfNotBlank("--outputdir", getOutputDir())
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jetbrains.rider.plugins.odatacliui.models.validators

import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.layout.ValidationInfoBuilder
import javax.swing.JTextField

class CliDialogModelValidator {
fun serviceNameValidation(): ValidationInfoBuilder.(JTextField) -> ValidationInfo? = {
if (it.text.trim().isEmpty())
error("Service name must not be empty")
else
null
}

fun metadataUriValidation(): ValidationInfoBuilder.(TextFieldWithBrowseButton) -> ValidationInfo? = {
if (it.text.trim().isEmpty())
error("Metadata source must not be empty")
else
null
}

fun fileNameValidation(): ValidationInfoBuilder.(JTextField) -> ValidationInfo? = {
if (it.text.trim().isNotEmpty() && it.text.contains(' '))
error("File name must not contain white spaces")
else
null
}

fun namespacePrefixValidation(): ValidationInfoBuilder.(JTextField) -> ValidationInfo? = {
if (it.text.trim().isNotEmpty() && it.text.contains(' '))
error("Namespace prefix must not contain whitespaces")
else
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class CommandBuilder(toolName: String, commandName: String) {
command.addParameter(commandName)
}

fun addIfNotEmpty(parameterName: String, parameterValue: String?): CommandBuilder {
if (parameterValue.isNullOrEmpty()) {
fun addIfNotBlank(parameterName: String, parameterValue: String?): CommandBuilder {
if (parameterValue.isNullOrBlank()) {
return this
}

Expand Down

0 comments on commit 567ac8e

Please sign in to comment.