Skip to content

Commit

Permalink
Merge pull request #1739 from blast-hardcheese/chore/preparing-for-1591
Browse files Browse the repository at this point in the history
chore/Preparing for 1591
  • Loading branch information
blast-hardcheese authored May 14, 2023
2 parents 427dd5f + 5199509 commit 6760c9b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 46 deletions.
4 changes: 3 additions & 1 deletion modules/core/src/main/scala/dev/guardrail/core/Mappish.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import cats.{ Eval, Foldable, Functor }

// Effectively Nested[F, (K, *), V]
// With some thought, this may just be expressible via a type alias
class Mappish[F[_], K, V](val value: F[(K, V)])
class Mappish[F[_], K, V](val value: F[(K, V)]) {
override def toString() = s"Mappish(${value.toString()})"
}

object Mappish {
def apply[F[_], A, B](value: F[(A, B)]): Mappish[F, A, B] = new Mappish(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,37 +346,34 @@ object ProtocolGenerator {
defaultPropertyRequirement,
components
)
defn <- renderDTOClass(clsName.last, supportPackage, params, parents)
encoder <- encodeModel(clsName.last, dtoPackage, params, parents)
decoder <- decodeModel(clsName.last, dtoPackage, supportPackage, params, parents)
tpe <- parseTypeName(clsName.last)
fullType <- selectType(dtoPackage.foldRight(clsName)((x, xs) => xs.prepend(x)))
staticDefns <- renderDTOStaticDefns(clsName.last, List.empty, encoder, decoder, params)
result <-
if (parents.isEmpty && props.isEmpty) (Left("Entity isn't model"): Either[String, ClassDefinition[L]]).pure[F]
else {
val nestedClasses = nestedDefinitions.flatTraverse {
case classDefinition: ClassDefinition[L] =>
for {
widenClass <- widenClassDefinition(classDefinition.cls)
companionTerm <- pureTermName(classDefinition.name)
companionDefinition <- wrapToObject(companionTerm, classDefinition.staticDefns.extraImports, classDefinition.staticDefns.definitions)
widenCompanion <- companionDefinition.traverse(widenObjectDefinition)
} yield List(widenClass) ++ widenCompanion.fold(classDefinition.staticDefns.definitions)(List(_))
case enumDefinition: EnumDefinition[L] =>
for {
widenClass <- widenClassDefinition(enumDefinition.cls)
companionTerm <- pureTermName(enumDefinition.name)
companionDefinition <- wrapToObject(companionTerm, enumDefinition.staticDefns.extraImports, enumDefinition.staticDefns.definitions)
widenCompanion <- companionDefinition.traverse(widenObjectDefinition)
} yield List(widenClass) ++ widenCompanion.fold(enumDefinition.staticDefns.definitions)(List(_))
}
nestedClasses.map { v =>
val finalStaticDefns = staticDefns.copy(definitions = staticDefns.definitions ++ v)
tpe.toRight("Empty entity name").map(ClassDefinition[L](clsName.last, _, fullType, defn, finalStaticDefns, parents))
}
}
} yield result
nestedClasses <- nestedDefinitions.flatTraverse {
case classDefinition: ClassDefinition[L] =>
for {
widenClass <- widenClassDefinition(classDefinition.cls)
companionTerm <- pureTermName(classDefinition.name)
companionDefinition <- wrapToObject(companionTerm, classDefinition.staticDefns.extraImports, classDefinition.staticDefns.definitions)
widenCompanion <- companionDefinition.traverse(widenObjectDefinition)
} yield List(widenClass) ++ widenCompanion.fold(classDefinition.staticDefns.definitions)(List(_))
case enumDefinition: EnumDefinition[L] =>
for {
widenClass <- widenClassDefinition(enumDefinition.cls)
companionTerm <- pureTermName(enumDefinition.name)
companionDefinition <- wrapToObject(companionTerm, enumDefinition.staticDefns.extraImports, enumDefinition.staticDefns.definitions)
widenCompanion <- companionDefinition.traverse(widenObjectDefinition)
} yield List(widenClass) ++ widenCompanion.fold(enumDefinition.staticDefns.definitions)(List(_))
}
defn <- renderDTOClass(clsName.last, supportPackage, params, parents)
} yield {
val finalStaticDefns = staticDefns.copy(definitions = staticDefns.definitions ++ nestedClasses)
if (parents.isEmpty && props.isEmpty) Left("Entity isn't model"): Either[String, ClassDefinition[L]]
else tpe.toRight("Empty entity name").map(ClassDefinition[L](clsName.last, _, fullType, defn, finalStaticDefns, parents))
}

}

private def prepareProperties[L <: LA, F[_]](
Expand Down Expand Up @@ -405,16 +402,27 @@ object ProtocolGenerator {
for {
nestedClassName <- formatTypeName(name).map(formattedName => getClsName(name).append(formattedName))
defn <- schema
.refine[F[Option[Either[String, NestedProtocolElems[L]]]]] { case x: ObjectSchema => x }(_ =>
fromModel(nestedClassName, schema, List.empty, concreteTypes, definitions, dtoPackage, supportPackage, defaultPropertyRequirement, components)
.map(Option(_))
.refine[F[Option[Either[String, NestedProtocolElems[L]]]]] { case x: ObjectSchema => x }(o =>
for {
defn <- fromModel(
nestedClassName,
o,
List.empty,
concreteTypes,
definitions,
dtoPackage,
supportPackage,
defaultPropertyRequirement,
components
)
} yield Option(defn)
)
.orRefine { case o: ComposedSchema => o }(o =>
for {
parents <- extractParents(o, definitions, concreteTypes, dtoPackage, supportPackage, defaultPropertyRequirement, components)
maybeClassDefinition <- fromModel(
nestedClassName,
schema,
o,
parents,
concreteTypes,
definitions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,17 +822,19 @@ class JacksonGenerator private (implicit Cl: CollectionsLibTerms[JavaLanguage, T

override def extractProperties(swagger: Tracker[Schema[_]]) =
swagger
.refine { case m: ObjectSchema => m }(m => Target.pure(m.downField("properties", _.getProperties()).indexedCosequence.value))
.orRefine { case c: ComposedSchema => c }(comp =>
Target.pure(
.refine[Target[List[(String, Tracker[Schema[_]])]]] { case m: ObjectSchema => m }(m =>
Target.pure(m.downField("properties", _.getProperties()).indexedCosequence.value)
)
.orRefine { case c: ComposedSchema => c } { comp =>
val extractedProps =
comp
.downField("allOf", _.getAllOf())
.indexedDistribute
.lastOption
.toList
.flatMap(_.downField("properties", _.getProperties).indexedCosequence.value.toList)
)
)
Target.pure(extractedProps)
}
.orRefine { case x: Schema[_] if Option(x.get$ref()).isDefined => x }(comp =>
Target.raiseUserError(s"Attempted to extractProperties for a ${comp.unwrapTracker.getClass()}, unsure what to do here (${comp.showHistory})")
)
Expand All @@ -853,20 +855,21 @@ class JacksonGenerator private (implicit Cl: CollectionsLibTerms[JavaLanguage, T
defaultValue: Option[com.github.javaparser.ast.Node]
) =
Target.log.function("transformProperty") {
val readOnlyKey = Option(name).filter(_ => property.downField("readOnly", _.getReadOnly).unwrapTracker.contains(true))
val emptyToNull =
property
val fallbackRawType = LiteralRawType(property.downField("type", _.getType()).unwrapTracker, property.downField("format", _.getFormat()).unwrapTracker)
for {
_ <- Target.log.debug(s"Args: (${clsName}, ${name}, ...)")

readOnlyKey = Option(name).filter(_ => property.downField("readOnly", _.getReadOnly()).unwrapTracker.contains(true))
emptyToNull = property
.refine { case d: DateSchema => d }(d => EmptyValueIsNull(d))
.orRefine { case dt: DateTimeSchema => dt }(dt => EmptyValueIsNull(dt))
.orRefine { case s: StringSchema => s }(s => EmptyValueIsNull(s))
.toOption
.flatten
.getOrElse(EmptyIsEmpty)
val dataRedaction = DataRedaction(property).getOrElse(DataVisible)

val fallbackRawType = LiteralRawType(property.downField("type", _.getType()).unwrapTracker, property.downField("format", _.getFormat()).unwrapTracker)
dataRedaction = DataRedaction(property).getOrElse(DataVisible)

for {
(tpe, classDep, rawType) <- meta match {
case core.Resolved(declType, classDep, _, rawType) =>
Target.pure((declType, classDep, rawType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,20 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
override def extractProperties(swagger: Tracker[Schema[_]]) =
swagger
.refine[Target[List[(String, Tracker[Schema[_]])]]] { case o: ObjectSchema => o }(m =>
Target.pure(m.downField("properties", _.getProperties).indexedCosequence.value)
Target.pure(m.downField("properties", _.getProperties()).indexedCosequence.value)
)
.orRefine { case c: ComposedSchema => c } { comp =>
val extractedProps =
comp.downField("allOf", _.getAllOf()).indexedDistribute.flatMap(_.downField("properties", _.getProperties).indexedCosequence.value)
comp
.downField("allOf", _.getAllOf())
.indexedDistribute
.flatMap(_.downField("properties", _.getProperties).indexedCosequence.value)
Target.pure(extractedProps)
}
.orRefine { case x: Schema[_] if Option(x.get$ref()).isDefined => x }(comp =>
Target.raiseUserError(s"Attempted to extractProperties for a ${comp.unwrapTracker.getClass()}, unsure what to do here (${comp.showHistory})")
)
.getOrElse(Target.pure(List.empty))
.map(_.toList)
.getOrElse(Target.pure(List.empty[(String, Tracker[Schema[_]])]))

override def transformProperty(
clsName: String,
Expand Down

0 comments on commit 6760c9b

Please sign in to comment.