Skip to content

Commit

Permalink
Use jsonb instead of varchar for complex enums (close #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
istreeter committed Jun 24, 2021
1 parent 283ba0b commit 09602ce
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
Expand Up @@ -57,7 +57,7 @@ object Type {
/** Derive a Postgres type, given JSON Schema */
def getDataType(properties: Schema, suggestions: List[DataTypeSuggestion]): Type =
suggestions match {
case Nil => Type.Varchar(DefaultVarcharSize) // Generic
case Nil => Type.Jsonb // Generic
case suggestion :: tail =>
suggestion(properties) match {
case Some(format) => format
Expand All @@ -69,8 +69,7 @@ object Type {
val complexEnumSuggestion: DataTypeSuggestion = properties =>
properties.enum match {
case Some(enums) if isComplexEnum(enums.value) =>
val longest = excludeNull(enums.value).map(_.noSpaces.length).maximumOption.getOrElse(16)
Some(Type.Varchar(longest))
Some(Type.Jsonb)
case _ => None
}

Expand Down Expand Up @@ -167,11 +166,6 @@ object Type {
case Some(max) => Some(Type.Varchar(max))
case None => Some(Type.Varchar(DefaultVarcharSize))
}
case (_, _, Some(e)) =>
e.value.map(jsonLength).maximumOption match {
case Some(max) => Some(Type.Varchar(max))
case None => Some(Type.Varchar(DefaultVarcharSize))
}
case _ => None
}
}
Expand All @@ -189,9 +183,6 @@ object Type {
varcharSuggestion
)

private def jsonLength(json: Json): Int =
json.fold(0, b => b.toString.length, _ => json.noSpaces.length, _.length, _ => json.noSpaces.length, _ => json.noSpaces.length)

/**
* Get set of types or enum as string excluding null
*
Expand Down
Expand Up @@ -95,7 +95,7 @@ class TypeSpec extends Specification {

"return jsonb type for a complex enum field" >> {
val properties = Schema(enum = Some(CommonProperties.Enum(List(Json.fromInt(12345), Json.fromString("y")))))
Type.getDataType(properties, Type.dataTypeSuggestions) must_== Type.Varchar(5)
Type.getDataType(properties, Type.dataTypeSuggestions) must_== Type.Jsonb
}

}
Expand Down

0 comments on commit 09602ce

Please sign in to comment.