Skip to content

Commit

Permalink
Add options with default method
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Dai <[email protected]>
  • Loading branch information
dai-chen committed Oct 6, 2023
1 parent 49d1482 commit 274b5a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ case class FlintSparkIndexOptions(options: Map[String, String]) {
*/
def indexSettings(): Option[String] = getOptionValue(INDEX_SETTINGS)

/**
* @return
* all option values and fill default value if unspecified
*/
def optionsWithDefault: Map[String, String] = {
val map = Map.newBuilder[String, String]
map ++= options

if (!options.contains(AUTO_REFRESH.toString)) {
map += (AUTO_REFRESH.toString -> autoRefresh().toString)
}
map.result()
}

private def getOptionValue(name: OptionName): Option[String] = {
options.get(name.toString)
}
Expand All @@ -76,6 +90,10 @@ object FlintSparkIndexOptions {
val CHECKPOINT_LOCATION: OptionName.Value = Value("checkpoint_location")
val INDEX_SETTINGS: OptionName.Value = Value("index_settings")

/**
* @return
* convert enum name to lowercase as public option name
*/
override def toString(): String = {
super.toString().toLowerCase(Locale.ROOT)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ class FlintSparkIndexOptionsSuite extends FlintSuite with Matchers {
options.refreshInterval() shouldBe empty
options.checkpointLocation() shouldBe empty
options.indexSettings() shouldBe empty
options.optionsWithDefault should contain("auto_refresh" -> "false")
}

test("should return default option value if unspecified with specified value") {
val options = FlintSparkIndexOptions(Map("refresh_interval" -> "1 Minute"))

options.optionsWithDefault shouldBe Map(
"auto_refresh" -> "false",
"refresh_interval" -> "1 Minute")
}

test("should report error if any unknown option name") {
the [IllegalArgumentException] thrownBy
the[IllegalArgumentException] thrownBy
FlintSparkIndexOptions(Map("autoRefresh" -> "true"))

the [IllegalArgumentException] thrownBy {
FlintSparkIndexOptions(Map(
"auto_refresh" -> "true",
"indexSetting" -> "test"
))
the[IllegalArgumentException] thrownBy {
FlintSparkIndexOptions(Map("auto_refresh" -> "true", "indexSetting" -> "test"))
} should have message "requirement failed: option name indexSetting is invalid"
}
}

0 comments on commit 274b5a5

Please sign in to comment.